1

I am executing the following code and always get an EOFException in urlConnection.getInputStream(). I know that the webserver is not returning proper headers, but if I use the same url in a web browser I get the expected content back.

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

Any ideas?

Edit 1:

I thought I was on to a possible solution. I found that using Android 2.3 I can read the response by calling getHeaderFields() (even though there is no header), but if I use Android 4.1 getHeaderFields() returns null. Any ideas?

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();
Kevin Westwood
  • 7,253
  • 8
  • 35
  • 50

3 Answers3

2

As your server is not talking proper HTTP you cannot use URL.openConnection, but you can still access it using a plain socket connection to 63.255.173.242 port 80 and sending the strings:

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

and then reading the resulting stream.

thedayofcondor
  • 3,737
  • 1
  • 17
  • 27
1

Your server is not responding with any HTTP headers, try using 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>
...
Emil Davtyan
  • 12,725
  • 5
  • 40
  • 62
  • Exactly. The problem is the server cannot be changed. Do you have any ideas of how I can read the response when it is not sending back headers? On Android 2.3, I can use urlConnection.getHeaderFields() to get the actual response, but this does not work in Android 4.0 and above. – Kevin Westwood Nov 04 '12 at 01:48
0

Try reading the response like this:

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);
}

sample code for my get xml code class 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;
    }
}
Eric Leschinski
  • 123,728
  • 82
  • 382
  • 321