我想从这个URL http://digitalcollections.tcd.ie/home/getMeta.php?pid=MS4418_021得到json对象作为字符串。它不起作用我在downloadUrl函数后出现错误。Android的HttpUrlConnection网址无法在模拟器上工作
java.io.IOException: unexpected end of stream on Connection{digitalcollections.tcd.ie:80, [email protected] hostAddress=134.226.115.12 cipherSuite=none protocol=http/1.1} (recycle count=0)
它虽然这个androidhive网址http://api.androidhive.info/volley/person_object.json工作。 我是新来httpconnection下面是我的下载url功能。错误似乎显示在这一行HttpURLConnection conn =(HttpURLConnection)url.openConnection();在该行之后conn.getInputStream调试器()表示IO异常和原因java.io.EOFException的:\ n未找到:大小= 0含量= ...
// Given a string representation of a URL, sets up a connection and gets
// an input stream.
private InputStream downloadUrl(String urlString) throws IOException {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(20000 /* milliseconds */);
conn.setConnectTimeout(30000 /* milliseconds */);
conn.setRequestMethod("GET");
//conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
return stream;
}
其它功能。
// Uses AsyncTask to create a task away from the main UI thread. This task takes a
// URL string and uses it to create an HttpUrlConnection. Once the connection
// has been established, the AsyncTask downloads the contents of the webpage as
// an InputStream. Finally, the InputStream is converted into a string, which is
// displayed in the UI by the AsyncTask's onPostExecute method.
private class DownloadXMLTask extends AsyncTask<String, Void, List<Entry>> {
private String urlFront = "";
@Override
protected List<Entry> doInBackground(String... urls) {
// params comes from the execute() call: params[0] is the url.
try {
return loadJsonFromNetwork(urls[0]);
} catch (IOException e) {
Log.d(TAG, "Unable to retrieve web page. URL may be invalid.");
return null;
} catch (JSONException e) {
Log.d(TAG, "XMLPULLPARSER ERROR IN download json task function");
return null;
}
}
}
// onPostExecute displays the results of the AsyncTask.
@Override
protected void onPostExecute(List<Entry> result) {
//post execution stuff
}
}
加载json和解析器,解析器可能无法正常工作尚未测试它。
private List<Entry> loadJsonFromNetwork(String urlString) throws IOException, JSONException {
InputStream stream = null;
int len = 20000; //max amount of characters to display in string
List<Entry> entries = new ArrayList<Entry>();
try {
stream = downloadUrl(urlString); //IOException
String jsonStr = readit(stream,len);
if(jsonStr.equals(null)){
Log.d(TAG, "ERROR json string returned null");
return entries;
}
JSONObject jsonObj = new JSONObject(jsonStr);
//Not sure if the json parser works yet haven't got that far
// Getting JSON Array node
identifier = jsonObj.getJSONArray("identifier");
// looping through All Contacts
for (int i = 0; i < identifier.length(); i++) {
JSONObject c = identifier.getJSONObject(i);
String id = c.getString("type");
if(id.equals("DRIS_FOLDER")) {
String folder = c.getString("$");
entries.add(new Entry(null,null,null,folder));
}
}
// Makes sure that the InputStream is closed after the app is
// finished using it.
//This is where IOexception is called and stream is null
} catch (IOException e) {
Log.d(TAG, "Unable to retrieve json web page. URL may be invalid."+ e.toString());
return entries;
}
finally {
if (stream != null) {
stream.close();
}
}
return entries;
}
我在Nexus_5_API_23模拟器上运行这个。
在此先感谢。
UPDATE:
不上Nexus_5_API_23模拟器工作吗?虽然它适用于三星GT-ST7500外接手机。希望它为模拟器工作。
你试过Nexus_5_API_23模拟器吗?它似乎并没有为此工作。虽然我在三星GT-ST7500外置手机上试过了你的代码,但它的工作原理,但我的代码也是如此。是否有一个原因,它不会在模拟器上工作? – Coder
我使用了Nexus 4 API 19仿真器。不过,我可以尝试一下。 –
@Coder刚刚尝试过Nexus 5 API 23仿真器,它的工作原理是一样的。 –