不能得到的Htt presponse的全身全身、Htt、presponse

由网友(追着太阳跑)分享简介:我把我的code中的AsyncTask,在OnPostExecution,结果在Dialog是从被记录在logcat中的不同。一个在logcat中是不完整的,一来在对话结束I put my code in AsyncTask, in OnPostExecution, the result in Dialog is...

我把我的code中的AsyncTask,在OnPostExecution,结果在Dialog是从被记录在logcat中的不同。 一个在logcat中是不完整的,一来在对话结束

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

下面是我的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);
    }       

在法的onCreate(),我执行DownloadTask:

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

我想收到完整的结果来分析,以JSON

I want to receive complete result to parse to JSON

推荐答案

尝试:

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

和看读/一个InputStream转换为字符串如果您真的需要它作为一个字符串

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

维护它作为一个的InputStream 并通过是的JSON解析器会更好,虽然。

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

阅读全文

相关推荐

最新文章