我目前正在关注本教程(http://mobile.dzone.com/news/android-tutorial-how-parse)//如何将doddataitemObj中的项目添加到dataitem字符串中-字符串PUBLICEVENTTYPE = d.getString(PUBLICEVENTTYPE);说本地变量PUBLICEVENTTYPE可能尚未初始化。指向JSONObject d = dataitems.getJSONObject(i);。我相信它已定义。我也尝试过没有公共String d的运气。但这会产生我刚才提到的代码错误。

我将不胜感激。

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;


public class ParseJSON {

{

    // Hashmap for ListView
    ArrayList<HashMap<String, String>> dataList = new ArrayList<HashMap<String, String>>();

  //Get the data (public static JSONObject getJSONfromURL(String url))
  //JSONObject json = ParseJSON.getJSONfromURL("http://api.geonames.org/postalCodeSearchJSON?formatted=true&postalcode=9791&maxRows=10&username=demo&style=full");
  JSONObject json = ParseJSON.getJSONfromURL(Globals.ParseJSONurl);

         try{
    //Get the element that holds the dataitem ( JSONArray )
    JSONArray dataitems = json.getJSONArray("dataitem");

                    //Loop the Array
          for(int i=0;i < dataitems.length();i++){

            // creating new HashMap
              HashMap<String, String> map = new HashMap<String, String>();

            JSONObject d = dataitems.getJSONObject(i);

            // D not working ?

            // add items from doddataitemObj to dataitem string
            String PUBLICEVENTTYPE = d.getString(PUBLICEVENTTYPE);

            // adding each child node to HashMap key => value
            map.put(PUBLICEVENTTYPE, PUBLICEVENTTYPE);

            // adding HashList to ArrayList
            dataList.add(map);
    }
         }catch(Exception e)        {
             e.printStackTrace();
             Log.e("log_tag", "Error parsing data "+e.toString());
           }
         }

public static JSONObject getJSONfromURL(String url){

    //initialize
    InputStream is = null;
    String result = "";
    JSONObject jArray = null;

    //http post
    try{
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);
        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);
        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.e("log_tag", "Error converting result "+e.toString());
    }

    //try parse the string to a JSON object
    try{
            jArray = new JSONObject(result);
    }catch(JSONException e){
        Log.e("log_tag", "Error parsing data "+e.toString());
    }

    return jArray;
}
}

最佳答案

 String PUBLICEVENTTYPE = d.getString("PUBLICEVENTTYPE");


要么

 String publicEventType = d.getString("PUBLICEVENTTYPE"); // more conventionnal

09-30 12:50