我正在执行以下代码,并始终在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 :(得分:2)
由于您的服务器没有使用正确的HTTP,您无法使用URL.openConnection,但您仍然可以使用到63.255.173.242端口80的普通套接字连接访问它并发送字符串:
GET /get_public_tbl.cgi?A=1 HTTP / 1.0 \ r \ n \ r \ n
然后读取结果流。
答案 1 :(得分: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>
...
答案 2 :(得分: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;
}
}