json解析中没有值错误

时间:2014-07-04 12:37:09

标签: android json

这是我的 我想解析它,但我对剧院没有任何价值我怎么能解决这个问题? 我的代码是:

CustomizedListView.java

public class CustomizedListView extends Activity {
    // All static variables
    static final String URL ="";

    // XML node keys

    JSONArray theaters = null;
    JSONObject json = null;
    ListView list;
    LazyAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


        ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();
         json=new JSONObject();
        try {
            // Getting Array of Contacts

            theaters = json.getJSONArray(KEY_SONG);
            // looping through All Contacts
            for(int i = 0; i < theaters.length(); i++){
                JSONObject c = theaters.getJSONObject(i);

                // Storing each json item in variable
                String name = c.getString(KEY_TITLE);
                String distance = c.getString(KEY_DURATION);
                String pincode = c.getString(KEY_ARTIST);
                //String image = c.getString(TAG_MOVIEIMAGE);

                // Phone number is agin JSON Object


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

                // adding each child node to HashMap key => value

                map1.put(KEY_TITLE,distance);
                map1.put(KEY_DURATION,pincode);
                map1.put(KEY_ARTIST,name);
                //map.put(TAG_MOVIEIMAGE, image);

                // adding HashList to ArrayList
                songsList.add(map1);
            }
        } catch (JSONException e) {
            e.printStackTrace();
            System.out.println("this is error" +e);
        }

            // adding HashList to ArrayList



        list=(ListView)findViewById(R.id.list);

        // Getting adapter by passing xml data ArrayList
        adapter=new LazyAdapter(this, songsList);        
        list.setAdapter(adapter);


        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {


            }
        });     
    }   
}

我想解析它,但我对剧院没有任何价值我怎么能解决这个问题? 我的代码是:我想解析它,但我对剧院没有价值,我怎么能解决这个问题? 我的代码是:

帮我解决错误..

2 个答案:

答案 0 :(得分:0)

您没有为json对象分配任何值,您必须在Async任务中执行此操作以从您的服务中获取Feed。

答案 1 :(得分:0)

正如@Abed El Majeed K已经说过的那样,你需要将它作为异步任务来完成。 我发现任何下载或互联网动作都需要在一个单独的线程上进行。不在MainUI上。

一个例子:

public class task extends AsyncTask<String, Void, String>{

    private StringBuilder inputStreamToString(InputStream is){
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedRead rd = new Buffer3edReader(new InputStreamReader(is));

        while((rLine = rd.readLine()) != null){
            answer.append(rLine);
        }

     return answer;
     }

    protected String doInBackground(String... param){
       String uRL = "http://example.com";

       URL url = new URL(uRL);
       HttpClient httpclient = new DefaultHttpClient();
       HttpPost httppost = new HttpPost(url);
       HttpResponse response = httpclient.execute(httppost);
       String jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
       return jsonResult;
   }

   protected void onPostExecute(String result){
       JSONObject jsonResponse = new JSONObject(result);
       JSONArray jsonNode = jsonResponse.getJSONArray(whatever_your_json_stream_is_called);
       for(int i = 0; i < jsonMainNode.length(); i++){
          JSONObject jsonchild = jsonNode.getJSONObject(i);
          String some_value = jsonchild.getString(some_key);
          ...
       }
   }
}

通过在AsyncTask上运行请求,您不会导致MainUiThread暂停,或等待下载完成。

话虽如此,您使用的JSON源是不正确的。 JSON结构是这样的:

{"employees":[
    {"firstName":"John", "lastName":"Doe"}, 
    {"firstName":"Anna", "lastName":"Smith"},
    {"firstName":"Peter", "lastName":"Jones"}
]} 

这可能会有所帮助:http://json.org/