不能得到的Htt presponse的全身 [英] Can't get full body of HttpResponse

查看:275
本文介绍了不能得到的Htt presponse的全身的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把我的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.

这篇关于不能得到的Htt presponse的全身的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆