>

JSONArray에서 HashMap을 가져 와서 SQLite로 보냅니다. 나에게주는 :    java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String. HashMap conversion issue.

유형은 HashMap<정수, HashMap<문자열, 문자열 >>입니다.

for(int i=0;i<adsarray.length();i++){
      JSONObject obj=adsarray.getJSONObject(i);
      HashMap mapReceivedAdDetails=new HashMap();
      HashMap mapBusinessInfo=new HashMap();    
      //mapBusinessInfo.put
      mapReceivedAdDetails.put("RecAdId", obj.getInt("received_ad_id"));
      mapReceivedAdDetails.put("RecBusinessId",obj.getInt("business_id"));
      mapReceivedAdDetails.put("RecBusinessId",obj.getInt("user_id"));
      mapReceivedAdDetails.put("RecAdtitle",obj.getString("received_ad_title"));
      mapReceivedAdDetails.put("RecAdDetail",obj.getString("received_ad_detail"));
      mapReceivedAdDetails.put("RecBusinessZip",obj.getString("business_zip"));
      mapReceivedAdDetails.put("RecBusinessCountry", obj.getString("business_country"));
      mapReceivedAdDetails.put("AdCreated", obj.getString("adreceived_created"));
      mapReceivedAd.put(i, mapReceivedAdDetails);
     //Log.i("ReceivedId",mapReceivedAd.toString());
      mapBusinessInfo.put("BusinessId",obj.getInt("business_id"));
      mapBusinessInfo.put("BusUserId",obj.getInt("user_id"));
      mapBusinessInfo.put("BusName",obj.getString("business_name"));
      mapBusinessInfo.put("BusSt1",obj.getString("business_street1"));
      mapBusinessInfo.put("BusSt2",obj.getString("business_street2"));
      mapBusinessInfo.put("BusCity",obj.getString("business_city"));
      mapBusinessInfo.put("BusState",obj.getString("business_state"));
      mapBusinessInfo.put("BusCountry",obj.getString("business_country"));
      mapBusinessInfo.put("BusZip",obj.getString("business_zip"));
      mapBusinessInfo.put("BusContactNo",obj.getString("business_contactno"));
      mapBusinessInfo.put("BusWebsite",obj.getString("business_website"));
      mapBusinessInfo.put("BusCreated",obj.getString("business_created"));
      mapBusinessDetail.put(i,mapBusinessInfo);
    // Log.i("BusinessDetail",mapBusinessDetail.toString());

     /* ad_business_id=obj.getInt("business_id");
      ad_user_id=obj.getInt("user_id");
      receive_ad_title=obj.getString("received_ad_title");
      receive_ad_detail=obj.getString("received_ad_detail");
      ad_business_zip=obj.getString("business_zip");
      ad_business_country=obj.getString("business_country");*/
      //ad_date_created=obj.getString("date_created");
      //Log.i("+ad_business_id+"+"+ad_user_id+" "+receive_ad_title+" "+receive_ad_detail+" "+ad_business_zip+" "+ad_business_country+" ");
 }

SQLIte 코드

while(i<mapBusinessInfo.size())
    {
      insert.bindLong(1, Integer.parseInt(mapBusinessInfo.get(i).get("BusUserId")));
      insert.bindLong(2, Integer.parseInt(mapBusinessInfo.get(i).get("BusinessId")));
      insert.bindString(3, mapBusinessInfo.get(i).get("BusName"));
      insert.bindString(4, mapBusinessInfo.get(i).get("BusSt1"));
      insert.bindString(5, mapBusinessInfo.get(i).get("BusSt2"));
      insert.bindString(6, mapBusinessInfo.get(i).get("BusCity"));
      insert.bindString(7, mapBusinessInfo.get(i).get("BusState"));
      insert.bindString(8, mapBusinessInfo.get(i).get("BusCountry"));
      insert.bindString(9, mapBusinessInfo.get(i).get("BusZip"));
      insert.bindString(10, mapBusinessInfo.get(i).get("BusContactNo"));
      insert.bindString(11, mapBusinessInfo.get(i).get("BusWebsite"));
      insert.bindLong(12, Long.parseLong(mapBusinessInfo.get(i).get("BusCreated")));
      insert.execute();
      i++;
    }
    db.setTransactionSuccessful();
    Log.i("Transaction", "Successful");
}

오류 :

insert.bindLong(1, Integer.parseInt(mapBusinessInfo.get(i).get("BusUserId")));

Logcat은 다음과 같습니다 :

03-13 09:05:23.034 18810-18866/? W/System.err: java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String 03-13 09:05:23.034 18810-18866/? W/System.err: at com.example.SQLITEDatabase.DatabaseHandle.InserBusinessInfo(DatabaseHandle.java:143) 03-13 09:05:23.042 18810-18866/? W/System.err: at com.example.deals.NewUserDetailPage$fetchadvertisements.doInBackground(NewUserDetailPage.java:283) 03-13 09:05:23.042 18810-18866/? W/System.err: at com.example.deals.NewUserDetailPage$fetchadvertisements.doInBackground(NewUserDetailPage.java:255) 03-13 09:05:23.046 18810-18866/? W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287) 03-13 09:05:23.062 18810-18866/? W/System.err: at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 03-13 09:05:23.062 18810-18866/? W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:137) 03-13 09:05:23.066 18810-18866/? W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 03-13 09:05:23.070 18810-18866/? W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 03-13 09:05:23.070 18810-18866/? W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 03-13 09:05:23.086 18810-18866/? W/System.err: at java.lang.Thread.run(Thread.java:856) 03-13 09:05:23.138 18810-18866/? W/System.err: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 03-13 09:05:23.170 18810-18866/? W/System.err: at android.os.Handler.(Handler.java:121) 03-13 09:05:23.170 18810-18866/? W/System.err: at android.widget.Toast$TN.(Toast.java:322) 03-13 09:05:23.174 18810-18866/? W/System.err: at android.widget.Toast.(Toast.java:91) 03-13 09:05:23.186 18810-18866/? W/System.err: at android.widget.Toast.makeText(Toast.java:238) 03-13 09:05:23.186 18810-18866/? W/System.err: at com.example.deals.NewUserDetailPage$fetchadvertisements.doInBackground(NewUserDetailPage.java:285) 03-13 09:05:23.194 18810-18866/? W/System.err: at com.example.deals.NewUserDetailPage$fetchadvertisements.doInBackground(NewUserDetailPage.java:255) 03-13 09:05:23.206 18810-18866/? W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287) 03-13 09:05:23.214 18810-18866/? W/System.err: at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 03-13 09:05:23.218 18810-18866/? W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:137) 03-13 09:05:23.222 18810-18866/? W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 03-13 09:05:23.254 18810-18866/? W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076) 03-13 09:05:23.274 18810-18866/? W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569) 03-13 09:05:23.282 18810-18866/? W/System.err: at java.lang.Thread.run(Thread.java:856) 03-13 09:05:23.454 320-320/? W/InputMethodManagerService: Window already focused, ignoring focus gain of: [email protected] attribute=null

  • 답변 # 1

    Hashmap 를 자유롭게 변환 할 수 있습니다   String 로  전혀 범죄가 없습니다. 그러나 당신이하고있는 일은 mapReceivedAdDetails 라는 Hasmap을 가지고 있다는 것입니다.  스 니펫 mapReceivedAd 에 표시되지 않은 다른 변수를 변환하려고합니다. .

    그렇지 않고

    mapReceivedAd.toString();
    
    

    실행

    mapReceivedAdDetails.toString();
    
    

관련 자료

  • 이전 python - tkinterfiledialog에서 askopenfilename을 사용하면 최상위 레벨 창이 사라집니다
  • 다음 php - 입력 값을 막대 차트에 전달하는 방법