我有一个需要向网页发出get请求的android应用程序。 我的代码编译和运行没有任何问题,但尽管我想打电话我的程序停止响应
这是我的代码
public void Check_Login()
{
HttpURLConnection connection = null;
try {
String link = "http://mobirep.co.za/server_scripts/check_login/check_login.php?username=andre&password=aaa";
URL url = new URL(link);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
InputStream is = connection.getInputStream();
int buffer;
while((buffer = is.read()) != -1)
{
System.out.print(buffer);
TextView tx = (TextView)findViewById(R.id.lblName);
tx.setText(buffer);
}
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
} finally {
if(null != connection) { connection.disconnect(); }
}
这是我的日志:
05-13 08:13:59.607: I/Choreographer(891): Skipped 115 frames! The application may be doing too much work on its main thread.
05-13 08:13:59.627: D/gralloc_goldfish(891): Emulator without GPU emulation detected.
05-13 08:14:00.048: I/Choreographer(891): Skipped 50 frames! The application may be doing too much work on its main thread.
05-13 08:14:15.078: D/AndroidRuntime(891): Shutting down VM
05-13 08:14:15.078: W/dalvikvm(891): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
05-13 08:14:15.138: E/AndroidRuntime(891): FATAL EXCEPTION: main
05-13 08:14:15.138: E/AndroidRuntime(891): android.os.NetworkOnMainThreadException
05-13 08:14:15.138: E/AndroidRuntime(891): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
05-13 08:14:15.138: E/AndroidRuntime(891): at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
05-13 08:14:15.138: E/AndroidRuntime(891): at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
05-13 08:14:15.138: E/AndroidRuntime(891): at java.net.InetAddress.getAllByName(InetAddress.java:214)
05-13 08:14:15.138: E/AndroidRuntime(891): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
05-13 08:14:15.138: E/AndroidRuntime(891): at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
05-13 08:14:15.138: E/AndroidRuntime(891): at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:340)
05-13 08:14:15.138: E/AndroidRuntime(891): at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
05-13 08:14:15.138: E/AndroidRuntime(891): at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
05-13 08:14:15.138: E/AndroidRuntime(891): at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:316)
05-13 08:14:15.138: E/AndroidRuntime(891): at libcore.net.http.HttpEngine.connect(HttpEngine.java:311)
05-13 08:14:15.138: E/AndroidRuntime(891): at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290)
05-13 08:14:15.138: E/AndroidRuntime(891): at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240)
05-13 08:14:15.138: E/AndroidRuntime(891): at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81)
05-13 08:14:15.138: E/AndroidRuntime(891): at com.appmobirep.MainActivity.Check_Login(MainActivity.java:70)
05-13 08:14:15.138: E/AndroidRuntime(891): at com.appmobirep.MainActivity$1.onClick(MainActivity.java:35)
05-13 08:14:15.138: E/AndroidRuntime(891): at android.view.View.performClick(View.java:4204)
05-13 08:14:15.138: E/AndroidRuntime(891): at android.view.View$PerformClick.run(View.java:17355)
05-13 08:14:15.138: E/AndroidRuntime(891): at android.os.Handler.handleCallback(Handler.java:725)
05-13 08:14:15.138: E/AndroidRuntime(891): at android.os.Handler.dispatchMessage(Handler.java:92)
05-13 08:14:15.138: E/AndroidRuntime(891): at android.os.Looper.loop(Looper.java:137)
05-13 08:14:15.138: E/AndroidRuntime(891): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-13 08:14:15.138: E/AndroidRuntime(891): at java.lang.reflect.Method.invokeNative(Native Method)
05-13 08:14:15.138: E/AndroidRuntime(891): at java.lang.reflect.Method.invoke(Method.java:511)
05-13 08:14:15.138: E/AndroidRuntime(891): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-13 08:14:15.138: E/AndroidRuntime(891): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-13 08:14:15.138: E/AndroidRuntime(891): at dalvik.system.NativeStart.main(Native Method)
05-13 08:19:15.257: I/Process(891): Sending signal. PID: 891 SIG: 9
请你帮我解决这个错误=)
答案 0 :(得分:0)
问题在这一行详细说明:
05-13 08:14:15.138: E/AndroidRuntime(891): android.os.NetworkOnMainThreadException
&#34;网络主线程异常&#34;。这意味着,您无法在UI线程中进行任何网络调用,因为如果这样做,UI将停止更新。
您需要做的是使用某种类型的线程方法。 Android中常见的是AsyncTask:http://developer.android.com/reference/android/os/AsyncTask.html