如何在为listview适配器发布数据之前解析json

时间:2014-12-16 11:52:17

标签: android json http-post

我正在尝试为我的Android应用程序制作json解析器。

我需要在我的listview适配器中获取json对象后发布一些数据。我使用此代码但它总是出错。我真的很努力,因为我改变了很多次,但我不能这样做。 (我可以轻松获取json数据,但在这种情况下,我想在JSON解析之前发帖。)我应该如何编辑我的代码?感谢您提供任何帮助,文件。

我收到此错误:

 java.lang.RuntimeException: An error occured while executing doInBackground()

这是我的代码:

 public class veritabaniKayit extends AsyncTask<String, Integer, JSONObject> {

    private ProgressDialog pDialog;
    JSONObject json2;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        yayDescription = (TextView)findViewById(R.id.yayDescription);
        yayDistance = (TextView)findViewById(R.id.yayDistance);
        yayCreatedTime = (TextView)findViewById(R.id.yayCreatedTime);
        pDialog = new ProgressDialog(HomeListview.this);
        pDialog.setMessage("Wait a min ...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected JSONObject doInBackground(String... params) {
        // TODO Auto-generated method stub
        postData(params[0],params[1],params[2]);
        JSONParser jParser = new JSONParser();
        // Getting JSON from URL
        url = "11.22.11.22/messages/list";
        System.out.println("THISISMYURL"+url);
        json2 = jParser.getJSONFromUrl(url);
        return json2;
    }

    protected void onPostExecute(JSONObject result){
        // pb.setVisibility(View.GONE);
    }

    protected void onProgressUpdate(Integer... progress){
        // pb.setProgress(progress[0]);
    }

    public void postData(String personName, String username, String email) {
        // Create a new HttpClient and Post Header
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://11.22.11.22/messages/list");
        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("token", "zyob3xkdnv"));
            nameValuePairs.add(new BasicNameValuePair("lat", "37"));
            nameValuePairs.add(new BasicNameValuePair("long", "-122"));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);

            // String json2 = EntityUtils.toString(response.getEntity());

           /* JSONObject jObject = null;
            try {
                jObject = new JSONObject(json);
                System.out.println("jObject"+jObject);
                String tokenValue=jObject.getString("token");
                System.out.println("tokenValue"+tokenValue);

                edit = pref.edit();
                edit.putString("sharedUsername", personName.toString());
                edit.putString("sharedEmail", email.toString());
                edit.putString("sharedToken", tokenValue);
                edit.commit();
            }

            catch (JSONException e) {
                e.printStackTrace();
            } */

            try {
                JSONObject json2;
                JSONParser jParser = new JSONParser();
                json2 = jParser.getJSONFromUrl(url);
                // Getting JSON Array from URL
                android = json2.getJSONArray(TAG_OS);
                for(int i = 0; i < android.length(); i++){
                    JSONObject c = android.getJSONObject(i);
                    // String  JSON item in a Variable
                    String yayDescription = c.getString(text);
                    String yayDistance = c.getString(distance);
                    String yayCreatedTime = c.getString(created_at);
                    // Adding value HashMap key => value
                    HashMap<String, String> map = new HashMap<String, String>();
                    map.put(text, yayDescription);
                    map.put(distance, yayDistance);
                    map.put(created_at, yayCreatedTime);
                    oslist.add(map);
                    myOwnPost =(ListView)findViewById(R.id.listView);
                    myOwnPost.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
                    // myOwnPost.setRotation(-90);
                    ListAdapter adapter = new SimpleAdapter(HomeListview.this, oslist,
                            R.layout.homelistviewitem,
                            new String[] { text, distance, created_at }, new int[] { R.id.yayDescription,R.id.yayDistance, R.id.yayCreatedTime });
                    myOwnPost.setAdapter(adapter);
                    myOwnPost.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view,
                                                int position, long id) {
                            String whatIsSoundName = oslist.get(+position).get("yayDescription");
                            Toast.makeText(HomeListview.this, "You Clicked at " + position, Toast.LENGTH_SHORT).show();
                        }
                    });
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            pDialog.dismiss();

            int responseCode = response.getStatusLine().getStatusCode();

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block

        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试将此代码放入

 @Override
 protected void onPostExecute(String result) {

 myOwnPost =(ListView)findViewById(R.id.listView);
                myOwnPost.setChoiceMode(ListView.CHOICE_MODE_SINGLE);ListAdapter adapter = new SimpleAdapter(HomeListview.this, oslist,
                        R.layout.homelistviewitem,
                        new String[] { text, distance, created_at }, new int[] { R.id.yayDescription,R.id.yayDistance, R.id.yayCreatedTime });
                myOwnPost.setAdapter(adapter);
                myOwnPost.setOnItemClickListener(new AdapterView.OnItemClickListener(){
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view,
                                            int position, long id) {
                        String whatIsSoundName = oslist.get(+position).get("yayDescription");
                        Toast.makeText(HomeListview.this, "You Clicked at " + position, Toast.LENGTH_SHORT).show();
                    }
                });
 }