1

I'm trying to fetch some data from db server. I understood I can't excute the network job on UI Main thread. the solution I found is AsyncTask. I override two method(onPostExecute, doInBackground), but after doInBackground, onPostExecute never called, and my android phone(galaxy s2) force my app to stop. where is the wrong place?

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();
    }
   }
nhaarman
  • 98,571
  • 55
  • 246
  • 278
  • possible duplicate of [onPostExecute not called after completion AsyncTask](http://stackoverflow.com/questions/3606505/onpostexecute-not-called-after-completion-asynctask) – nhaarman Jul 05 '12 at 16:28

1 Answers1

2

The problem is inside your onCLick method. All you want to execute after getHtml() must be placed inside your onPostExecute. This is beacuse your AsyncTask is executed asyncronously.

Stefano Ortisi
  • 5,288
  • 3
  • 33
  • 41
  • Thank you very much! It works well as following your advice. I am not close to thread programming not yet but you helped me go up to next step. Thanks – Hoe Hyeon Jeong Jul 05 '12 at 23:00