我有一个Android应用程序,我想通过它读取json以下的json。
[{"name":"ABCD","country":"INDIA","twitter":"abc"},{"name":"nehasharma","country":"india","twitter":"indore"},{"name":"Arif","country":"India","twitter":"@sdf"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"gsutam","country":"india","twitter":"hwello"},{"name":"bawender","country":"18","twitter":null},{"name":"dddd","country":"india","twitter":"sdtt"},{"name":"dddd","country":"india","twitter":"sdtt"},{"name":"dddd","country":"india","twitter":"fghjj"},{"name":"je","country":"xe","twitter":"@rtttt.com\n"},{"name":"ajh","country":"eyohio","twitter":"mp"},{"name":"hasan","country":"jsjs","twitter":"snsns"},{"name":"13.3738383383","country":"38.3838383829","twitter":"location"},{"name":"Latitude:13.07136399","country":"Longitude:77.55943079","twitter":"Current Location"},{"name":"Latitude:13.07136399","country":"Longitude:77.55943079","twitter":"Current Location"},{"name":"Latitude:13.07136399","country":"Longitude:77.55943079","twitter":"Current Location"},{"name":"13.07136399","country":"77.55943079","twitter":"Current Location"},{"name":"13.07136399","country":"77.55943079","twitter":"Current Location"}]
Here is the android code.
这是android代码。
public class Cultural extends ListActivity {
private ProgressDialog pDialog;
private static String url = "http://hmkcode.appspot.com/jsonservlet";
private static final String NAME = "name";
private static final String COUNTRY = "country";
private static final String TWITTER = "twitter";
// contacts JSONArray
JSONArray contacts = null;
// Hashmap for ListView
ArrayList> contactList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cultural);
contactList = new ArrayList>();
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView)view.findViewById(R.id.name)).getText().toString();
String country = ((TextView) view.findViewById(R.id.time)).getText().toString();
String twitter = ((TextView) view.findViewById(R.id.venue)).getText().toString();
Intent in = new Intent(getApplicationContext(),SingleEventActivity.class);
in.putExtra(NAME, name);
in.putExtra(COUNTRY, country);
in.putExtra(TWITTER, twitter);
startActivity(in);
}
});
// Calling async task to get json
new GetContacts().execute();
}
/**
* Async task class to get json by making HTTP call
* */
private class GetContacts extends AsyncTask {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Cultural.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
// Creating service handler class instance
ServiceHandler sh = new ServiceHandler();
// Making a request to url and getting response
String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
Log.d("Response: ", "> " + jsonStr);
if (jsonStr != null) {
try {
JSONObject jsonObj = new JSONObject(jsonStr);
// Getting JSON Array node
// contacts = jsonObj.getJSONArray(NAME);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String name = c.getString(NAME);
String time = c.getString(COUNTRY);
String twitter=c.getString(TWITTER);
// Phone node is JSON Object
// tmp hashmap for single contact
HashMap contact = new HashMap();
// adding each child node to HashMap key => value
contact.put(NAME, name);
contact.put(COUNTRY, time);
contact.put(TWITTER,twitter);
//contact.put(TAG_ADDRESS, address);
// adding contact to contact list
contactList.add(contact);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
Log.e("ServiceHandler", "Couldn't get any data from the url");
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(Cultural.this, contactList,R.layout.list_item, new String[] { NAME, COUNTRY,TWITTER }, new int[] { R.id.name,R.id.time, R.id.venue});
setListAdapter(adapter);
}
}
}
Whenever i try to run the app, i get a black screen and i get the following information in console: JSONArray cannot be converted to JSONObject error.
每当我尝试运行应用程序时,我得到一个黑屏,我在控制台中获得以下信息:JSONArray无法转换为JSONObject错误。
The data is getting downloaded and i am able to see it in console.
数据正在下载,我可以在控制台中看到它。
Where am i going wrong here ?
我在哪里错了?