【发布时间】:2011-07-05 15:31:09
【问题描述】:
我遇到了一个非常奇怪的问题,我不知道从哪里开始。
我正在向服务器发送一个 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 中运行。
亲切的问候, 水母
【问题讨论】:
-
您是否通过
Log.<?>()将字符串写入日志?日志有一个最大输出长度,所以检查最好将字符串分成不超过 500 个字符左右的片段,以确保所有内容都被打印出来。 -
您可能会发现 EntityUtils 在将实体作为字符串检索时很有用,从而使您无需使用最后 10 行左右。
-
@LeffelMania:不,我停止了调试选项并检查了那里的字符串长度。 @David Caunt:谢谢!
标签: java android string httprequest android-3.0-honeycomb