gpt4 book ai didi

android - HTTP 响应 - Android

转载 作者:行者123 更新时间:2023-11-29 22:20:46 27 4
gpt4 key购买 nike

我正在尝试让我的应用程序向我的网络服务器(位于 IP 10.0.2.2)发布一些内容并接收响应。我正在使用 HttpHelper 对象来执行所有服务器交互方法。目前我的响应变量没有得到任何返回。通过调试我知道循环:

while ((line = rd.readLine()) != null) {

从未输入过,但应该输入,因为它应该肯定会收到“OK”行?

非常感谢您的帮助!

网页代码就是:

header("HTTP/1.1 200 OK");

java代码是:

        List<NameValuePair> data = new ArrayList<NameValuePair>();
HttpHelper httpHelper = new HttpHelper("http://10.0.2.2/", data);
StringBuilder response = httpHelper.postData();

...

public class HttpHelper {
final HttpClient client;
final HttpPost post;
final List<NameValuePair> data;

public HttpHelper(String address, List<NameValuePair> data) {
client = new DefaultHttpClient();
post = new HttpPost(address);
this.data = data;
}

private class GetResponseTask extends AsyncTask<Void, Void, StringBuilder> {
protected StringBuilder doInBackground(Void... arg0) {
try {
HttpResponse response = client.execute(post);
return inputStreamToString(response.getEntity().getContent());
} catch (ClientProtocolException e) {
Log.e("debug", e.getLocalizedMessage());
} catch (IOException e) {
Log.e("debug", e.getLocalizedMessage());
}
return null;
}
}

public StringBuilder postData() {
try {
post.setEntity(new UrlEncodedFormEntity(data));
return (new GetResponseTask().execute()).get();
} catch (UnsupportedEncodingException e) {
Log.e("debug", e.getLocalizedMessage());
} catch (InterruptedException e) {
Log.e("debug", e.getLocalizedMessage());
} catch (ExecutionException e) {
Log.e("debug", e.getLocalizedMessage());
}
return null;
}

private StringBuilder inputStreamToString(InputStream is)
throws IOException {
String line = "";
StringBuilder total = new StringBuilder();

// Wrap a BufferedReader around the InputStream
BufferedReader rd = new BufferedReader(new InputStreamReader(is));

// Read response until the end
while ((line = rd.readLine()) != null) {
Log.v("debug", "top of readlineloop");
total.append(line);
Log.v("debug-readline", line);
}
// Return full string
return total;
}
}

最佳答案

响应实体,即 getEntity() 返回的实体,不包括响应 header ,看起来这就是您感兴趣的内容。要检索状态代码,请使用:

response.getStatusLine().getStatusCode()

“实体”是响应内容。

编辑:要从 PHP 发出内容,请使用 echo , print_r() , var_dump() ,许多其他功能,以及 - 最重要的 - <?php...?> 之外的所有文本标签。

关于android - HTTP 响应 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7285831/

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