4

I'm making an app a part of which involves downloading data from my server in JSON format. I'm using HTTP Post to achieve the same. The JSON file I'm downloading is around 2-3 Kb in size. I'm doing all these operations on a different thread.

However, the performance of my app is very unpredictable. I tried debugging it and found that sometimes my app gets stuck at line where I'm creating HttpPost object and sometimes it hardly takes 1-2 seconds to execute that line. What can be the issue ? Can it be because my app is taking lot of memory ? One more thing, my app runs fine second time once I force close it. Thanks !

public String getJSONString(String url) {
    try {
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(url);
        HttpResponse httpResponse = httpClient.execute(httpPost);
        HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) 
        {sb.append(line + "n");}
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        Log.e("Buffer Error", "Error converting result " + e.toString());
    }
    return json;

}

}

Gaurav Arora
  • 17,124
  • 5
  • 33
  • 44
  • Tell me, where are you testing your app? Is it in the emulator or on the device itself? – kittu88 Oct 15 '12 at 06:36
  • 1
    Is this all running in a separate thread from the main UI thread? I would hope so, because otherwise shouldn't even work on all versions of Android. How about posting the thread creation as well, because that might help determine issues. – MikeIsrael Oct 15 '12 at 06:46
  • I've tested it both on device and emulator. I'm doing it on a separate thread ! – Gaurav Arora Oct 15 '12 at 07:23

2 Answers2

1

you can try by this code..

                    // Create a new HttpClient and Post Header
        httpclient = new DefaultHttpClient();
        httppost = new HttpPost(your URL) 

        // Sending details in Json Format
        JSONObject holder = new JSONObject();
        try { 
                     ------------
                   --------------

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

    StringEntity se = new StringEntity(holder.toString());
    se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
    httppost.setEntity(se);
    response = httpclient.execute(httppost);



            // Sends data to server

        StringEntity se = new StringEntity(holder.toString());

        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
                "application/json"));

        httppost.setEntity(se);

        response = httpclient.execute(httppost);

        resp = response.toString();

        String responseServer = EntityUtils.toString(response.getEntity());
Rahul Baradia
  • 11,802
  • 17
  • 73
  • 121
Satyam
  • 1,672
  • 3
  • 20
  • 34
0

I saw this post while having the same problem as you

HTTP Post requests using HttpClient take 2 seconds, why?

can you confirm if its working?

HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
HttpClient httpclient = new DefaultHttpClient(params);
Community
  • 1
  • 1
WillingLearner
  • 739
  • 6
  • 10