【问题标题】:Android Socket Error when run on AVD在 AVD 上运行时出现 Android Socket 错误
【发布时间】:2012-03-08 07:24:43
【问题描述】:

我在以下代码的套接字中遇到问题,当我在 AVD 上运行程序时,它导致程序停止工作(不幸的是,您的 -app- 已停止),我在 Windows 7 上使用 Android 4.0 平台 .. 我试图将套接字部分移动到按钮单击,所以当我单击按钮时程序停止工作,所以在套接字定义中出现错误。 (Socket 插座;)

public class ServerClient extends Activity {
// declaration of button, textView
private Button bt;
   private TextView tv;
  //port number
   private static final int REDIRECTED_SERVERPORT = 5000;
  //ip address
   private String serverIpAddress = "10.0.2.2";
   @Override
   public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      bt = (Button) findViewById(R.id.myButton);
      tv = (TextView) findViewById(R.id.myTextView);

         // on click on the button the socket will be created
      bt.setOnClickListener(new OnClickListener() {

           Socket socket; //this line cause the app to stop working

         public void onClick(View v) {

          try {
             InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
             socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);

          } catch (UnknownHostException e1) {
             e1.printStackTrace();
          } catch (IOException e1) {
             e1.printStackTrace();
          }
            try {
               EditText et = (EditText) findViewById(R.id.EditText01);
               String str = et.getText().toString();
               PrintWriter out = new PrintWriter(new BufferedWriter(
                     new OutputStreamWriter(socket.getOutputStream())),
                     true);
               out.println(str);
               Log.d("Client", "Client sent message");

            } catch (UnknownHostException e) {
               tv.setText("Error1");
               e.printStackTrace();
            } catch (IOException e) {
               tv.setText("Error2");
               e.printStackTrace();
            } catch (Exception e) {
               tv.setText("Error3");
               e.printStackTrace();
            }
         }
      });
   }
}

【问题讨论】:

    标签: java android android-layout sdk


    【解决方案1】:

    您好,我遇到了同样的问题(Windows 7、Android 4.03)。我已经使用 android 2.33(API 级别 10)和带有 android 2.33 内核的模拟器解决了这个问题。

    【讨论】:

      【解决方案2】:

      您应该通过adb 中的此命令将端口从您的 PC 转发到 AVD:

      adb forward tcp:YOUR_PORT_NUM tcp:YOUR_PORT_NUM
      

      【讨论】:

        【解决方案3】:

        您的 AndroidManifest.xml 中是否设置了互联网权限?

        你需要这条线:

        <uses-permission android:name="android.permission.INTERNET" />
        

        【讨论】:

        • 是的,我在AndroidManifest.xml中添加了这个权限,但是点击按钮时仍然出现错误..
        • 那我建议你使用Eclipse中的LogCat工具看看发生了什么。您通常会在那里收到一条错误消息以及一个堆栈跟踪,您可以在其中看到哪里出了问题。
        • 使用 LogCat 时出现以下错误... 03-07 20:52:00.056: E/AndroidRuntime(571): java.lang.RuntimeException: Unable to start activity ComponentInfo{com .app.ServerClient/com.app.ServerClient.ServerClient}:android.os.NetworkOnMainThreadException
        • 好吧,看来你是不允许在 Android 3.0 及更高版本的主线程上进行网络调用的,所以你必须启动一个新线程来发出请求,或者你可以使用类似 AsyncTask ( developer.android.com/reference/android/os/AsyncTask.html)
        • 我一直在早期的 SDK (Android 2.3) 中尝试此代码,但问题再次发生,因此它不依赖于 Android 平台。 AsyncTask 是否可以将套接字从 android 发送到 PC?如果这可能是他们的任何教程,请 ??
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-08-01
        相关资源
        最近更新 更多