如何在onPostExecute方法中获得结果

时间:2017-01-20 11:54:16

标签: android json model-view-controller android-sdk-manager

我正在尝试从模型中获取getcontent()方法,但是当我尝试获取方法时,它显示为null值。

public class Gettingcomment extends AsyncTask<String,String,List<CommentModel>>{


    @Override
    protected List<CommentModel> doInBackground(String... params) {

        String commenturl = params[0];

        Populatecomments populatecomments =new Populatecomments();
       // populatecomments.getCommentModelList(commenturl+mytrendinid);

        Log.i("mytrendin",commenturl);

        List<CommentModel>  model= populatecomments.getCommentModelList(commenturl);

        return model;
    }

    @Override
    protected void onPostExecute(List<CommentModel> results) {
        super.onPostExecute(results);
        CommentModel commentModel = new CommentModel();
        String content = commentModel.getContent();
        Toast.makeText(getApplicationContext(),content,Toast.LENGTH_LONG).show();


    }
}

2 个答案:

答案 0 :(得分:2)

检查

 @Override
protected void onPostExecute(List<CommentModel> results) {
    super.onPostExecute(results);
    if(results.size()>0){
    for(int i=0;i<results.size();i++){
        CommentModel commentModel = results.get(i);
        String content = commentModel.getContent();
        Toast.makeText(getApplicationContext(),content,Toast.LENGTH_LONG).show();
      }
    }
}

答案 1 :(得分:1)

您正在尝试从新创建的没有任何内容的CommentModel实例获取getContent。所以请从doinBackground结果中查看。

public class Gettingcomment extends AsyncTask<String,String,List<CommentModel>>{


@Override
protected List<CommentModel> doInBackground(String... params) {

    String commenturl = params[0];

    Populatecomments populatecomments =new Populatecomments();
   // populatecomments.getCommentModelList(commenturl+mytrendinid);

    Log.i("mytrendin",commenturl);

    List<CommentModel>  model= populatecomments.getCommentModelList(commenturl);

    return model;
}

@Override
protected void onPostExecute(List<CommentModel> results) {
    super.onPostExecute(results);
    if(results!=null && results.size()>0){
    //here i am getting getContent from 0 position of CommentModel list you can loop to get all the model's content
    String content = results.get(0).getContent();
    Toast.makeText(getApplicationContext(),content,Toast.LENGTH_LONG).show();
    }
}

}