如何从网页(Android)获取信息?
我使Android应用程序必须从页面http://example.com/get.php?id=1接收数据。为此,我用下面的方法:如何从网页(Android)获取信息?
private String getData()
{
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext(); // !!! application stops here
HttpGet httpGet = new HttpGet("http://example.com/get.php?id=1");
HttpResponse response = null;
try {
response = httpClient.execute(httpGet, localContext);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String result = "";
BufferedReader reader = null;
try {
reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String line = null;
try {
while ((line = reader.readLine()) != null){
result += line + "\n";
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Now you have the whole HTML loaded on the result variable
return result;
}
我还添加了权限的AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.repetitor_ent_free"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.INTERNET" />
的问题是,当我在设备上运行该应用程序我得到的错误: 的意外停止应用MyApp(进程com.android.myapp)。请再试一次。
你有android.os.NetworkOnMainThreadException
,如果你想更好的解决办法尝试使用AsyncTask。有关线索的更多信息,请参阅'processes and threads'(请仔细阅读本指南以避免将来出现此问题)。
例如,你可以尝试以下解决方法之一:
1)使用罗马的答案,并添加如下内容:
final StringBuilder sb = ... ;
final TextView tv = ... ;
tv.post(new Runnable() {
public void run() {
tv.setText(sb.toString());
}
});
或使用活动方法:
runOnUiThread(new Runnable() {
public void run() {
final StringBuilder sb = ... ;
final TextView tv = ... ;
tv.setText(sb.toString());
}
});
2 )使用AsyncTask:
new AsyncTask<Void, Void, Void>() {
protected Void doInBackground(Void... none) {
//
// your code
//
return null;
}
protected void onProgressUpdate(Void... none) {}
protected void onPostExecute(Void none) {
// use this to set the text
final StringBuilder sb = ... ;
final TextView tv = ... ;
tv.setText(sb.toString());
}
}.execute();
尝试使用此:
new Thread(){
@Owerride
public void run(){
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
InputStreamReader streamReader = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(streamReader);
StringBuilder sb = new StringBuilder();
String line = null;
while((line = br.readLine()) != null) {
sb.append(line);sb.append("\n");
}
}
}.start();
你可以试试下面的代码:
HttpURLConnection conn = null;
try {
URL url = new URL("http://example.com/get.php?id=1");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-length", "0");
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.connect();
InputStreamReader streamReader = new InputStreamReader(conn.getInputStream());
//Do what you need with the data here
} catch (Exception e) {
// Handle your exceptions
} finally {
if (conn != null) {
conn.disconnect();
conn = null;
}
}
}
同样的问题,但会触发异常。 – abg 2012-01-18 06:39:42
啊,没有发现pastebin链接。你在主要活动中运行这个网络代码吗?它需要驻留在另一个线程中,以避免由@ m039建议的阻塞或Aysync任务 – 2012-01-18 14:01:19
尝试logcat它。如果网络在主线程中使用,我得到错误(在sdk api> 5),也许是。 – m039 2012-01-17 08:29:29
发布Logcat输出以简化调试。 – Thommy 2012-01-17 08:29:44
检查logcat。你会发现一个例外,然后在这里发布。 – 2012-01-17 08:29:56