相关疑难解决方法(0)

如何将OnPostExecute()的结果导入主活动,因为AsyncTask是一个单独的类?

我有这两节课.我的主要活动和扩展的一个AsyncTask,现在在我的主要活动,我需要从得到的结果OnPostExecute()AsyncTask.我怎样才能将结果传递给我的主要活动?

这是示例代码.

我的主要活动.

public class MainActivity extends Activity{

    AasyncTask asyncTask = new AasyncTask();

    @Override
    public void onCreate(Bundle aBundle) {
        super.onCreate(aBundle);            

        //Calling the AsyncTask class to start to execute.  
        asyncTask.execute(a.targetServer); 

        //Creating a TextView.
        TextView displayUI = asyncTask.dataDisplay;
        displayUI = new TextView(this);
        this.setContentView(tTextView); 
    }

}
Run Code Online (Sandbox Code Playgroud)

这是AsyncTask类

public class AasyncTask extends AsyncTask<String, Void, String> {

TextView dataDisplay; //store the data  
String soapAction = "http://sample.com"; //SOAPAction header line. 
String targetServer = "https://sampletargeturl.com"; //Target Server.

//SOAP Request.
String soapRequest …
Run Code Online (Sandbox Code Playgroud)

android android-asynctask

338
推荐指数
7
解决办法
27万
查看次数

使用AsyncTask

抱歉,如果这是一个简单的问题,但我对此很新,还在学习.

我有一个应用程序,当我的用户在输入他们的详细信息后单击按钮登录时,它崩溃与android.os.NetworkOnMainThreadException我发现这是因为我在主线程上执行网络操作并解决我需要使用AsyncTask,我被语法困住了.

单击按钮后,这是我的代码,调用函数进行连接,然后将json解析为sqlite db.

// Login button Click Event
    btnLogin.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            String email = inputEmail.getText().toString();
            String password = inputPassword.getText().toString();
            UserFunctions userFunction = new UserFunctions();
            JSONObject json = userFunction.loginUser(email, password);

            // check for login response
            try {
                if (json.getString(KEY_SUCCESS) != null) {
                    loginErrorMsg.setText("");
                    String res = json.getString(KEY_SUCCESS); 
                    if(Integer.parseInt(res) == 1){
                        // user successfully logged in
                        // Store user details in SQLite Database
                        DatabaseHandler db = new DatabaseHandler(getApplicationContext());
                        JSONObject json_user = json.getJSONObject("user");

                        // Clear all previous …
Run Code Online (Sandbox Code Playgroud)

android exception android-asynctask

13
推荐指数
1
解决办法
1万
查看次数

重复网络任务的最佳实践?

我有一个小的Android应用程序,我需要每隔几秒钟做一些FTP的东西.在学习了很难在UI线程上运行网络内容的东西是Android不喜欢的东西后,我来到这个解决方案:

// This class gets declared inside my Activity
private class CheckFtpTask extends AsyncTask<Void, Void, Void> {
    protected Void doInBackground(Void... dummy) {
        Thread.currentThread().setName("CheckFtpTask");
        // Here I'll do the FTP stuff       
        ftpStuff();
        return null;
    }
}

// Member variables inside my activity
private Handler checkFtpHandler;
private Runnable checkFtpRunnable;

// I set up the task later in some of my Activitiy's method:
checkFtpHandler = new Handler();
checkFtpRunnable = new Runnable() {
    @Override
    public void run() {
        new CheckFtpTask().execute((Void[])null);
        checkFtpHandler.postDelayed(checkFtpRunnable, 5000);
    }
}; …
Run Code Online (Sandbox Code Playgroud)

multithreading android android-networking android-asynctask

5
推荐指数
1
解决办法
1015
查看次数

不使用get()方法从AsyncTask返回值

我正在尝试从DoInBackground中的asynctask返回值,但调用get()方法会冻结我的UI.如何将代码重新编写为回调方法?:

public class GetUrlDataTask extends AsyncTask<String, Integer, String> {
String response;
HttpUtils util;
@Override
protected String doInBackground(String... params) {
    try {
        util = new HttpUtils(params[0]);
        response = util.getContent();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return response;
}

@Override
protected void onPostExecute(String result) {
    super.onPostExecute(result);
}
Run Code Online (Sandbox Code Playgroud)

在我的活动中,我得到了结果 response = new GetUrlDataTask().execute("site").get;

android http android-asynctask

3
推荐指数
2
解决办法
1万
查看次数

正在执行Asynctask时按钮保持按下状态

我有一个按钮,按下后,执行以下代码:

public void onClick(View v) {
            // TODO Auto-generated method stub
            //progressSpin.setVisibility(View.VISIBLE);
            try {
                data=new WebkioskExtractor().execute(username,password).get();
                System.out.println("Data = "+data);                 
            } catch (Exception e) {
                // TODO Auto-geneorated catch block
                e.printStackTrace();
            }
            //progressSpin.setVisibility(View.GONE);
        }
Run Code Online (Sandbox Code Playgroud)

从代码中清楚,我必须等待AsyncTask完成,因为我依赖于它返回的数据.问题是,当执行任务(从互联网上获取一些数据)时,按钮仍处于按下状态.即使我将我创建的进度条设置为VISIBLE,也不会显示.

我怎样才能解决这个问题?我希望按下按钮一次,然后进度条应该开始旋转,这不会发生.

android android-progressbar android-asynctask

0
推荐指数
1
解决办法
173
查看次数