Android AsyncTask输入和输出不能正常工作

问题描述:

第一:抱歉我的英语可能不好。Android AsyncTask输入和输出不能正常工作

我有2个类:处理UI的主类和用于通过使用PHP从SQL Server检索数据的第二个类。

从第一个类中调用第二个类中的mehtod,并传递和检索变量。 其实它工作正常,没有AsyncTask

但是因为我想使用运行Android 3.0及更高版本的设备上的代码,我需要将该方法更改为AsyncTask。否则我得到这个错误: “android.os.networkonmainthreadexception

实际工作的代码如下所示:

主要类:

... 
String inputString="1"; 
String outputString; 
outputString = Class2.SomeMethodInClass2(inputString); 
.... 

等级2:

public class Class2 { 

    public static String SomeMethodInClass2(String input) { 
     String Output; 
     ... 
     //Do some php-sql stuff based on "input"-variable 
     //and return the "output"-variable 
     ... 
     return output; 
    } 
} 

此代码工作完全在Android 2.0但我需要将其更改为AsyncTask,因为Andoid 3.0及以上版本给我:“android.os.networkonmainthreadexception

我已阅读了很多有关AsyncTask的帖子,但是我无法在代码中使用输入和输出。 Eclipse allways告诉我,我的代码有问题。

我该如何改变我的代码,成为一个工作的异步任务? (请使用我上面的代码示例进行说明)

预先感谢您!

亲切的问候

--edit:如果有一个更简单的方式得到3.0比的AsyncTask高于摆脱“android.os.networkonmainthreadexception”,这将是罚款呢! -

+0

请发布您的异步任务代码。此外,您需要在单独的线程中执行紧张而耗时的操作 - 对于不仅仅是OS 3.0及更高版本的每个API版本都是如此。 – Skynet 2014-09-04 09:54:14

+0

友善的建议:先尝试__googling__首先或更好** SO **首先 – BlackBeard 2014-09-04 10:26:55

+0

谢谢,我使用谷歌搜索尝试了3天。我有很多asyncTask Code-Tryes,每个人都有不同的错误消息。我不认为他们中的任何人都会朝着正确的方向发展 – jemandanders 2014-09-04 10:33:56

有所回调在SecondClass

扩展您的SecondClassAsynctask

实施doinbackground 回报结果preExecutedoinbackgroundpostExecute方法

做你的东西doinbackground

postExecute结果Callback

FirstClass 开始SecondClass(执行)实施SecondClass.CallbackFirstClass

传递Callback参考回调只是处理你的下一个操作,结果

编辑:

public class SecondClass extends AsyncTask<String, Void, String> { 
public interface Callback { 
    public void update(String result); 
} 
Callback mCallback; 

    public SecondClass(Callback callback) { 
     super(); 
    mCallback = callback; 
    } 

    @Override 
    protected String doInBackground(String... params) { 
    String result = null; 
     //do your stuff and save result 
     return result; 
    } 

@Override 
    protected void onPostExecute(String result) { 
    if(mCallback != null) 
    mCallback.update(result) 
     super.onPostExecute(e); 
    } 
} 

public class FirstClass implements SecondClass.Callback{ 
@Override 
public void update(String result){ 
     //do your stuff with result 
} 

return_type someMethod(){ 
    SecondClass sc = new SecondClass(this) ; 
    sc.execute(someurl); 
    } 
    } 
+0

谢谢,我相信这个答案是正确的,但我只是无法弄清楚如何做到这一点我的代码... – jemandanders 2014-09-04 10:54:40

+0

感谢您的编辑,现在它似乎更清楚我 – jemandanders 2014-09-04 12:04:25

感谢您的帖子!

但是我发现了一种替代方式,对我来说比做异步任务似乎更容易和更好。 我加入到我的主类:

public class MainClass extends Activity implements OnTouchListener { 
    ... 
    BackgroundOperations1 ourBackgroundOperations; //<===new 
    ... 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     ... 
     ourBackgroundOperations = new BackgroundOperations1(); //<===new 
     ... 
    } 

    @Override 
    protected void onPause() { 
     .... 
     ourBackgroundOperations.pause(); // <===new 
    } 

    @Override 
    protected void onResume() { 
     .... 
     ourBackgroundOperations.resume(); // <===new 
    } 




    //whole new class inside of "MainClass" 
    //runs a different thread, i believe...? 
    public class BackgroundOperations1 implements Runnable { 
      Thread ourThread = null; 
      boolean isRunning = false; 



      public void pause() { 
        isRunning = false; 
        while (true) { 
         try { 
          ourThread.join(); 
         } catch (InterruptedException e) { 
          e.printStackTrace(); 
         } 
         break; 
        } 
      } 

      public void resume() { 
        isRunning = true; 
        ourThread = new Thread(this); 
        ourThread.start(); 
      } 

      @Override 
      public void run() { 
       while (isRunning) { 
        if (dothis==true){ 
         //here i call my mehtod from class2, whenever "dothis" is set to true (somewhere in my onTouch or where ever i want) 
         String inputString="1"; 
         String outputString; 
         outputString = Class2.SomeMethodInClass2(inputString); 

        } 
       } 
      } 
    } 
}  

这适用于Android 4.0,我认为这就是做我想做的最佳方式。对我来说,AsyncTask似乎更清晰,因为我可以用简单的方式调用我的方法来传递变量。

还是有理由不这样做?