繁体   English   中英

解析响应的json字符串时出现错误

[英]I am getting an error when i parse the json string which is response

private String getHttpPost(String url, List<NameValuePair> params) {
// TODO Auto-generated method stub


     HttpClient client = new DefaultHttpClient();
     HttpPost httpPost = new HttpPost(url);
    //  Log.d("Entire httppost::", " " + httpPost);
     //httpPost.setHeader("Accept", "application/json");
      //  httpPost.setHeader("Content-type", "application/json");
     try {
    httpPost.setEntity(new UrlEncodedFormEntity(params));

    HttpResponse response = client.execute(httpPost);
    HttpEntity entity = response.getEntity();
    is = entity.getContent();
    Log.e("server Response", "json format "+is);
     } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
        e.printStackTrace();
        }
        // return str.toString();
     try{
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line="0";

            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }

            is.close();
            result=sb.toString();
            //Log.d("Json array as response", " " + sb);
        }catch(Exception e){
            Log.e("log_tag", "Error converting result "+e.toString());
        }

        //paring data
     if (result != null) {
        try{
        jArray = new JSONArray(result);

        Log.d("Json array as response", " " + jArray);

        for(int a=0; a<jArray.length(); a++){
            JSONObject json_data = jArray.getJSONObject(a);

                rid = json_data.optString("id").toString();  
                responcerecent = json_data.optString("response").toString();
               Log.d("Id",""+ rid);
                Log.d("Response", " "+ responcerecent);
                dbHelper.updateSyncStatus(rid, responcerecent);
        }



        }catch(JSONException e1){
             e1.printStackTrace();
        }catch (ParseException e2){
            e2.printStackTrace();
        }

        //dbHelper.delete_classdata();
        //Log.d("All data relatedclass table deleted", "dbHelper.delete_classdata()");  
    }  

     else {
        Log.e("ServiceHandler", "Couldn't get any data from the url");
    }


    return sb.toString();
   }

我的logcat

08-20 13:41:32.670: D/Json array as response(11758):  [{"response":"1","id":"3"}]
08-20 13:41:32.670: D/Id(11758): 3
08-20 13:41:32.670: D/Response(11758):  1
08-20 13:41:32.670: W/dalvikvm(11758): threadid=11: thread exiting with uncaught exception (group=0x41561ba8)
08-20 13:41:32.670: E/AndroidRuntime(11758): FATAL EXCEPTION: Thread-10188
08-20 13:41:32.670: E/AndroidRuntime(11758): Process: com.edbeans.attendance:my_process, PID: 11758
08-20 13:41:32.670: E/AndroidRuntime(11758): java.lang.NullPointerException
08-20 13:41:32.670: E/AndroidRuntime(11758):    at com.edbeans.attendance.MyService.getHttpPost(MyService.java:325)
08-20 13:41:32.670: E/AndroidRuntime(11758):    at com.edbeans.attendance.MyService.access$0(MyService.java:272)
08-20 13:41:32.670: E/AndroidRuntime(11758):    at com.edbeans.attendance.MyService$1.run(MyService.java:207)
08-20 13:41:32.670: E/AndroidRuntime(11758):    at java.lang.Thread.run(Thread.java:841)

我得到适当的响应,但它没有解析并添加到相应的表中。 我无法得到什么错误。 任何人都可以检查我的代码是否正确。 此方法写入服务类中,以在后台线程上发送数据并从服务器获取响应。 嗨,如果我删除dbHelper.updateSyncStatus(rid,responcerecent); 行,然后它可以在没有ANR的情况下正常工作,但是我想将该响应添加到db表中。

您正在使用的HTTP客户端已被弃用,不应使用。 我建议您改用OkHttp ,因为它更易于使用且更稳定。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM