Android 代码在onPostExecute函数中不起作用

Android 代码在onPostExecute函数中不起作用,android,android-asynctask,Android,Android Asynctask,onPostExecute功能有问题。它没有调用函数,因为即使只调用了setText(“某物”)函数,也无法打印任何内容。请帮忙 public class Update extends Activity { JSONArray jArray = null; String result = null; StringBuilder sb = null; InputStream is = null; String ct_name; LinearLayo

onPostExecute
功能有问题。它没有调用函数,因为即使只调用了
setText(“某物”)
函数,也无法打印任何内容。请帮忙

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();
            }
        }
    }

}
公共类更新扩展活动{
JSONArray-jArray=null;
字符串结果=null;
StringBuilder sb=null;
InputStream=null;
字符串ct_名称;
线性布局;
弦乐;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u更新);
Toast.makeText(getBaseContext(),“完成布局”,Toast.LENGTH\u LONG)
.show();
新的长操作().execute();
}
类LongOperation扩展了异步任务{
@凌驾
受保护的字符串doInBackground(字符串…参数){
//http post
HttpClient HttpClient=新的DefaultHttpClient();
//为什么要使用10.0.2.2
HttpPost HttpPost=新的HttpPost(
"http://10.0.2.2/moodle/myFile.php");
试一试{
ArrayList nameValuePairs=新的ArrayList();
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
response.getStatusLine().getStatusCode();
HttpEntity getResponseEntity=response.getEntity();
responseString=EntityUtils.toString(getResponseEntity);
}捕获(客户端协议例外e){
//TODO自动生成的捕捉块
}捕获(IOE异常){
//TODO自动生成的捕捉块
}
回报率;
}
受保护的void onPostExecute(字符串resultStr){
试一试{
JSONObject json=新的JSONObject(responseString);
JSONArray jArray=json.getJSONArray(“结果”);
l=(线性布局)findViewById(R.id.container);
for(int i=0;i
这是异步任务之前的工作代码

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();
 }


}
}
公共类MainActivity扩展活动{
JSONArray-jArray=null;
字符串结果=null;
StringBuilder sb=null;
InputStream=null;
字符串ct_名称;
线性布局;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList nameValuePairs=新的ArrayList();
//http post
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
//为什么要使用10.0.2.2
HttpPost HttpPost=新的HttpPost(“http://10.0.2.2/moodle/myFile.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
}捕获(例外e){
e(“Log_标记”,“http连接错误”+e.toString());
}
//将响应转换为字符串
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
sb=新的StringBuilder();
sb.append(reader.readLine()+“\n”);
字符串行=“0”;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
}捕获(例外e){
Log.e(“Log_标记”,“错误转换结果”+e.toString());
}
试一试{
jArray=新的JSONArray(结果);
JSONObject json_data=null;
l=(线性布局)findViewById(R.id.container);

对于此块最后一行中的(int i=0;i,应使用resultStr not responseString。 您也没有在
doInBackground
方法中记录大多数异常。 养成使用
Log.e(标记“错误描述…”,异常)
而不是使用
e.printstackTrace()
e.toString()


你能试着把
@Override
放在
onPostExecute
method@Override受保护的void onPostExecute(String resultStr){TextView TextView=new TextView(null);TextView.setText(“ct_name”)}我完成了此操作。它也无法显示任何内容
protected void onPostExecute(String resultStr) {
        try {
            JSONObject json = new JSONObject(responseString);