Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在AsyncTask类中添加更多方法?_Java_Android_Sockets_Android Asynctask - Fatal编程技术网

Java 如何在AsyncTask类中添加更多方法?

Java 如何在AsyncTask类中添加更多方法?,java,android,sockets,android-asynctask,Java,Android,Sockets,Android Asynctask,我想通过Java套接字在我的android和服务器(在我的PC中)之间建立一些连接。简单地说,它只是通过本地网络发送字符串和整数等数据 我读过关于AsyncTask类的文章。我在类(名为RemoteClient的类)中的doInBackground()方法中添加了套接字连接代码。以下是通过套接字连接和初始化连接的代码: @Override protected Void doInBackground(Void... arg0) { Log.i("Socket","C

我想通过Java套接字在我的android和服务器(在我的PC中)之间建立一些连接。简单地说,它只是通过本地网络发送字符串和整数等数据

我读过关于AsyncTask类的文章。我在类(名为
RemoteClient
的类)中的
doInBackground()
方法中添加了套接字连接代码。以下是通过套接字连接和初始化连接的代码:

    @Override
    protected Void doInBackground(Void... arg0) {
        Log.i("Socket","Connecting to the remote server: " + host);
        try{
            socket = new Socket(host,8025);
            oos = new ObjectOutputStream(socket.getOutputStream());

        }catch(IOException ex){
            ex.printStackTrace();
        }
        return null;

    }
我只想通过套接字向服务器发送一个方法
sendInteger(int-value)
。我已经完成了连接,连接得很好

那么,我应该把
sendInteger(int-value)
方法放在哪里,我可以把该方法和实现的方法一起放在
RemoteClient
类中吗

我想在单击按钮时调用
sendInteger(int-value)
方法


谢谢。

是的,您可以将sendInteger放入异步任务中。我喜欢使用构造函数而不是AsyncTask参数,因为它更灵活。您可以将接口实现为回调,以在UI中接收服务器应答。onPostExecute总是在onBackground执行之后由系统调用。onPostExecute也在UI线程上运行,因此您不必处理返回UI线程的问题,因为您的AsyncTask将在不同的线程上运行

您的代码如下所示:

Fragmentclass or Activity:

private void onButtonClick() {
  ...
  RemoteClient rc = new RemoteClient(value, (response) -> {
     Log.d(response);
  };
  rc.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
  ...
}

public class RemoteClient extends AsyncTask<Void, Void, Void> {
  private Callback callback;
  private int intValue;

  public RemoteClient(int intValue, Callback callback) {
    this.callback = callback;
    this.intValue = intValue;
  }

  @Override
  protected Void doInBackground(Void... arg0) {
      Log.i("Socket","Connecting to the remote server: " + host);
      try{
        socket = new Socket(host,8025);
        oos = new ObjectOutputStream(socket.getOutputStream());
        sendInteger(intvalue);
      } catch(IOException ex){
        ex.printStackTrace();
      }
      return null;

  }

  private void sendInteger(int value) {
    // send the integer
  }

  protected void onPostExecute(Void aVoid) { 
    if (callback != null) {
       callback.response;
    }
  }

private interface Callback {
  void serverResponse(String value);
}
}
Fragmentclass或Activity:
私有void onButtonClick(){
...
RemoteClient rc=新的RemoteClient(值,(响应)->{
Log.d(答复);
};
rc.executeOnExecutor(异步任务.线程池执行器);
...
}
公共类RemoteClient扩展异步任务{
私有回调;
私有int值;
公共远程客户端(int值,回调){
this.callback=回调;
this.intValue=intValue;
}
@凌驾
受保护的Void doInBackground(Void…arg0){
Log.i(“套接字”,“连接到远程服务器:“+主机”);
试一试{
插座=新插座(主机,8025);
oos=newObjectOutputStream(socket.getOutputStream());
sendInteger(intvalue);
}捕获(IOEX异常){
例如printStackTrace();
}
返回null;
}
私有void sendInteger(int值){
//发送整数
}
受保护的void onPostExecute(void aVoid){
if(回调!=null){
回应;
}
}
专用接口回调{
void serverResponse(字符串值);
}
}

如果您想变得更灵活,可以实现一个NetworkManager类,该类只启动异步任务。然后您可以始终将字符串作为JSON传递给AsyncTask。或者您只使用AsyncTask,但我建议使用继承。

感谢您的帮助,我可以像
rc.sendInteger(value)那样直接调用函数吗
在主类中?因为我调用了
rc.execute()在OnCuto方法中初始化连接,因此它在启动时自动连接到服务器。谢谢诺是unhandy,因为在执行doIn后台之后,任务将被杀死和垃圾收集。考虑我所提到的。使用NETWORKMAMER作为Singleton,在那里建立连接并解析您的UPU。使用GSON将t转换为JSON,这样您就不必处理不同的输入。您还可以使用改型!这是一个易于使用的库,来自谷歌推荐的用于网络通信的库。