【问题标题】:connecting to socket using asynctask android使用 asynctask android 连接到套接字
【发布时间】:2014-04-12 20:53:55
【问题描述】:

我正在尝试制作一个启动画面,而我正在尝试通过套接字连接服务器。

我对 Android 业务还很陌生,所以我有可能做错了,反正它不起作用。

public class SplashScreen extends Activity {
private Socket socket; // the socket
private final int port = 1500; // the socket port - 1500
private boolean connectivity=false; // if connected to the socket or not
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    ConnectToServer cts= new ConnectToServer();
    cts.execute();}


private class ConnectToServer extends AsyncTask<Void, Void, Void> {

 @Override
 protected Void doInBackground(Void... params) {
     try {
         Log.d("socket", "trying to connect");
         socket = new Socket("10.0.0.11", port);
     } catch (UnknownHostException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
     } catch (IOException e1) {
         // TODO Auto-generated catch block
         e1.printStackTrace();
     }
     if (socket.isConnected())
         connectivity=true;
     return null;
 }}


 protected void onPostExecute() {
// execution of result of Long time consuming operation
 Intent i = new Intent(SplashScreen.this, MainActivity.class);
 i.putExtra("connectivity", connectivity);
 startActivity(i);
 finish();}

【问题讨论】:

  • 你是在模拟器还是真机上试试这个?因为在鸸鹋上它不会工作
  • 当然是在真实设备上
  • 恐怕 10.0.0.11(这是一个 LAN IP)对于 android 来说是无法访问的,因为它不是你的 LAN 的一部分。 Android 有自己的虚拟 LAN 和自己的 127.0.0.1/localhost,因此对 10.x.x.x IP 的请求不会离开 AOS。这样的数据包会在 AOS 内死掉
  • 在我决定使用 asynctask 之前,我使用一个简单的线程以不同的方式编写了这段代码。该 ip 地址工作得很好,我能够连接我的服务器,所以这不是问题。问题是 - 使用这种代码(AsyncTask)我无法连接到服务器,这段代码基本上没有任何反应
  • 和?它对你有用吗?如果是这样,只需尝试回退到简单的线程时间,它比 AsyncTask 工作得更好更快,所以没有必要制作 AsyncTask。还要确保 Wifi 已打开。 :) 你也看到来自Log.d("socket", "trying to connect"); 的日志消息了吗?

标签: android sockets android-asynctask


【解决方案1】:

天哪!你放错了大括号!!!另外,我建议您将这些属性设置为受保护而不是私有,这是一个很长的故事来解释原因(它是为了防止创建特殊的静态方法来获取您的私有字段。您看不到编译器完成的那个过程)。

public class SplashScreen extends Activity {
    protected Socket socket; // the socket
    protected final int port = 1500; // the socket port - 1500
    protected boolean connectivity=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash_screen);
    this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
    ConnectToServer cts= new ConnectToServer();
    cts.execute();
    }

    private class ConnectToServer extends AsyncTask<Void, Void, Void> {

         @Override
         protected Void doInBackground(Void... params) {

             Log.d("socket", "trying to connect");
             try {
                 socket = new Socket("10.0.0.11", port);
             } catch (UnknownHostException e1) {
                 // TODO Auto-generated catch block
                 e1.printStackTrace();
             } catch (IOException e1) {
                 // TODO Auto-generated catch block
                 e1.printStackTrace();
             }
             if (socket.isConnected())
                 connectivity=true;
             return null;
         } // here u have the second } and you finished the class declaration without the following method which is overriden in activity!!!


          @Override
     protected void onPostExecute(Void result) {
        // execution of result of Long time consuming operation
         Intent i = new Intent(getBaseContext(), PreviewActivity.class);
         i.putExtra("connectivity", connectivity);
         startActivity(i);
         finish();

         }
    }
}

【讨论】:

  • 当然它编译得很好! Activity 有自己的 protected void onPostExecute() 方法,但您需要在 AsyncTask 类声明中覆盖它!!!不是活动的!!!!
  • 我已经更新了src,如果您仍然不明白这一点,请复制粘贴它。
  • 哇,我什至没有注意到我现在就试试
  • 我已经在我的项目中插入了你的代码并且它可以工作,至少它会转储日志消息。小心大括号。
  • 我已经更新了代码,onPostExecute 被错误地声明/覆盖,所以如果你真的拿走了我的代码,它不应该编译。
猜你喜欢
  • 2013-12-26
  • 1970-01-01
  • 2015-08-03
  • 1970-01-01
  • 2012-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-24
相关资源
最近更新 更多