2 myc 100 myc_100 于 2013.09.28 14:10 提问

在 AsyncTask 中不能调用 onPostExecute 方法

我想从数据库服务器中获取数据,但是却不能在 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();
    }
   }
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!