Android 中的 HTTP Post 问题

发布于 2024-09-18 03:16:41 字数 1241 浏览 5 评论 0原文

我在 Android 中进行 HTTP POST 时遇到问题。

当代码读取响应时出现问题,它无法获取我想要检索的完整网页代码。

我只检索了网络的一部分。

这是代码:

    try {

        HttpClient httpclient = new DefaultHttpClient(); 

        HttpPost httppost = new HttpPost(URL);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
        nameValuePairs.add(new BasicNameValuePair("text", "06092010"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

        HttpResponse response; 
        response=httpclient.execute(httppost);

        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));


        String  s = "";
        String line = reader.readLine();

        while(line != null){
            s += line+"\n";
            line = reader.readLine();
        }


        Log.d("Street", "Result: "+s);          


    } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block          
        Log.d("Street", e.toString());   
    } catch (IOException e) { 
        // TODO Auto-generated catch block          
        Log.d("Street", e.toString());
    } catch (Exception e) {
        Log.d("Street", e.toString());
    }

I'm getting a problem making an HTTP POST in Android.

The problem occur when the code is reading the response, it cant obtain the complete web page code I want to retrieve.

I only retrieve a piece of the web.

Here is the code:

    try {

        HttpClient httpclient = new DefaultHttpClient(); 

        HttpPost httppost = new HttpPost(URL);
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
        nameValuePairs.add(new BasicNameValuePair("text", "06092010"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

        HttpResponse response; 
        response=httpclient.execute(httppost);

        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));


        String  s = "";
        String line = reader.readLine();

        while(line != null){
            s += line+"\n";
            line = reader.readLine();
        }


        Log.d("Street", "Result: "+s);          


    } catch (ClientProtocolException e) { 
        // TODO Auto-generated catch block          
        Log.d("Street", e.toString());   
    } catch (IOException e) { 
        // TODO Auto-generated catch block          
        Log.d("Street", e.toString());
    } catch (Exception e) {
        Log.d("Street", e.toString());
    }

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

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

发布评论

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

评论(2

仙气飘飘 2024-09-25 03:16:41

使用您的代码我得到了相同的结果,但现在我知道问题所在了。

问题不是代码,而是 Android LogCat(记录器),我在其中打印结果字符串。在此记录器中,如果字符串太长,则仅显示结果的一小部分。

所以问题是android的记录器显示长字符串的方式

感谢Gaz的帮助!

Using your code I get the same result, but now I know the problem.

The problem isnt it the code, is the Android LogCat (a Logger) where I print the resulting string. In this logger if the string is too long it only shows a short piece of the result.

So the problem was the way that the logger of android shows the longs strings

Thanks Gaz for the help!

满天都是小星星 2024-09-25 03:16:41

那么 reader.readLine 在到达流末尾之前返回 null 吗?缓冲区末尾是否包含换行符? 文档建议流的结尾不构成“行尾”:

读取一行文本。一条线被认为被任何一个终止
换行符 ('\n')、回车符 ('\r') 或回车符
紧接着是换行符。

我自己使用这种方法,它有效,但不是一个非常有效的解决方案:

public static String URLToString(URL url) throws IOException {
    InputStream in = (InputStream) url.getContent();
    int ch;

    StringBuffer b = new StringBuffer();
    while( ( ch = in.read() ) != -1 ){
        b.append( (char)ch );
    }

    return b.toString();
}

So reader.readLine is returning null before you have reached the end of the stream? Does the buffer contain a newline at the end? The docs suggest that the end of the stream does not constitute an "end of line":

Read a line of text. A line is considered to be terminated by any one
of a line feed ('\n'), a carriage return ('\r'), or a carriage return
followed immediately by a linefeed.

I use this approach myself, it works, but is not a very efficient solution:

public static String URLToString(URL url) throws IOException {
    InputStream in = (InputStream) url.getContent();
    int ch;

    StringBuffer b = new StringBuffer();
    while( ( ch = in.read() ) != -1 ){
        b.append( (char)ch );
    }

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