【问题标题】:HTTP Responses - AndroidHTTP 响应 - Android
【发布时间】:2011-09-02 15:35:58
【问题描述】:

我正在尝试让我的应用程序向我的网络服务器(位于 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;
}
}

【问题讨论】:

    标签: android http-headers httpresponse


    【解决方案1】:

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

    response.getStatusLine().getStatusCode()
    

    “实体”是响应内容。

    编辑:要从 PHP 发出内容,请使用 echoprint_r()var_dump()、许多其他函数,以及 - 最重要的是 - 所有在 &lt;?php...?&gt; 标记之外的文本。

    【讨论】:

    • 啊,我明白了,谢谢。你知道设置响应内容的php代码吗?
    • 按照php.net/manual/function.header.php尝试header("HTTP/1.0 123 somemessage");
    • “响应内容”,至少在getContent() 方法的意义上,代表响应数据,而不是HTTP/响应行末尾的状态字符串。让 Jonny 决定他到底需要什么……
    • 感谢大家的帮助。我可以问一下 - 我是否以正确的方式将我的 android 应用程序与网络服务器连接起来,还是有比使用 http 响应更好的方法?
    • “响应”的定义是什么?似乎有两个漂浮在周围。通常,他们使用 JSON 或 XML 等易于解析的格式的响应数据,并让 Web 服务器生成状态行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    相关资源
    最近更新 更多