我遇到了一個非常奇怪的問題,我沒有絲毫的想法從哪裏開始。作爲http響應中斷的字符串? - Java,Platform 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。
親切的問候, 水母
你通過'註銷到日誌寫入字符串。 >()'?日誌具有最大輸出長度,因此檢查最好將字符串分成不大於500個左右的字符,以確保所有內容都被打印出來。 – LeffelMania
您可能會發現[EntityUtils](http://developer.android。com/reference/org/apache/http/util/EntityUtils.html)用於以字符串形式檢索實體,從而不需要最後10行。 –
@LeffelMania:不,我停止了調試選項,並在那裏檢查了字符串長度。 @David Caunt:謝謝! – jellyfish