代码不在onPostExecute函数中

时间:2014-05-09 09:40:20

标签: android android-asynctask

onPostExecute功能出现问题。它没有调用该函数,因为即使只调用setText("Something")函数也无法打印出来。请帮帮....

public class Update extends Activity {

    JSONArray jArray = null;
    String result = null;
    StringBuilder sb = null;
    InputStream is = null;
    String ct_name;
    LinearLayout l;
    String responseString;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        Toast.makeText(getBaseContext(), "Done layout", Toast.LENGTH_LONG)
                .show();

        new LongOperation().execute();
    }

    class LongOperation extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {

            // http post

            HttpClient httpclient = new DefaultHttpClient();

            // Why to use 10.0.2.2
            HttpPost httppost = new HttpPost(
                    "http://10.0.2.2/moodle/myFile.php");
            try {
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                response.getStatusLine().getStatusCode();
                HttpEntity getResponseEntity = response.getEntity();
                responseString = EntityUtils.toString(getResponseEntity);

            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
            } catch (IOException e) {
                // TODO Auto-generated catch block
            }
            return responseString;
        }

        protected void onPostExecute(String resultStr) {
            try {
                JSONObject json = new JSONObject(responseString);
                JSONArray jArray = json.getJSONArray("result");
                l = (LinearLayout) findViewById(R.id.container);
                for (int i = 0; i < jArray.length(); i++) {
                    JSONObject json_data = jArray.getJSONObject(i);
                    ct_name = json_data.getString("name");
                }
                TextView txt = (TextView) findViewById(R.id.txt_1);
                txt.setText("Executed");
                Toast.makeText(getBaseContext(), "this is post",
                        Toast.LENGTH_LONG).show();

                Toast.makeText(getBaseContext(), ct_name, Toast.LENGTH_LONG)
                        .show();
                TextView textView = new TextView(null);
                textView.setText(ct_name
                        + " is out, please attempt it as soon as possible"
                        + "\n");
                l.addView(textView);

            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

这是异步任务之前的工作代码。

public class MainActivity extends Activity {

JSONArray jArray = null;
String result = null;
StringBuilder sb = null;
InputStream is = null;
String ct_name;
LinearLayout l;
@Override
protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//http post
try{
     HttpClient httpclient = new DefaultHttpClient();

     //Why to use 10.0.2.2
     HttpPost httppost = new HttpPost("http://10.0.2.2/moodle/myFile.php");
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
     HttpResponse response = httpclient.execute(httppost);
     HttpEntity entity = response.getEntity();
     is = entity.getContent();
     }catch(Exception e){
         Log.e("log_tag", "Error in http connection"+e.toString());
    }
//convert response to string 
try{
      BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
       sb = new StringBuilder();
       sb.append(reader.readLine() + "\n");

       String line="0";
       while ((line = reader.readLine()) != null) {
                      sb.append(line + "\n");
        }
        is.close();
        result=sb.toString();
        }catch(Exception e){
              Log.e("log_tag", "Error converting result "+e.toString());
        }



try{
      jArray = new JSONArray(result);
      JSONObject json_data=null;

        l = (LinearLayout) findViewById(R.id.container);
      for(int i=0;i<jArray.length();i++){
             json_data = jArray.getJSONObject(i);
             ct_name=json_data.getString("name");//here "Name" is the column name in database
             Toast.makeText(getBaseContext(), ct_name,
                       Toast.LENGTH_LONG).show();
             TextView textView = new TextView(this);
             textView.setText(ct_name + " is out, please attempt it as soon as possible"+ "\n");
             l.addView(textView);
         }

}
      catch(JSONException e1){
       Toast.makeText(getBaseContext(), "No Data Found" ,Toast.LENGTH_LONG).show();
      } catch (ParseException e1) {
   e1.printStackTrace();
 }


}
}

1 个答案:

答案 0 :(得分:0)

在此块的最后一行中,您应该使用resultStr而不是responseString。 您也没有记录doInBackground方法中的大多数异常。 养成Log.e(TAG,"description of error....",exception)而不是使用e.printstackTrace()e.toString()

的习惯
protected void onPostExecute(String resultStr) {
        try {
            JSONObject json = new JSONObject(responseString);