无法从 http 响应中找到正文数据

发布于 2024-10-17 06:17:36 字数 1580 浏览 1 评论 0原文

我正在执行名称/值对的 HTTP POST,然后尝试获取 HTTP 响应正文(并将其放入名为描述的字符串中)。

我无法找到访问响应消息正文的方法。下面是我迄今为止想到的最好的方法,但它不起作用。 “描述”变量最终变成了空。

最终目标是在服务器端编写代码来获取发布数据并返回包含有用信息的消息正文。现在我只有一个存根 html,它总是返回相同的东西(参见下面的 html)。换句话说,应用程序将向服务器发送 DTC(诊断故障代码,来自汽车),服务器将查找它并发回 HTML 正文内的描述文本。

该方法的源代码:

public String getDTCDescription (String DTC) {
    String description = "";

    String url = "http://site/test.html";
    List<NameValuePair> args = new ArrayList<NameValuePair>();

    args.add(new BasicNameValuePair("testkey","testValue")); 

    HttpResponse h;

    h = postData(url, args);

    BufferedInputStream content = null; 
    try {
        content = new BufferedInputStream(h.getEntity().getContent());
    } catch (Exception e) {
        Log.e("NetDTCInfo",e.getMessage());
    }

    if (content == null) {
        return "";
    }

    // try and loop through all the data waiting on the input stream. 
    try {
        while (content.available() > 0) {
            Log.d("NetDTCInfo","Reading a byte...");
            description = description + (char) content.read();
            Log.d("NetDTCInfo","Description is now: " + description);
        }
    } catch (IOException e) {
        Log.e("NetDTCInfo","Error while reading. " + e.getMessage());
    }


    return description;
}

文件 test.html 的源代码

<html>
        <body>
                <pre>
                        P0521|This is a description of DTC code P0521.
                </pre>
        </body>
</html>

I am performing an HTTP POST of a name/value pair and then trying to obtain the HTTP response body (and place it into the String called description).

I'm unable to find a way to access the body of the response message. What I have below is the best I've come up with so far but it doesn't work. The "description" variable just ends up empty.

The end goal is to have code on the sever-side which takes the post data and returns a message body containing useful information. For now I just have a stub html in place that always returns the same thing (see html below). In other words, the app will send a DTC (Diagnostic Trouble Code, from a car) to the server and the server will look it up and send back the description text inside of the HTML body.

Source code to the method:

public String getDTCDescription (String DTC) {
    String description = "";

    String url = "http://site/test.html";
    List<NameValuePair> args = new ArrayList<NameValuePair>();

    args.add(new BasicNameValuePair("testkey","testValue")); 

    HttpResponse h;

    h = postData(url, args);

    BufferedInputStream content = null; 
    try {
        content = new BufferedInputStream(h.getEntity().getContent());
    } catch (Exception e) {
        Log.e("NetDTCInfo",e.getMessage());
    }

    if (content == null) {
        return "";
    }

    // try and loop through all the data waiting on the input stream. 
    try {
        while (content.available() > 0) {
            Log.d("NetDTCInfo","Reading a byte...");
            description = description + (char) content.read();
            Log.d("NetDTCInfo","Description is now: " + description);
        }
    } catch (IOException e) {
        Log.e("NetDTCInfo","Error while reading. " + e.getMessage());
    }


    return description;
}

Source code to the file test.html

<html>
        <body>
                <pre>
                        P0521|This is a description of DTC code P0521.
                </pre>
        </body>
</html>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

巡山小妖精 2024-10-24 06:17:36

看起来你这样做的策略很复杂,而不是在响应中发送一个 HTML 页面,只需发送一个你想要最终在客户端得到的字符串。如果您的响应消息中有更复杂的数据,我还建议您在客户端使用 XML 响应和 SAX 解析。

It looks your stratagy to do this is complex, rahter than sending a HTML page in respose just send a string which you want to get finally at your client side. One more thing i would suggest if you have more complex data in response message then use XML response and SAX Parsing at your client end.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文