http 响应被切断时出现的字符串? - Java,平台Android 3.0
我遇到了一个非常奇怪的问题,我完全不知道从哪里开始。
我正在向服务器发送一个 http 请求并获得一个简单的字符串作为响应。这在我的智能手机应用程序中运行良好;它甚至在我的浏览器中运行良好。然而,虽然我以为我只是复制并粘贴智能手机代码,但它不再适用于我的平板电脑 (Android 3.0.1) 版本的应用程序。
我已经检查了调试器,旧版本得到了一个长度为 2958 个字符的字符串。不过,新版本只获取长度为 1334 的字符串。我记录了新版本的 URL,将其放入浏览器中,再次得到一串 2985 个字符的字符串。
我真的找不到我的代码有任何重大差异(请参见下文)。另外,我不敢相信 Android 中发生了一些限制字符串长度的变化?!
那么有人有想法吗?
原始智能手机代码:
if (CheckInternet())
{
myURL = new URL(params[0]);
httpClient = AndroidHttpClient.newInstance("android");
if (rtype == RequestType.GET)
{
httpRequest = new HttpGet(myURL.toExternalForm());
}
else
{
httpRequest = new HttpPost(myURL.toExternalForm());
HttpEntity myEntity = new StringEntity(message, "UTF-8");
((HttpPost) httpRequest).setEntity(myEntity);
}
HttpParams httpParams = new BasicHttpParams();
HttpProtocolParams.setHttpElementCharset(httpParams, "UTF-8");
HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
HttpConnectionParams.setSoTimeout(httpParams, timeout);
httpRequest.setParams(httpParams);
response = httpClient.execute(httpRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 300 || statusCode >= 305)
{
errorMessage = getStatusCodeMessage(statusCode, act);
}
else
{
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream instream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line);
result = sb.toString();
}
}
}
新平板电脑版本中的代码:
if (CheckInternet())
{
if (isCancelled()) return null; //that's for an AsyncTask
URL myURL = new URL(params[0]);
httpClient = AndroidHttpClient.newInstance("android");
if (isCancelled()) return null;
if (params[1] == null)
{
httpRequest = new HttpGet(myURL.toExternalForm());
}
else
{
httpRequest = new HttpPost(myURL.toExternalForm());
HttpEntity myEntity = new StringEntity(params[1], "UTF-8");
((HttpPost) httpRequest).setEntity(myEntity);
}
httpRequest.setParams(httpParams);
if (isCancelled()) return null;
HttpResponse response = httpClient.execute(httpRequest);
httpClient.close();
if (isCancelled()) return null;
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 300 || statusCode >= 305)
{
error = HttpHelper.getStatusCodeMessage(statusCode, getActivity());
}
else
{
if (isCancelled()) return null;
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream instream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line);
String test = sb.toString(); //that was for debugging the string
return test;
}
}
}
两个请求都在 AsyncTask 中运行。
亲切的问候, 海蜇
I have run into a very strange problem and I don't have the slightest idea where to start.
I am sending a http request to a server and get a simple string as response. This worked fine in my smartphone app; it even works fine in my browser. However, while I thought I'd simply copy-and-pasted the smartphone code, it doesn't work for my tablet (Android 3.0.1) version of the app anymore.
I have checked with the debugger and the old version gets a string with a length of 2958 characters. The new version only gets a string of the length 1334, though. I've logged the URL of the new version, put it into my browser and got a string of 2985 characters again.
I really can't find any major difference in my code (please see below). Also, I can't believe there was some change in Android that would limit string length?!
So does anybody have an idea?
Original Smartphone code:
if (CheckInternet())
{
myURL = new URL(params[0]);
httpClient = AndroidHttpClient.newInstance("android");
if (rtype == RequestType.GET)
{
httpRequest = new HttpGet(myURL.toExternalForm());
}
else
{
httpRequest = new HttpPost(myURL.toExternalForm());
HttpEntity myEntity = new StringEntity(message, "UTF-8");
((HttpPost) httpRequest).setEntity(myEntity);
}
HttpParams httpParams = new BasicHttpParams();
HttpProtocolParams.setHttpElementCharset(httpParams, "UTF-8");
HttpProtocolParams.setContentCharset(httpParams, "UTF-8");
HttpConnectionParams.setConnectionTimeout(httpParams, timeout);
HttpConnectionParams.setSoTimeout(httpParams, timeout);
httpRequest.setParams(httpParams);
response = httpClient.execute(httpRequest);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 300 || statusCode >= 305)
{
errorMessage = getStatusCodeMessage(statusCode, act);
}
else
{
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream instream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line);
result = sb.toString();
}
}
}
Code in the new Tablet version:
if (CheckInternet())
{
if (isCancelled()) return null; //that's for an AsyncTask
URL myURL = new URL(params[0]);
httpClient = AndroidHttpClient.newInstance("android");
if (isCancelled()) return null;
if (params[1] == null)
{
httpRequest = new HttpGet(myURL.toExternalForm());
}
else
{
httpRequest = new HttpPost(myURL.toExternalForm());
HttpEntity myEntity = new StringEntity(params[1], "UTF-8");
((HttpPost) httpRequest).setEntity(myEntity);
}
httpRequest.setParams(httpParams);
if (isCancelled()) return null;
HttpResponse response = httpClient.execute(httpRequest);
httpClient.close();
if (isCancelled()) return null;
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == 300 || statusCode >= 305)
{
error = HttpHelper.getStatusCodeMessage(statusCode, getActivity());
}
else
{
if (isCancelled()) return null;
HttpEntity entity = response.getEntity();
if (entity != null)
{
InputStream instream = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(instream, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
sb.append(line);
String test = sb.toString(); //that was for debugging the string
return test;
}
}
}
Both requests are running in an AsyncTask.
Kind regards,
jellyfish
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定这就是原因,但它看起来很可疑 -
我会等到消耗 HttpEntity 之后再关闭客户端。
I'm not sure this is the cause, but it looks suspicious -
I'd wait until after consuming the HttpEntity before closing the client.
我自己也是新手,所以如果我听起来像个白痴,请原谅我。
我在这篇文章中发现了一个有趣的点:
http:// android-developers.blogspot.com/2010/12/new-gingerbread-api-strictmode.html
它说“你不应该在主线程上执行网络请求。事实上,在即将到来的Honeycomb 版本 我们已将主线程上的网络请求设置为致命错误,除非您的应用面向 Honeycomb 之前的 API 版本”
您是否正在运行请求在单独的 ASyncThread 中?我无法通过查看代码来判断。我自己做这件事真是太开心了。如果您有任何想法,请告诉我,因为我很想看看您是如何做到的。
真挚地,
安德鲁
I am new to this myself, so please forgive me if I sound like an idiot.
I found an interesting point in this article:
http://android-developers.blogspot.com/2010/12/new-gingerbread-api-strictmode.html
It said "you should never do network requests on your main thread. In fact, in the upcoming Honeycomb release we’ve made network requests on the main thread a fatal error, unless your app is targeting an API version before Honeycomb"
Are you running your request in a separate ASyncThread? I cant tell by looking at the code. I am having a doozie of a time doing this myself. Please let me know if you come up with anything, as I would LOVE to see how you did it.
Sincerely,
Andrew