我正在做一个连接到我的PC服务器的客户端应用程序,但在运行时我的应用程序崩溃了。我认为问题在于我的androidmanifest.xml中的互联网权限
- > ANDROID APP代码:
public class Main extends Activity {
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b = (Button)findViewById(R.id.connect); //reference to the send button
b.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
try {
Socket client = new Socket("192.168.1.2", 4444); //connect to server
client.close(); //closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
- >的AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.client"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.client.Main"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这里有我的LogCat:
05-29 13:32:22.850: E/AndroidRuntime(18707): FATAL EXCEPTION: main
05-29 13:32:22.850: E/AndroidRuntime(18707): android.os.NetworkOnMainThreadException
05-29 13:32:22.850: E/AndroidRuntime(18707): at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1133)
05-29 13:32:22.850: E/AndroidRuntime(18707): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:84)
05-29 13:32:22.850: E/AndroidRuntime(18707): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-29 13:32:22.850: E/AndroidRuntime(18707): at libcore.io.IoBridge.connect(IoBridge.java:112)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.net.Socket.startupSocket(Socket.java:566)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.net.Socket.tryAllAddresses(Socket.java:127)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.net.Socket.<init>(Socket.java:177)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.net.Socket.<init>(Socket.java:149)
05-29 13:32:22.850: E/AndroidRuntime(18707): at com.example.client.Main$1.onClick(Main.java:39)
05-29 13:32:22.850: E/AndroidRuntime(18707): at android.view.View.performClick(View.java:4240)
05-29 13:32:22.850: E/AndroidRuntime(18707): at android.view.View$PerformClick.run(View.java:17721)
05-29 13:32:22.850: E/AndroidRuntime(18707): at android.os.Handler.handleCallback(Handler.java:730)
05-29 13:32:22.850: E/AndroidRuntime(18707): at android.os.Handler.dispatchMessage(Handler.java:92)
05-29 13:32:22.850: E/AndroidRuntime(18707): at android.os.Looper.loop(Looper.java:137)
05-29 13:32:22.850: E/AndroidRuntime(18707): at android.app.ActivityThread.main(ActivityThread.java:5103)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.lang.reflect.Method.invokeNative(Native Method)
05-29 13:32:22.850: E/AndroidRuntime(18707): at java.lang.reflect.Method.invoke(Method.java:525)
05-29 13:32:22.850: E/AndroidRuntime(18707): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
05-29 13:32:22.850: E/AndroidRuntime(18707): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-29 13:32:22.850: E/AndroidRuntime(18707): at dalvik.system.NativeStart.main(Native Method)
请帮帮我
答案 0 :(得分:2)
移动
Socket client = new Socket("192.168.1.2", 4444); //connect to server
client.close(); //closing the connection
加入AsyncTask
或Thread
如果您对Network
执行UIThread
次操作,则会抛出NetworkOnMainThreadException
。
答案 1 :(得分:0)
NetworkOnMainThreadException
。所以建议在单独的线程或AsyncTask
中进行。做类似下面的事情,
b.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View arg0) {
new myTask().execute();
}
});
private class myTask extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
Socket client = new Socket("192.168.1.2", 4444); //connect to server
client.close(); //closing the connection
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
}