2012-09-11 41 views
1

我正在執行以下代碼並始終在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(); 
+0

你能發佈異常堆棧跟蹤嗎? – randominstanceOfLivingThing

+0

異常堆棧跟蹤爲空 –

+0

另請參閱:http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests – 2012-09-11 05:52:16

回答

2

當你的服務器是不是說適當的HTTP不能使用URL.openConnection,但你仍然可以使用普通插座連接到63.255.173.242端口80訪問和發送的字符串:

GET/get_public_tbl.cgi?A = 1 HTTP/1.0 \ r \ n \ r \ n

然後讀取結果流。

+0

這就是我所需要的。謝謝。 –

0

要讀這樣的迴應:

BufferedReader in = new BufferedReader(new InputStreamReader(
    response.getEntity().getContent(), "UTF-8")); 

char[] buf = new char[1000]; 
int l = 0; 
while (l >= 0) 
{ 
    builder.append(buf, 0, l); 
    l = in.read(buf); 
} 

示例代碼爲我GET XML代碼類GetXMLTask.java

public class GetXMLTask 
{ 
    public ArrayList<JSONObject> getOutputFromUrl(String url) 
    { 
     ArrayList<JSONObject> output = new ArrayList<JSONObject>(); 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     HttpResponse response; 
     StringBuilder builder= new StringBuilder(); 
     JSONObject myjson ; 
     JSONArray the_json_array; 
     try 
     { 
      response = httpClient.execute(httpPost); 
      BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8")); 
      char[] buf = new char[1000]; 
      int l = 0; 
       while (l >= 0) 
       { 
        builder.append(buf, 0, l); 
        l = in.read(buf); 
       } 
      myjson = new JSONObject("{child:"+builder.toString()+"}"); 
      the_json_array = myjson.getJSONArray("child"); 
      //System.out.println("json array length()===>>>"+the_json_array.length());//shows no of child 
      for (int i = 0; i < the_json_array.length(); i++) 
      { 
       JSONObject another_json_object = the_json_array.getJSONObject(i);//the_json_array.getJSONObject(i); 
       //System.out.println("json object length()"+another_json_object.length()); 
        output.add(another_json_object); 
      } 
     } catch (ClientProtocolException e) { 
      System.out.println("ClientProtocolException :"+e); 
      e.printStackTrace(); 
     } catch (IOException e) { 
      System.out.println("IOException :"+e); 
      e.printStackTrace(); 
     } catch (JSONException e) { 
      System.out.println("JSONException hussain :"+e); 
      e.printStackTrace(); 
     } 
     //System.out.println(output); 
     //System.out.println(output.toString()); 
     return output; 
    } 
} 
+1

你是如何得到迴應的? –

+0

將httppostclient用於Web服務 –

+0

您有任何示例代碼嗎? –

1

你的服務器是不與任何HTTP頭響應,請嘗試使用getErrorStream

emil$ curl -v http://63.255.173.242/get_public_tbl.cgi?A=1 
* About to connect() to 63.255.173.242 port 80 (#0) 
* Trying 63.255.173.242... connected 
* Connected to 63.255.173.242 (63.255.173.242) port 80 (#0) 
> GET /get_public_tbl.cgi?A=1 HTTP/1.1 
> User-Agent: curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5 
> Host: 63.255.173.242 
> Accept: */* 
> 
<?xml version="1.0" encoding="UTF-8"?> 
<record table="public" stationID="1" timestamp="19:49:40 11/03/2012" record_number="18372"> 
<value> 
<name>SaveSite</name> 
<data>0.00</data> 
</value> 
<value> 
<name>Latitude</name> 
<data>-111.00</data> 
... 
+0

正是。問題是服務器無法更改。你有沒有任何想法可以在沒有發回頭文件時讀取響應?在Android 2.3上,我可以使用urlConnection.getHeaderFields()獲取實際的響應,但這在Android 4.0及更高版本中不起作用。 –

+0

嘗試使用'getErrorStream' –

相關問題