onPostExecute调用得太晚

时间:2015-08-08 09:49:25

标签: java android android-asynctask httprequest ui-thread

首先,我将用文字简要描述过程/问题。之后,我将附上所有代码。

  1. 我在调试模式下启动android应用程序。
  2. 调用主要活动。
  3. 通过"意图"。
  4. 调用Activity2
  5. 然后(在onCreate中)我启动Async活动以从Web服务器获取信息。我用这个命令"新的Kontakt(info1,info2,信息).execute();"

  6. 我在" Kontakt"中的onPostExecute方法中设置了一个断点。看看会发生什么。但!!它并没有止步于此。所以我想," okey,doInBackground方法由于某种原因没有被调用"

  7. 当我继续运行代码时,出乎意料的是,onPostExecute被调用(在onClick结束时的某个地方)并在断点处停止,就像我想要的那样,但是为时已晚,因为我希望输出已经在onCreate方法中,我调用了#34; new Kontakt(info1,info2,information).execute();"。

  8. 那么为什么它太晚调用onPostExecute以及我该怎样做才能使进程在onCreate中已经正确地调用它" new Kontakt(info1,info2,information).execute(); "在Activity2?

  9. 代码在

    之下

    MainActivity.java

        package com.example.myapplication;
    
        import android.content.Context;
        import android.content.Intent;
        import android.content.SharedPreferences;
        import android.graphics.Color;
        import android.graphics.PorterDuff;
        import android.graphics.drawable.Drawable;
        import android.preference.PreferenceManager;
        import android.support.v7.app.ActionBarActivity;
        import android.os.Bundle;
        import android.util.Log;
        import android.view.Menu;
        import android.view.MenuItem;
        import android.view.View;
        import android.widget.Button;
        import android.widget.TextView;
    
    
        public class MainActivity extends ActionBarActivity implements View.OnClickListener {
            Button button1;
    
            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
    
                button1 = (Button) findViewById(R.id.button1);
                button1.setOnClickListener(this);
            }
    
    
            @Override
            public void onClick(View view) {
                switch (view.getId()) {
                    case R.id.button1:
                        Intent intent = new Intent(this, Activity2.class);
                        startActivity(intent);
                        break;
                }
            }
        }
    

    Activity2.java

            package com.example.myapplication;
    
            import android.app.AlertDialog;
            import android.content.Context;
            import android.content.DialogInterface;
            import android.content.SharedPreferences;
            import android.net.ConnectivityManager;
            import android.net.NetworkInfo;
            import android.preference.PreferenceManager;
            import android.support.v7.app.ActionBarActivity;
            import android.os.Bundle;
            import android.text.InputType;
            import android.text.TextUtils;
            import android.util.Log;
            import android.view.Menu;
            import android.view.MenuItem;
            import android.view.View;
            import android.view.WindowManager;
            import android.view.inputmethod.InputMethodManager;
            import android.widget.Button;
            import android.widget.EditText;
    
            import org.apache.http.NameValuePair;
            import org.apache.http.client.HttpClient;
            import org.apache.http.client.entity.UrlEncodedFormEntity;
            import org.apache.http.client.methods.HttpPost;
            import org.apache.http.impl.client.DefaultHttpClient;
            import org.apache.http.message.BasicNameValuePair;
            import org.apache.http.params.BasicHttpParams;
            import org.apache.http.params.HttpConnectionParams;
            import org.apache.http.params.HttpParams;
    
            import java.io.IOException;
            import java.net.MalformedURLException;
            import java.util.ArrayList;
    
    
    public class Activty2 extends ActionBarActivity implements View.OnClickListener {
    
        Button button;
        Context information;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity2);
            information=this;
            String info1="Yeah";
            String info2="Yeah";
            new Kontakt(info1, info2, information).execute();
    
            button = (Button) findViewById(R.id.button);
            button.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
            if (view.getId() == R.id.button) {
            }
        }
    

    Kontakt.java

    package com.example.myapplication;
    
    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.SharedPreferences;
    import android.os.AsyncTask;
    import android.util.Log;
    
    
    
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.params.BasicHttpParams;
    import org.apache.http.params.HttpConnectionParams;
    import org.apache.http.params.HttpParams;
    import org.json.JSONObject;
    
    import java.util.ArrayList;
    
    public class Kontakt extends AsyncTask<Void, Void, Integer> {
    
        public static final int CONNECTION_TIMEOUT = 1000 * 15;
        public static final String SERVER_ADDRESS = "http://test.site.net/";
    
        String info1;
        String info2;
        private final Context information;
    
    public Kontakt(String info1, String info2, Context context) {
    this.info1=info1;
    this.Info2=info2;
        this.information=context;
    }
    
    
            @Override
            protected Integer doInBackground(Void... params) {
    
    
                ArrayList<NameValuePair> dataToSend = new ArrayList<>();
                dataToSend.add(new BasicNameValuePair("user", info1));
                dataToSend.add(new BasicNameValuePair("password", info2));
    
                HttpParams httpRequestParams = getHttpRequestParams();
    
                HttpClient client = new DefaultHttpClient(httpRequestParams);
                HttpPost post = new HttpPost(SERVER_ADDRESS
                        + "Register.php");
    
                try {
                    post.setEntity(new UrlEncodedFormEntity(dataToSend));
                    client.execute(post);
                    int result=1;
                    return result;
    
                } catch (Exception e) {
                    e.printStackTrace();
                    int result=0;
                    return result;
                }
    //return null;
    
            }
    
        private HttpParams getHttpRequestParams() {
            HttpParams httpRequestParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(httpRequestParams,
                    CONNECTION_TIMEOUT);
            HttpConnectionParams.setSoTimeout(httpRequestParams,
                    CONNECTION_TIMEOUT);
            return httpRequestParams;
        }
    
        @Override
        protected void onPostExecute(Integer result)
        {
            super.onPostExecute(result);
            SharedPreferences.Editor editor = information.getSharedPreferences("INTERNET_KOLL", Context.MODE_PRIVATE).edit();
            editor.putString("internet_status", String.valueOf(result));
            editor.commit();
        }
    
    }
    

1 个答案:

答案 0 :(得分:0)

Activity2,当onCreate()拨打new Kontakt(info1, info2, information).execute();时:

  • 异步任务(Kontakt)在后台启动
  • 执行onCreate()继续

所以你没有立即得到回应是正常的。