0
我已經看到和其他相關帖子,但我不明白哪個地方的代碼我必須改變。如果有人有一些想法這是什麼,我應該改變代碼,謝謝。以下是MainActivity和個XML等JSON解析與列表視圖?
MainActivity
package com.example.dell.json;
import android.app.ListActivity;
....
import java.util.HashMap;
public class MainActivity extends ListActivity {
private ProgressDialog pDialog;
// URL to get contacts JSON
private static String url = "{\n" +
" \"contacts\": [\n" +
" {\n" +
" \"id\": \"c200\",\n" +
" \"name\": \"Ravi Tamada\",\n" +
" \"email\": \"[email protected]\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c201\",\n" +
" \"name\": \"Johnny Depp\",\n" +
" \"email\": \"[email protected]\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c202\",\n" +
" \"name\": \"Leonardo Dicaprio\",\n" +
" \"email\": \"[email protected]\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n" +
" {\n" +
" \"id\": \"c203\",\n" +
" \"name\": \"John Wayne\",\n" +
" \"email\": \"[email protected]\",\n" +
" \"address\": \"xx-xx-xxxx,x - street, x - country\",\n" +
" \"gender\" : \"male\",\n" +
" \"phone\": {\n" +
" \"mobile\": \"+91 0000000000\",\n" +
" \"home\": \"00 000000\",\n" +
" \"office\": \"00 000000\"\n" +
" }\n" +
" },\n";
// JSON Node names
private static final String TAG_CONTACTS = "contacts";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "name";
private static final String TAG_EMAIL = "email";
private static final String TAG_ADDRESS = "address";
private static final String TAG_GENDER = "gender";
private static final String TAG_PHONE = "phone";
private static final String TAG_PHONE_MOBILE = "mobile";
private static final String TAG_PHONE_HOME = "home";
private static final String TAG_PHONE_OFFICE = "office";
// contacts JSONArray
JSONArray contacts = null;
// Hashmap for ListView
ArrayList<HashMap<String, String>> contactList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contactList = new ArrayList<HashMap<String, String>>();
ListView lv = getListView();
// Listview on item click listener
lv.setOnItemClickListener(new AdapterView.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 cost = ((TextView) view.findViewById(R.id.email))
.getText().toString();
String description = ((TextView) view.findViewById(R.id.mobile))
.getText().toString();
// Starting single contact activity
Intent in = new Intent(getApplicationContext(),
SingleContactActivity.class);
in.putExtra(TAG_NAME, name);
in.putExtra(TAG_EMAIL, cost);
in.putExtra(TAG_PHONE_MOBILE, description);
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<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(MainActivity.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(TAG_CONTACTS);
// looping through All Contacts
for (int i = 0; i < contacts.length(); i++) {
JSONObject c = contacts.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String email = c.getString(TAG_EMAIL);
String address = c.getString(TAG_ADDRESS);
String gender = c.getString(TAG_GENDER);
// Phone node is JSON Object
JSONObject phone = c.getJSONObject(TAG_PHONE);
String mobile = phone.getString(TAG_PHONE_MOBILE);
String home = phone.getString(TAG_PHONE_HOME);
String office = phone.getString(TAG_PHONE_OFFICE);
// tmp hashmap for single contact
HashMap<String, String> contact = new HashMap<String, String>();
// adding each child node to HashMap key => value
contact.put(TAG_ID, id);
contact.put(TAG_NAME, name);
contact.put(TAG_EMAIL, email);
contact.put(TAG_PHONE_MOBILE, mobile);
// 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(
MainActivity.this, contactList,
R.layout.list_item, new String[] { TAG_NAME, TAG_EMAIL,
TAG_PHONE_MOBILE }, new int[] { R.id.name,
R.id.email, R.id.mobile });
setListAdapter(adapter);
}
}
}
的代碼和以下是錯誤:
10-06 15:04:27.627 19502-19502/? D/ActivityThread: ACT-PAUSE_ACTIVITY handled : 0/[email protected]
10-06 15:04:27.650 19502-19522/? I/System.out: httpget:{
10-06 15:04:27.650 19502-19522/? I/System.out: "contacts": [
10-06 15:04:27.650 19502-19522/? I/System.out: {
10-06 15:04:27.650 19502-19522/? I/System.out: "id": "c200",
10-06 15:04:27.650 19502-19522/? I/System.out: "name": "Ravi Tamada",
10-06 15:04:27.650 19502-19522/? I/System.out: "email": "[email protected]",
10-06 15:04:27.650 19502-19522/? I/System.out: "address": "xx-xx-xxxx,x - street, x - country",
10-06 15:04:27.650 19502-19522/? I/System.out: "gender" : "male",
10-06 15:04:27.650 19502-19522/? I/System.out: "phone": {
10-06 15:04:27.650 19502-19522/? I/System.out: "mobile": "+91 0000000000",
10-06 15:04:27.650 19502-19522/? I/System.out: "home": "00 000000",
10-06 15:04:27.650 19502-19522/? I/System.out: "office": "00 000000"
10-06 15:04:27.650 19502-19522/? I/System.out: }
10-06 15:04:27.650 19502-19522/? I/System.out: },
10-06 15:04:27.650 19502-19522/? I/System.out: {
10-06 15:04:27.650 19502-19522/? I/System.out: "id": "c201",
10-06 15:04:27.650 19502-19522/? I/System.out: "name": "Johnny Depp",
10-06 15:04:27.650 19502-19522/? I/System.out: "email": "[email protected]",
10-06 15:04:27.650 19502-19522/? I/System.out: "address": "xx-xx-xxxx,x - street, x - country",
10-06 15:04:27.650 19502-19522/? I/System.out: "gender" : "male",
10-06 15:04:27.650 19502-19522/? I/System.out: "phone": {
10-06 15:04:27.650 19502-19522/? I/System.out: "mobile": "+91 0000000000",
10-06 15:04:27.650 19502-19522/? I/System.out: "home": "00 000000",
10-06 15:04:27.650 19502-19522/? I/System.out: "office": "00 000000"
10-06 15:04:27.650 19502-19522/? I/System.out: }
10-06 15:04:27.650 19502-19522/? I/System.out: },
....
10-06 15:04:27.651 19502-19522/? I/System.out: },
10-06 15:04:27.658 19502-19522/? D/dalvikvm: create interp thread : stack size=128KB
10-06 15:04:27.658 19502-19522/? D/dalvikvm: create new thread
10-06 15:04:27.659 19502-19522/? D/dalvikvm: new thread created
10-06 15:04:27.659 19502-19522/? D/dalvikvm: update thread list
10-06 15:04:27.659 19502-19524/? D/dalvikvm: threadid=12: interp stack at 0x6143d000
10-06 15:04:27.660 19502-19524/? D/dalvikvm: threadid=12: created from interp
10-06 15:04:27.667 19502-19522/? W/System.err: Caused by: java.lang.IllegalArgumentException: Illegal character in scheme at index 0: {
10-06 15:04:27.668 19502-19522/? W/System.err: %20%20%20%20"contacts":%20[
10-06 15:04:27.668 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20{
10-06 15:04:27.669 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"id":%20"c200",
10-06 15:04:27.669 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"name":%20"Ravi%20Tamada",
10-06 15:04:27.669 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"email":%20"[email protected]",
10-06 15:04:27.669 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"address":%20"xx-xx-xxxx,x%20-%20street,%20x%20-%20country",
10-06 15:04:27.669 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"gender"%20:%20"male",
10-06 15:04:27.670 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"phone":%20{
10-06 15:04:27.670 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"mobile":%20"+91%200000000000",
10-06 15:04:27.670 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"home":%20"00%20000000",
10-06 15:04:27.671 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"office":%20"00%20000000"
10-06 15:04:27.671 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20}
...
10-06 15:04:27.674 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"email":%20"[email protected]",
10-06 15:04:27.675 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"address":%20"xx-xx-xxxx,x%20-%20street,%20x%20-%20country",
10-06 15:04:27.675 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"gender"%20:%20"male",
10-06 15:04:27.675 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"phone":%20{
10-06 15:04:27.675 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"mobile":%20"+91%200000000000",
10-06 15:04:27.675 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"home":%20"00%20000000",
10-06 15:04:27.675 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"office":%20"00%20000000"
10-06 15:04:27.678 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20}
10-06 15:04:27.678 19502-19522/? W/System.err: %20%20%20%20%20%20%20%20},
10-06 15:04:27.683 19502-19522/? W/System.err: at java.net.URI.create(URI.java:730)
10-06 15:04:27.684 19502-19522/? W/System.err: at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:83)
10-06 15:04:27.684 19502-19522/? W/System.err: at com.example.dell.json.ServiceHandler.makeServiceCall(ServiceHandler.java:68)
10-06 15:04:27.685 19502-19522/? W/System.err: at com.example.dell.json.ServiceHandler.makeServiceCall(ServiceHandler.java:34)
10-06 15:04:27.685 19502-19522/? W/System.err: at com.example.dell.json.MainActivity$GetContacts.doInBackground(MainActivity.java:263)
10-06 15:04:27.687 19502-19522/? W/dalvikvm: threadid=11: calling UncaughtExceptionHandler
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: Process: com.example.dell.json, PID: 19502
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: java.lang.RuntimeException: An error occured while executing doInBackground()
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: Caused by: java.lang.IllegalArgumentException: Illegal character in scheme at index 0: {
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20"contacts":%20[
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20{
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"id":%20"c200",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"name":%20"Ravi%20Tamada",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"email":%20"[email protected]",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"address":%20"xx-xx-xxxx,x%20-%20street,%20x%20-%20country",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"gender"%20:%20"male",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"phone":%20{
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"mobile":%20"+91%200000000000",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"home":%20"00%20000000",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"office":%20"00%20000000"
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20}
...
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"mobile":%20"+91%200000000000",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"home":%20"00%20000000",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"office":%20"00%20000000"
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20}
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20},
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20{
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"id":%20"c202",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"name":%20"Leonardo%20Dicaprio",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"email":%20"[email protected]",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"address":%20"xx-xx-xxxx,x%20-%20street,%20x%20-%20country",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"gender"%20:%20"male",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20}
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20},
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20{
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20"id":%20"c203",
10-06 15:04:27.690 19502-19522/? E/AndroidRuntime: %20%20%20%20
10-06 15:04:27.691 19502-19502/? E/: appName=com.example.dell.json, acAppName=/system/bin/surfaceflinger
10-06 15:04:27.691 19502-19502/? E/: 0
10-06 15:04:27.691 19502-19502/? E/: appName=com.example.dell.json, acAppName=/system/bin/surfaceflinger
10-06 15:04:27.691 19502-19502/? E/: 0
10-06 15:04:27.696 19502-19502/? D/GraphicBuffer: create handle(0x5d088eb0) (w:528, h:144, f:1)
10-06 15:04:27.697 19502-19502/? I/MaliEGL: [Mali]window_type=1, is_framebuffer=0, errnum = 0
10-06 15:04:27.697 19502-19502/? I/MaliEGL: [Mali]surface->num_buffers=4, surface->num_frames=3, win_min_undequeued=1
10-06 15:04:27.697 19502-19502/? I/MaliEGL: [Mali]max_allowed_dequeued_buffers=3
10-06 15:04:27.699 19502-19502/? D/GraphicBuffer: close handle(0x5d088eb0) (w:528 h:144 f:1)
10-06 15:04:27.700 19502-19502/? D/GraphicBuffer: create handle(0x5d088eb0) (w:528, h:144, f:1)
10-06 15:04:27.703 19502-19522/? W/Process: killProcess [19502]
10-06 15:04:27.703 19502-19502/? D/OpenGLRenderer: Enabling debug mode 0
10-06 15:04:27.704 19502-19522/? W/System.err: java.lang.Throwable
10-06 15:04:27.705 19502-19502/? D/GraphicBuffer: create handle(0x619cf8e0) (w:768, h:768, f:1)
10-06 15:04:27.708 19502-19502/? D/OpenGLRenderer: setViewport 513x144 <0x619cd6d8>
10-06 15:04:27.715 19502-19522/? W/System.err: at android.os.Process.killProcess(Process.java:957)
10-06 15:04:27.717 19502-19522/? W/System.err: at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:94)
10-06 15:04:27.717 19502-19522/? W/System.err: at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:693)
10-06 15:04:27.717 19502-19522/? W/System.err: at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:690)
10-06 15:04:27.717 19502-19522/? I/Process: Sending signal. PID: 19502 SIG: 9
from your log'10-06 15:04:27.690 19502-19522 /? E/AndroidRuntime:導致:java.lang.IllegalArgumentException:在索引0的方案中的非法字符:' –