gpt4 book ai didi

java - 无法获得完整的 HttpResponse

转载 作者:行者123 更新时间:2023-11-30 02:48:50 27 4
gpt4 key购买 nike

我将我的代码放在 AsyncTask 中,在 OnPostExecution 中,Dialog 中的结果与 Logcat 中记录的结果不同。Logcat里的不全,Dialog里的全

这是我的代码:

    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:

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

最佳答案

尝试:

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

并查看 Read/convert an InputStream to a String如果您真的需要它作为String

将其维护为 InputStream 并将that 传递给 JSON 解析器会更好。

关于java - 无法获得完整的 HttpResponse,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24563604/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com