voici mon problem j'essaie de me connecter a ma base de donnée de reupérer un json array et le mette dans une liste view mais ça marche pas
voila le code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| package com.androidhive.jsonparsing;
import java.util.ArrayList;
import java.util.HashMap;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
public class AndroidJSONParsingActivity extends ListActivity {
// url to make request
private static String url = "http://192.168.1.3/ville/ville.php";
// JSON Node names
private static final String TAG_CLIENT = "client";
private static final String TAG_ID = "Code_client";
private static final String TAG_NOM = "Nom";
private static final String TAG_PRENOM = "PRENOM";
private static final String TAG_SOCIET = "Societé";
private static final String TAG_ADRESS = "Adresse";
private static final String TAG_VILLE = "Ville";
private static final String TAG_TEL = "Tel";
private static final String TAG_FAX = "fax";
private static final String TAG_GSM = "Gsm";
private static final String TAG_EMAIL = "Email";
JSONArray client = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> clientList = new ArrayList<HashMap<String, String>>();
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(url);
try {
// Getting Array of Contacts
client = json.getJSONArray("client");
// looping through All Contacts
for(int i = 0; i < client.length(); i++){
JSONObject c = client.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_ID);
String nom = c.getString(TAG_NOM);
String prenom = c.getString(TAG_PRENOM);
String societe = c.getString(TAG_SOCIET);
String address = c.getString(TAG_ADRESS);
String ville = c.getString(TAG_VILLE);
String tel = c.getString(TAG_TEL);
String fax = c.getString(TAG_FAX);
String gsm = c.getString(TAG_GSM);
String email = c.getString(TAG_EMAIL);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_ID, id);
map.put(TAG_NOM, nom);
map.put(TAG_EMAIL, email);
map.put(TAG_TEL, tel);
// adding HashList to ArrayList
clientList.add(map);
}
} catch (JSONException e) {
e.printStackTrace();
}
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(this, clientList,
R.layout.list_item,
new String[] { TAG_NOM, TAG_EMAIL, TAG_TEL }, new int[] {
R.id.name, R.id.email, R.id.mobile });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// Launching new screen on Selecting Single ListItem
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 cost = ((TextView) view.findViewById(R.id.email)).getText().toString();
String description = ((TextView) view.findViewById(R.id.mobile)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(TAG_NOM, name);
in.putExtra(TAG_EMAIL, cost);
in.putExtra(TAG_TEL, description);
startActivity(in);
}
});
}
} |
voila la format json recupérer:
{"client":[{"Code_client":"3","Nom":"jhon","Prenom":"smith",null:"noname","Adresse":"nonamekk","Ville":"kqjldjq","Tel":"22520225","Fax":"2255565","Gsm":"2545555","Email":"jjhkjqh@hotmail.com","Descrition_complementaires":"qsvwdsvwxsv"},{"Code_client":"4","Nom":"jhon","Prenom":"smith",null:"noname","Adresse":"nonamekk","Ville":"kqjldjq","Tel":"22520225","Fax":"2255565","Gsm":"2545555","Email":"jjhkjqh@hotmail.com","Descrition_complementaires":"qsvwdsvwxsv"},{"Code_client":"5","Nom":"jhon","Prenom":"smith",null:"noname","Adresse":"nonamekk","Ville":"kqjldjq","Tel":"22520225","Fax":"2255565","Gsm":"2545555","Email":"jjhkjqh@hotmail.com","Descrition_complementaires":"qsvwdsvwxsv"},{"Code_client":"6","Nom":"jhon","Prenom":"smith",null:"noname","Adresse":"nonamekk","Ville":"kqjldjq","Tel":"22520225","Fax":"2255565","Gsm":"2545555","Email":"jjhkjqh@hotmail.com","Descrition_complementaires":"qsvwdsvwxsv"}]}
et voila le logcat:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| 05-14 23:17:12.929: W/dalvikvm(357): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-14 23:17:12.940: E/AndroidRuntime(357): FATAL EXCEPTION: main
05-14 23:17:12.940: E/AndroidRuntime(357): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.androidhive.jsonparsing/com.androidhive.jsonparsing.AndroidJSONParsingActivity}: java.lang.NullPointerException
05-14 23:17:12.940: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-14 23:17:12.940: E/AndroidRuntime(357): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-14 23:17:12.940: E/AndroidRuntime(357): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-14 23:17:12.940: E/AndroidRuntime(357): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-14 23:17:12.940: E/AndroidRuntime(357): at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 23:17:12.940: E/AndroidRuntime(357): at android.os.Looper.loop(Looper.java:123)
05-14 23:17:12.940: E/AndroidRuntime(357): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-14 23:17:12.940: E/AndroidRuntime(357): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 23:17:12.940: E/AndroidRuntime(357): at java.lang.reflect.Method.invoke(Method.java:521)
05-14 23:17:12.940: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-14 23:17:12.940: E/AndroidRuntime(357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-14 23:17:12.940: E/AndroidRuntime(357): at dalvik.system.NativeStart.main(Native Method)
05-14 23:17:12.940: E/AndroidRuntime(357): Caused by: java.lang.NullPointerException
05-14 23:17:12.940: E/AndroidRuntime(357): at com.androidhive.jsonparsing.AndroidJSONParsingActivity.onCreate(AndroidJSONParsingActivity.java:59)
05-14 23:17:12.940: E/AndroidRuntime(357): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-14 23:17:12.940: E/AndroidRuntime(357): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-14 23:17:12.940: E/AndroidRuntime(357): ... 11 more |
Partager