Asmi
Asmi

Reputation: 365

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();
   }

My 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)

I am getting proper response but it not get parse and add to respective table. What is the error i cant get it. can anyone check my code is correct or not. This method is write in service class to send the data on background thread and fetch the response from server. Hi if i remove dbHelper.updateSyncStatus(rid, responcerecent); line then its properly work without ANR but i want to add that response to table in db.. so how can I solve this

Upvotes: 0

Views: 103

Answers (1)

Erik Hellman
Erik Hellman

Reputation: 539

The HTTP client you are using is deprecated and shouldn't be used. I recommend that you use OkHttp instead as that is much easier to use and more stable.

Upvotes: 1

Related Questions