我在使用 Android 时遇到问题:
我必须能够从生成内容的 URL 中读取内容,然后将它们放入数组中,分割读取的字符串。我使用了不同的方法,但无法读取该字符串。我能怎么做?
这是我现在使用的函数(继续出现字符串“nn va”):
private void vaia () { // funzione vaia *******************************************************
try {
HttpClient httpclient = new DefaultHttpClient(); // Create HTTP Client
HttpGet httpget = new HttpGet(URL); // Set the action you want to do
HttpResponse response = httpclient.execute(httpget); // Executeit
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent(); // Create an InputStream with the response
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) // Read line by line
sb.append(line + "\n");
String resString = sb.toString(); // Result is here
Log.i("string letta", resString);
is.close(); // Close the stream
}
catch (Exception e) {
Log.i("Risultato eccezione","nn va");
//e.printStackTrace();
}
}
此时的 URL:http://www.innovacem.com/public/glace/leggiFoto.php http://www.innovacem.com/public/glace/leggiFoto.php(应写“vuoto”)
谢谢 :)
使用异步任务:
private class ReadTask extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... params)
{
getResponseFromUrl(params[0]);
return "success";
}
}
要调用它,请使用:
new ReadTask().execute(<your url>);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)