问题描述:

This is my

I want to parse it but i am getting no value for theater how can i solve this problem?

My code is:

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) {

}

});

}

}

I want to parse it but i am getting no value for theater how can i solve this problem?

My code is:I want to parse it but i am getting no value for theater how can i solve this problem?

My code is:

pl help me to resolve error..

网友答案:

You are not assigning any value to the json object, you have to do it in Async Task to fetch the feed from your service.

网友答案:

As @Abed El Majeed K has already said, you need to do it as an async task. i've found that any downloading, or internet actions are required to be preformed on a separate thread & not on the MainUI.

An example:

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);
          ...
       }
   }
}

by running requests on AsyncTask, you are not causing the MainUiThread to pause, or wait for the download to complete.

With that said, the JSON source you are using is incorrect. JSON structure is like this:

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

here might help: http://json.org/

相关阅读:
Top