我正在开发一个Android应用程序,我在其中点击了一个URL并以HTML格式获取响应。当我尝试从InputStream接收数据时抛出异常。我发布我的代码和日志。请检查并帮我解决此异常。
//paramString1 = url,paramString = header, paramList = paramiters
public String a(String paramString1 , String paramString2, List paramList)
{
String str1 = null;
DefaultHttpClient localDefaultHttpClient = new DefaultHttpClient();
localDefaultHttpClient.getParams().setParameter("http.useragent", "Mozilla/5.0 (Windows NT 6.1; rv:12.0) Gecko/20120403211507 Firefox/12.0");
HttpParams localHttpParams = localDefaultHttpClient.getParams();
HttpConnectionParams.setConnectionTimeout(localHttpParams, 20000);
HttpConnectionParams.setSoTimeout(localHttpParams, 30000);
HttpPost localHttpPost = new HttpPost(paramString1);
localHttpPost.addHeader("Accept-Encoding", "gzip");
InputStream localInputStream = null;
try {
localHttpPost.setEntity(new UrlEncodedFormEntity(paramList));
localHttpPost.setHeader("Referer", paramString2);
HttpResponse localHttpResponse = localDefaultHttpClient.execute(localHttpPost);
int i = localHttpResponse.getStatusLine().getStatusCode();
Log.d(getClass().getSimpleName(), " response code " + i);
localInputStream = localHttpResponse.getEntity().getContent();
Header localHeader = localHttpResponse.getFirstHeader("Content-Encoding");
if ((localHeader != null) && (localHeader.getValue().equalsIgnoreCase("gzip"))) {
GZIPInputStream localObject = null;
localObject = new GZIPInputStream(localInputStream);
Log.d("API", "GZIP Response decoded!");
BufferedReader localBufferedReader = new BufferedReader(new InputStreamReader((InputStream)localObject, "UTF-8"));
StringBuilder localStringBuilder = new StringBuilder();
while (true) {
String str2 = localBufferedReader.readLine();
if (str2 == null)
break;
localHttpResponse.getEntity().consumeContent();
str1 = localStringBuilder.toString();
localStringBuilder.append(str2).append("\n");
}
}
}
catch (IOException localIOException) {
localHttpPost.abort();
Log.w(getClass().getSimpleName(), "Error IOException for URL " + paramString1, localIOException);
}
catch (Exception localException) {
localHttpPost.abort();
Log.w(getClass().getSimpleName(), "Error Exception for URL " + paramString1, localException);
}
return (String)str1;
}
异常堆栈跟踪:
06-19 12:34:30.513: W/RequetHelper(8105): java.net.SocketException: Socket closed
06-19 12:34:30.513: W/RequetHelper(8105): at libcore.io.Posix.recvfromBytes(Native Method)
06-19 12:34:30.513: W/RequetHelper(8105): at libcore.io.Posix.recvfrom(Posix.java:131)
06-19 12:34:30.513: W/RequetHelper(8105): at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
06-19 12:34:30.513: W/RequetHelper(8105): at libcore.io.IoBridge.recvfrom(IoBridge.java:513)
06-19 12:34:30.513: W/RequetHelper(8105): at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
06-19 12:34:30.513: W/RequetHelper(8105): at java.net.PlainSocketImpl.access$000(PlainSocketImpl.java:46)
06-19 12:34:30.513: W/RequetHelper(8105): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
06-19 12:34:30.513: W/RequetHelper(8105): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
06-19 12:34:30.513: W/RequetHelper(8105): at org.apache.http.impl.io.AbstractSessionInputBuffer.read(AbstractSessionInputBuffer.java:134)
06-19 12:34:30.513: W/RequetHelper(8105): at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:161)
06-19 12:34:30.513: W/RequetHelper(8105): at org.apache.http.impl.io.ChunkedInputStream.read(ChunkedInputStream.java:175)
06-19 12:34:30.513: W/RequetHelper(8105): at org.apache.http.conn.EofSensorInputStream.read(EofSensorInputStream.java:178)
06-19 12:34:30.513: W/RequetHelper(8105): at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:198)
06-19 12:34:30.513: W/RequetHelper(8105): at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:152)
06-19 12:34:30.513: W/RequetHelper(8105): at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:167)
06-19 12:34:30.513: W/RequetHelper(8105): at java.io.InputStreamReader.read(InputStreamReader.java:244)
06-19 12:34:30.513: W/RequetHelper(8105): at java.io.BufferedReader.fillBuf(BufferedReader.java:130)
06-19 12:34:30.513: W/RequetHelper(8105): at java.io.BufferedReader.readLine(BufferedReader.java:390)
06-19 12:34:30.513: W/RequetHelper(8105): at com.pehel.requestHelper.RequetHelper.a(RequetHelper.java:158)
06-19 12:34:30.513: W/RequetHelper(8105): at com.pehel.requestHelper.RequetHelper$GetPnrStatus.doInBackground(RequetHelper.java:71)
06-19 12:34:30.513: W/RequetHelper(8105): at com.pehel.requestHelper.RequetHelper$GetPnrStatus.doInBackground(RequetHelper.java:1)
06-19 12:34:30.513: W/RequetHelper(8105): at android.os.AsyncTask$2.call(AsyncTask.java:287)
06-19 12:34:30.513: W/RequetHelper(8105): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
06-19 12:34:30.513: W/RequetHelper(8105): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
06-19 12:34:30.513: W/RequetHelper(8105): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
06-19 12:34:30.513: W/RequetHelper(8105): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
06-19 12:34:30.513: W/RequetHelper(8105): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
06-19 12:34:30.513: W/RequetHelper(8105): at java.lang.Thread.run(Thread.java:856)