使用AsyncTask在ListView中获取json数据 [英] get json data in ListView with AsyncTask

查看:102
本文介绍了使用AsyncTask在ListView中获取json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用asyncTask在我的ListView中显示服务器中的一些数据.连接和JsonParser正常工作,但是当我尝试将数据放入ListView时,我的OnPostExecute()错误.

I would like to display some data from my server in my ListView with an asyncTask. The connection and JsonParser work well, but I have an error in my OnPostExecute(), when I try to put my data in the ListView.

因此,这是调用asynctask的类:

public class GetInfo extends Activity{ 

    ListView listeView;

    protected void onCreate(Bundle bundle)
    {
        super.onCreate(bundle);    
        setContentView(R.layout.listdata);   
        listeView = (ListView) findViewById(R.id.maListeView);     

        GetJson getJson = new GetJson(GetInfo.this);
        getJson.execute();           
    }
}

这里没有问题.这是我的课程AsyncTask:

Here there are no problems. This is my class AsyncTask:

class GetJson extends AsyncTask<Void, Integer,  ArrayList<String>> {

    private Context Mycontext;
    private ListView listview; 
    private ArrayAdapter<String> arrayadapter;
    private ProgressDialog pDialog;
    private ArrayList<String> donnees;
    private ListActivity listAct;

    public GetJson(GetInfo getInfo) {
        Mycontext = getInfo;
    }

    @Override
    protected void onPreExecute() {
        // Showing progress dialog before sending http request
        this.pDialog  = new ProgressDialog(Mycontext);
        this.pDialog.setMessage("Please wait..");
        this.pDialog.setIndeterminate(true);
        this.pDialog.setCancelable(false);
        this.pDialog.show();    
    }

    @Override
    protected  ArrayList<String> doInBackground(Void... arg0) {          
        String result = null;
        InputStream is = null;
        JSONObject json_data=null;
        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        this.donnees = new ArrayList<String>();

        try{
            //commmand http
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost("http://192.168.0.100/timesync.php");
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
        }
        catch(Exception e){
            Log.i("taghttppost",""+e.toString());           
        }

        //parse response
        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));         
            StringBuilder sb  = new StringBuilder();           
            String line = null;            
            while ((line = reader.readLine()) != null)
            {
                sb.append(line + "\n");
            }

            is.close();

            result = sb.toString();
        }
        catch(Exception e)
        {
            Log.i("tagconvertstr",""+e.toString());
        }        

        //get json data
        try{
            JSONArray jArray = new JSONArray(result);             
            for(int i=0;i<jArray.length();i++)
            {             
                json_data = jArray.getJSONObject(i);
                this.donnees.add("date :"+ json_data.getString("time") + " ou " +
                    json_data.getString("date") + json_data.getString("hour"));     
            }
        }
        catch(JSONException e){
            Log.i("tagjsonexp",""+e.toString());
        } catch (ParseException e) {
            Log.i("tagjsonpars",""+e.toString());
        }
        return this.donnees;
    }

    @Override
    protected void onPostExecute(ArrayList<String> donnees) {    
        super.onPostExecute(donnees);

        this.pDialog.dismiss();

        for(int i=0;i<donnees.size();i++)
        {             
            Log.i("time", donnees.get(i));
        } 

        // this.arrayadapter = new ArrayAdapter<String>(Mycontext, android.R.layout.simple_list_item_1, this.donnees);
        // this.listview.setAdapter(this.arrayadapter);
    }
}

我的问题是注释行:

// this.arrayadapter = new ArrayAdapter<String>(Mycontext, android.R.layout.simple_list_item_1, this.donnees);
// this.listview.setAdapter(this.arrayadapter);

我不明白为什么...

and I don't understand why ...

这是我的错误:

11-05 16:43:36.560: E/AndroidRuntime(11200): FATAL EXCEPTION: main
11-05 16:43:36.560: E/AndroidRuntime(11200): java.lang.NullPointerException
11-05 16:43:36.560: E/AndroidRuntime(11200):at com.json.GetJson.onPostExecute(Adapter.java:134)
11-05 16:43:36.560: E/AndroidRuntime(11200):at android.os.AsyncTask.finish(AsyncTask.java:631)
11-05 16:43:36.560: E/AndroidRuntime(11200):at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-05 16:43:36.560: E/AndroidRuntime(11200):at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)

.
.
.

正在记录结果,所以我很确定我的代码已传递到OnPostExecute().

The result is being logged, so I'm pretty sure that my code is passed into OnPostExecute().

推荐答案

问题是您的列表视图未初始化.

The problem is that your list view is not initialized.

步骤1)更新AsyncTask构造函数:

Step 1) Update the AsyncTask constructor:

public GetJson(GetInfo getInfo, ListView list) {
    Mycontext = getInfo;
    listView = list;
}

第2步)在OnCreate

GetJson getJson = new GetJson(GetInfo.this, listeView);
getJson.execute();

这篇关于使用AsyncTask在ListView中获取json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆