2015-10-07 56 views
-2

我有一段基本上同步在线数据库之间的数据的代码。然而,我得到一个特定的代码行(map.put("id", obj.get(mydb.WEB_ID).toString());)的错误,其中一个整数值从android sqlite数据库中获得并提交给在线数据库。全COSE是显示如下:Android HashMap问题

public void updateSQLite(String response){ 
    ArrayList<HashMap<String, String>> syncL; 
    syncL = new ArrayList<HashMap<String, String>>(); 
    // Create GSON object 
    Gson gson = new GsonBuilder().create(); 
    try { 
     // Extract JSON array from the response 
     JSONArray arr = new JSONArray(response); 
     System.out.println(arr.length()); 
     // If no of array elements is not zero 
     if(arr.length() != 0){ 
      // Loop through each array element, get JSON object which has userid and username 
      for (int i = 0; i < arr.length(); i++) { 
       // Get JSON object 
       JSONObject obj = (JSONObject) arr.get(i); 
       System.out.println(obj.get("web_id")); 
       System.out.println(obj.get("phone_id")); 
       System.out.println(obj.get("msg_id")); 
       mydb.updateWebSync(obj.get(obj.get("phone_id").toString(), obj.get("msg_id").toString(), obj.get("web_id").toString()); 

       HashMap<String, String> map = new HashMap<String, String>(); 
       map.put("id", obj.get(mydb.WEB_ID).toString()); 
       map.put("p_id", obj.get(mydb.COLUMN_ID).toString()); 
       map.put("s", "1"); 
       syncL.add(map); 
      } 
      updateMySQLSyncSts(gson.toJson(syncL), "syncsts"); 
      Toast.makeText(getApplicationContext(), "Download Messages success!", Toast.LENGTH_SHORT).show(); 
     } 
    } catch (JSONException e) { 
     Toast.makeText(getApplicationContext(), "Download Messages error!", Toast.LENGTH_SHORT).show(); 
     e.printStackTrace(); 
    } 
} 

在我的Android SQLite数据库中,mydb.WEB_ID的值存储为一个整数。任何援助表示赞赏。

+0

什么样的错误?编译时间或运行时间?如果使用'String.valueOf(obj.get(“web_id”))''而不是'obj.get(mydb.WEB_ID).toString()'会发生什么? – Blackbelt

+0

错误仍然相同。至于它是编译时还是运行时错误,我真的不知道其中的差别,但错误开始像'13704-13704 /? W/System.err:' – Biko

回答

0

我想通了我的错误...我打电话给HashMap这是不同于json变量的数据库列名称。非常感谢您的协助。

0
Hashmap<String,String> 

它只包含字符串值。所以你必须将它转换为String。 使用

toString() 

函数它会给你错误。

尝试用

String.ValueOf(obj.get("web_id")) 

它会在整数值转换为字符串和您的问题得到解决。

快乐编码。 :P

+0

不幸的是,这个解决方案没有工作 – Biko

+0

只是在你的logcat中打印你的(obj.get(“web_id”))并给我这个值。 –

+0

试一试这段代码首先检查你从DB获得的内容的长度(),如果长度()> 0,然后转换它。所以只需添加if((obj.get(“web_id”))。toString()。length()> 0) –