1

我正在执行以下代码,并且总是在 urlConnection.getInputStream() 中获得 EOFException。我知道网络服务器没有返回正确的标头,但是如果我在网络浏览器中使用相同的 url,我会得到预期的内容。

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();
4

3 回答 3

2

由于您的服务器没有使用正确的 HTTP,因此您不能使用 URL.openConnection,但您仍然可以使用到 63.255.173.242 端口 80 的普通套接字连接并发送字符串来访问它:

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

然后读取结果流。

于 2012-11-04T01:53:49.917 回答
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>
...
于 2012-11-04T01:44:34.670 回答
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);
}

我的获取 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;
    }
}
于 2012-10-30T10:54:52.847 回答