如何从返回类型void doInBackground获取数据到android中的postexecute JSON解析

时间:2014-06-06 19:18:34

标签: android json facebook

//大家好,我的问题是我的doinbackground是一个void类型所以我想从它获取数据到postexecute,有人能给我一些建议如何将数据从doinbackground传递到postexecute吗?

        @Override
        protected Void doInBackground(Session... params) {


            Request r =  new Request(params[0], "me/home");

            Response res = Request.executeAndWait(r);
            try {
            GraphObject go = res.getGraphObject();


        JSONObject json = go.getInnerJSONObject();
        JSONArray jArray;

            jArray = json.getJSONArray("data");

        for (int i = 0; i < jArray.length(); i++) {
            JSONObject currObj = jArray.getJSONObject(i);
             userId = currObj.getString("id");
            if (currObj.has("message")) {
                userMessage = currObj.getString("message");
            }
            else if(currObj.has("story")){
                userMessage = currObj.getString("story");
            } else{
                userMessage = "no message/story set";
            }
            JSONObject fromObj = currObj
                    .getJSONObject("from");
            String from = fromObj.getString("name");

            HashMap<String, String>newslist = new HashMap<String, String>();
            newslist.put(id, userId);
            newslist.put(message, userMessage);
            newslist.put(name, name);

            list.add(newslist);


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



            //Log.i("result", go.toString()); //HERE
            return null;
        }

//这是我的POSTEXECUTE

protected void onPostExecute(String result){

        BaseAdapter adapter = new SimpleAdapter(getActivity(), list, R.layout.list_item, 
                new String[]{id, message , name}, new int[] {R.id.textView2, R.id.textView3, R.id.textView4});
            Log.i("LISTOFUSERID", userId);
            listView.setAdapter(adapter);
            adapter.notifyDataSetChanged();

        Log.i("message ni tonting", userMessage);
        if (pDialog.isShowing())
            pDialog.dismiss();

    }

}

1 个答案:

答案 0 :(得分:0)

doInBackground()任务完成后,将自动调用onPostExecute()任务。 doInBackGround()方法将返回doInBackground()中完成的后台进程的结果。

在您的代码中,您可以更改doInBackGround()

末尾的返回部分
catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



        //Log.i("result", go.toString()); //HERE
        return result;    

然后你可以在onPostExecute()任务中访问像

这样的参数的结果
protected void onPostExecute(String result){ //Can use any data types
        super.onPostExecute(result);