Android 中的 HTTP Post 问题
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用您的代码我得到了相同的结果,但现在我知道问题所在了。
问题不是代码,而是 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!
那么 reader.readLine 在到达流末尾之前返回 null 吗?缓冲区末尾是否包含换行符? 文档建议流的结尾不构成“行尾”:
我自己使用这种方法,它有效,但不是一个非常有效的解决方案:
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":
I use this approach myself, it works, but is not a very efficient solution: