Java 检查异步任务上的连接

Java 检查异步任务上的连接,java,android,multithreading,android-asynctask,Java,Android,Multithreading,Android Asynctask,在asynctask之前,我检查了连接和正确的链接,但有时应用程序会崩溃,因为我认为它是在UI线程上执行的。如果我将代码放在AsyncTask上,应用程序总是会崩溃。有解决办法吗 在onCreate方法上: if(connectionOK()) { try { url = new URL(bundle.getString("direccion")); con = (HttpURLConnection)

在asynctask之前,我检查了连接和正确的链接,但有时应用程序会崩溃,因为我认为它是在UI线程上执行的。如果我将代码放在AsyncTask上,应用程序总是会崩溃。有解决办法吗

在onCreate方法上:

if(connectionOK())
      {
          try {
                url = new URL(bundle.getString("direccion"));
                con = (HttpURLConnection) url.openConnection();
                if(con.getResponseCode() == HttpURLConnection.HTTP_OK)
                {
                    Tarea tarea = new Tarea(this);
                    tarea.execute();
                }
                else
                {
                    con.disconnect();
                   //show alertdialog with the problem
                    direccionInvalida();
                }
        } catch (Exception e){e.printStackTrace();}

      }
      else
      { 
             //show alertdialog with the problem
             notConnection() 
      }

你的问题很模糊

但是:在较新的androids中,您不能在UI线程上执行这一行:

con = (HttpURLConnection) url.openConnection();
因此,一个简单的解决方案是在新线程中添加所有内容:

new Thread() {
    public void run() {
        //add all your code
    }
}.start();
但是,您的代码有一些块显示如下对话框(猜测):

//show alertdialog with the problem
notConnection();
这些功能应该在UI线程上完成。因此,请使用处理程序:

//add this outsire the thread
Handler mHandler = new Handler();
然后在代码中使用:

mHandler.post(new Runnable() {
    public void run() {
        notConnection();
    }
});


最后,这是一个解决方案。真正的解决方案是,您无论如何都要发布异步任务,并在
onPostExecute()

中处理错误或成功。试试这个,检查
doInBackground
内部的网络连接

public class GetTask extends AsyncTask<Void, Void, Integer> {

    protected void onPreExecute() {
        mProgressDialog = ProgressDialog.show(MainActivity.this,
                "Loading", "Please wait");
    }

    @Override
    protected Integer doInBackground(Void... params) {
        // TODO Auto-generated method stub
                   if(connectionOK()){
        //ADD YOUR API CALL
              return 0;
                  }esle{
                     return 1;
                   }

    }

    protected void onPostExecute(Integer result) {
        super.onPostExecute(result);
        if (mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        } 
                    if(result == 0){
                       //do your stuff
                     }else{
                      //show alertdialog with the problem
                     }

    }
}
公共类GetTask扩展异步任务{
受保护的void onPreExecute(){
mProgressDialog=ProgressDialog.show(MainActivity.this,
“正在装货”,“请稍候”);
}
@凌驾
受保护的整数doInBackground(Void…params){
//TODO自动生成的方法存根
if(connectionOK()){
//添加您的API调用
返回0;
}埃斯勒{
返回1;
}
}
受保护的void onPostExecute(整数结果){
super.onPostExecute(结果);
if(mProgressDialog.isShowing()){
mProgressDialog.disclose();
} 
如果(结果==0){
//做你的事
}否则{
//显示带有问题的alertdialog
}
}
}

您可以发布您的任务吗?您在哪里检查连接?能否显示错误的日志/堆栈跟踪?