当我解析响应

时间:2015-08-20 08:16:15

标签: android json

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中添加该表的响应..所以我该如何解决这个

1 个答案:

答案 0 :(得分:1)

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