onPostExecute方法永远不会在AsyncTask中调用

时间:2012-07-05 16:26:07

标签: android android-asynctask

我正在尝试从数据库服务器获取一些数据。我明白我无法在UI主线程上执行网络作业。我找到的解决方案是AsyncTask。我重写了两个方法(onPostExecute,doInBackground),但是 在doInBackground之后,onPostExecute从未调用过,我的android手机(galaxy s2)迫使我的应用程序停止。哪里错了?

public class MainActivity extends Activity {

    EditText mResult;
    private static final String TAG = "JSON";
    String Json;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mResult = (EditText)findViewById(R.id.result);

        Button btn = (Button)findViewById(R.id.parse);
        btn.setOnClickListener(new Button.OnClickListener(){
            @Override
            public void onClick(View v){
                getHtml("http://192.168.0.12/index.php");
                //Json = getHtml("http://192.168.0.12/index.php");
                Log.i(TAG, Integer.toString(5));
                Log.i(TAG, Json);
                Log.i(TAG, Integer.toString(6));
                try{
                    String Result = "member list: \n";
                    JSONArray ja = new JSONArray(Json);
                    for(int j=0; j<ja.length(); j++)
                    {
                        JSONObject order = ja.getJSONObject(j);
                        Result +="ID : " + order.getInt("id") + " " +
                        "number : " + order.getInt("gisoo")+ " " +
                        "name : " + order.getString("name")+ " " +
                        "sex : " + order.getString("sex")+ " " +
                        "age : " + order.getInt("age")+ " " +
                        "school : " + order.getString("university")+ " " +
                        "phone : " + order.getString("phone")+ " " +
                        "no : " + order.getInt("numberOfVolunteer")+ " " +
                        "lastlogin : " + order.getString("lastLogin")+ " " +
                        "lastactivity : " + order.getString("lastVolunteer")+ " " +
                        "message : " + order.getString("message")+ "\n\n";
                    }
                    mResult.setText(Result);
                } catch(JSONException e)
                {
                    Toast.makeText(v.getContext(), e.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    }

    private void getHtml(String url)
    {
        new ProcessHtmlTask().execute(url);
    }

    private class ProcessHtmlTask extends AsyncTask<String, Void, String>{

        @Override
        protected void onPostExecute(String JSON){
            Log.i(TAG, Integer.toString(8));  //I couldn't see number 8 in log.
            Json = JSON;

            return;
        }

        @Override
        protected String doInBackground(String... addr){
        StringBuilder jsonHtml = new StringBuilder();
        HttpURLConnection conn;
        try{
            URL url = new URL(addr[0]);
            conn = (HttpURLConnection)url.openConnection();

            if(conn != null)
            {

                conn.setConnectTimeout(10000);
                conn.setUseCaches(true);
                int n = conn.getResponseCode();
                Log.i(TAG, Integer.toString(n));
                if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){

                    BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "EUC-KR"));
                    for(;;){
                        String line = br.readLine();

                        if(line == null) break;
                        jsonHtml.append(line + "\n");
                    }

                    Log.i(TAG, Integer.toString(3));
                    br.close();
                }
                Log.i(TAG, Integer.toString(4));
                conn.disconnect();
            }

        } catch(Exception ex){
            Log.i(TAG, ex.getMessage());

        }
        Log.i(TAG, jsonHtml.toString());  
        Log.i(TAG, Integer.toString(5));  //I checked html string and number 5 showed up in log.
        return jsonHtml.toString();
    }
   }

1 个答案:

答案 0 :(得分:2)

问题出在你的onCLick方法中。在getHtml()之后要执行的所有内容都必须放在onPostExecute中。这是因为AsyncTask是异步执行的。