Java 在android中启动asynctask时出错

Java 在android中启动asynctask时出错,java,android,Java,Android,我在两个不同的服务中启动两个异步任务第一个工作正常并打开一个套接字,但第二个没有启动这里是我的应用程序基于wifip2p中的客户端套接字编程的代码 public class RecieveAudioService extends Service { String tag = "Recieve Audio Service"; @Override public IBinder onBind(Intent intent) { return null;

我在两个不同的服务中启动两个异步任务第一个工作正常并打开一个套接字,但第二个没有启动这里是我的应用程序基于wifip2p中的客户端套接字编程的代码

    public class RecieveAudioService extends Service {

    String tag = "Recieve Audio Service";

    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Toast.makeText(getApplicationContext(), "Recieve audio :requesting to client", Toast.LENGTH_LONG).show();

        String[] param = {"h", "j", "k"};

        new request().execute();
        Log.v("Recieve Audio", "Inside Recieve audio service");

        return super.onStartCommand(intent, flags, startId);
    }

//===================== This class is sending request to server for connection when invocked
    public class request extends AsyncTask<String, String, String> {

        protected String doInBackground(String... arg0) {

            Log.v("second asyn", "this is second asynchronous task");

            try {

                Toast.makeText(getApplicationContext(), "Request to connect sent to server",
                        Toast.LENGTH_LONG).show();
                String toConnectDeviceIP = "192.168.49.1";
                Integer toConnectDevicePort = 8988;

                Socket connect = new Socket();

                connect.bind(null);
                connect.connect((new InetSocketAddress(toConnectDeviceIP, toConnectDevicePort)),
                        5000);
                Log.v(tag, "sent the connection request to clint");
                connect.close();

            } catch (Exception e) {
                // TODO Auto-generated catch block
                Log.v(tag, "" + e.toString());
                Toast.makeText(getApplicationContext(), "i found exception in connection"
                        +e.toString(), Toast.LENGTH_LONG).show();
            }

            return "success";
        }
    }
//====================================================================

}
公共类ReceiveAudioService扩展服务{
String tag=“接收音频服务”;
@凌驾
公共IBinder onBind(意向){
返回null;
}
@凌驾
公共int onStartCommand(Intent Intent、int标志、int startId){
Toast.makeText(getApplicationContext(),“接收音频:请求客户端”,Toast.LENGTH\u LONG.show();
字符串[]param={“h”,“j”,“k”};
新请求().execute();
Log.v(“接收音频”、“内部接收音频服务”);
返回super.onStartCommand(intent、flags、startId);
}
//============================此类在调用时正在向服务器发送连接请求
公共类请求扩展异步任务{
受保护的字符串doInBackground(字符串…arg0){
Log.v(“第二个asyn”,“这是第二个异步任务”);
试一试{
Toast.makeText(getApplicationContext(),“发送到服务器的连接请求”,
Toast.LENGTH_LONG).show();
字符串toConnectDeviceIP=“192.168.49.1”;
整数toConnectDevicePort=8988;
套接字连接=新套接字();
connect.bind(null);
connect.connect((新的InetSocketAddress(toConnectDeviceIP,toConnectDevicePort)),
5000);
Log.v(标记“已向clint发送连接请求”);
connect.close();
}捕获(例外e){
//TODO自动生成的捕捉块
Log.v(tag,“+e.toString());
Toast.makeText(getApplicationContext(),“我在连接中发现异常”
+e、 toString(),Toast.LENGTH_LONG).show();
}
返回“成功”;
}
}
//====================================================================
}

您似乎是在Android 4.x/3.x上启动应用程序的。Android 3.0之前的
AsyncTasks
并行运行,但更高版本(>3.x)的系统会一个接一个地执行任务。所以你有两个选择:

a) 使用
线程
而不是
异步任务

b) 使用
AsyncTask
executeOnExecutor
并发执行任务:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
} else {
    task.execute();
}

一次只能运行一个异步任务实例。那么,如果我在“活动”中执行请求,则如何向服务器发送请求?它会导致错误networkOperationOnMainThread或类似错误。请参见此处,您需要另一个执行器来执行其他异步任务。或者只是使用一个线程,因为显然你没有做任何事情的结果。或者甚至是一项意向服务。谢谢,这似乎很有帮助:)@用户127467不客气。若答案对你们有帮助,别忘了接受它。谢谢