1

I put my code in AsyncTask, in OnPostExecution, the result in Dialog is different from the one which is logged in Logcat. The one in Logcat is incomplete, the one in Dialog is complete

Here is my code:

    private class DownloadTask extends AsyncTask<String, Void, String>{     
    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub          
        String url =params[0];
        String data = "";
        try{
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost,localContext);
            int status = httpResponse.getStatusLine().getStatusCode();
            if(status == 200){ //Receive OK
                data = EntityUtils.toString(httpResponse.getEntity());
            }
        }catch(Exception e){
            Log.e("HTTP", "ERROR");
        }
        return data;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);    
            AlertDialog.Builder builder = new Builder(MainActivity.this);
            builder.setMessage(result);
            builder.setPositiveButton("OK", null);
            builder.create().show();
            Log.d("Response", result);
    }       

In method onCreate(), I executed DownloadTask:

String url = "https://maps.googleapis.com/maps/api/directions/json?origin=10.9052136,106.6928473&destination=10.736465,106.710779&sensor=false&units=metric&mode=driving";

DownloadTask downloadTask = new DownloadTask();
downloadTask.execute(url);

I want to receive complete result to parse to JSON

1 Answers1

1

Try:

InputStream in = httpResponse.getEntity().getContent();

and see Read/convert an InputStream to a String if you really need it as a String.

Maintaining it as an InputStream and passing that to the JSON parser would be better though.

Community
  • 1
  • 1
OldCurmudgeon
  • 64,482
  • 16
  • 119
  • 213