我正在執行以下代碼並始終在urlConnection.getInputStream()中獲取EOFException。我知道網絡服務器沒有返回正確的標題,但是如果我在網絡瀏覽器中使用相同的網址,我會收到預期的內容。urlConnection.getInputStream()拋出EOFException
URL url1;
try
{
url1 = new URL("http://63.255.173.242/get_public_tbl.cgi?A=1");
HttpURLConnection urlConnection = (HttpURLConnection) url1.openConnection();
try
{
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
readStream(in);
}
catch(Exception e)
{
e.printStackTrace();
}
finally
{
urlConnection.disconnect();
}
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}
任何想法?
編輯1:
我以爲我是在一個可能的解決方案。我發現使用Android 2.3我可以通過調用getHeaderFields()來讀取響應(儘管沒有標頭),但是如果我使用Android 4.1,getHeaderFields()將返回null。有任何想法嗎?
url1 = new URL("http://63.255.173.242/get_public_tbl.cgi?A=1");
URLConnection urlConnection = url1.openConnection();
Map<String, List<String>> fields = urlConnection.getHeaderFields();
你能發佈異常堆棧跟蹤嗎? – randominstanceOfLivingThing
異常堆棧跟蹤爲空 –
另請參閱:http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests – 2012-09-11 05:52:16