Android httpGet 上的 URL 重写输入流读取器
如果我有这段代码并且 url 是 "http://www.example.com/someFile.txt" 一切正常。但是如果我输入 "http://www.example.com/export/something" (在服务器上的 URL rewrite/.htaccess 的帮助下),桌面浏览器中再次在 Android 中输出相同的结果我收到错误 Error w/file: End of input at character 0 of
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
String result = "";
HttpGet httpGet = new HttpGet("http://www.example.com/export/notWorking");
HttpResponse response = null;
try {
response = httpClient.execute(httpGet, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = null;
while ((line = reader.readLine()) != null){
result += line;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
急需解决此问题吗?
编辑:使用用户代理提供帮助
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.71");
没有帮助。
If I have this piece of code and url is "http://www.example.com/someFile.txt" everything works just fine. But if I put "http://www.example.com/export/something" (with help of URL rewrite/.htaccess on the server) which in desktop browser again outputs the same result in Android I get error Error w/file: End of input at character 0 of
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
String result = "";
HttpGet httpGet = new HttpGet("http://www.example.com/export/notWorking");
HttpResponse response = null;
try {
response = httpClient.execute(httpGet, localContext);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = null;
while ((line = reader.readLine()) != null){
result += line;
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Hot to solve this problem?
EDIT: Help with user agent
HttpParams params = new BasicHttpParams();
params.setParameter(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.24 (KHTML, like Gecko) Chrome/11.0.696.71");
doesn't help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案 - 非常琐碎我必须说 - 我删除了模拟器,创建了一个新模拟器,然后一切都工作正常......所以将来 - 仅在设备上进行测试本身:)
Solution - pretty trivial I must say - I deleted the emulator, created a new one and then everything is working just fine...so in future - test only on the device itself :)