Hello im trying to retrieve some data from a website using JSON. I got some code from an tutorial but the tutorial didn't work. I thought it was becuase the url from the tutorial wasn't working, but when i tried a working url it still gives the same error. Can someone tell me what I'm doing wrong. I read everywhere that using JSON should be very simple and easy, but I really got an headache trying al the tutorials and reading al the examples.
Heres the code from the tutorial called Main.java:
public class Main extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listplaceholder);
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
//the website I use lacks some security so I don't wanne put it here.
//its still under construction.
JSONObject json = JSONfunctions.getJSONfromURL("http://somewebsite/jsonexample.html");
try{
JSONArray student = json.getJSONArray("student");
for(int i=0;i<student.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = student.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "Naam: " + e.getString("name"));
map.put("age", "Leeftijd: " + e.getString("age"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist , R.layout.main,
new String[] { "name", "age" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
final ListView lv = getListView();
lv.setTextFilterEnabled(true);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
@SuppressWarnings("unchecked")
HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position);
Toast.makeText(Main.this, "ID '" + o.get("id") + "' was clicked.", Toast.LENGTH_SHORT).show();
}
});
}
}
Here is the code from the tutorial called JSONfunctions.java:
public class JSONfunctions {
public static JSONObject getJSONfromURL(String url){
InputStream is = null;
String result = "";
JSONObject jArray = null;
//http post
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection "+e.toString());
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
jArray = new JSONObject(result);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return jArray;
}
}
the json:
{ "student":[{ "id" : "1", "name" : "Piet", "age" : "10" }{ "id" : "2", "name" : "Jaap", "age" : "6" }] }
And the logcat:
03-12 11:24:09.773: E/AndroidRuntime(911): FATAL EXCEPTION: main
03-12 11:24:09.773: E/AndroidRuntime(911): java.lang.RuntimeException: Unable to start activity ComponentInfo{me.mikey.my.games.galgjex/me.mikey.my.games.galgjex.Main}: java.lang.NullPointerException
03-12 11:24:09.773: E/AndroidRuntime(911): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
03-12 11:24:09.773: E/AndroidRuntime(911): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
03-12 11:24:09.773: E/AndroidRuntime(911): at android.app.ActivityThread.access$600(ActivityThread.java:130)
03-12 11:24:09.773: E/AndroidRuntime(911): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
03-12 11:24:09.773: E/AndroidRuntime(911): at android.os.Handler.dispatchMessage(Handler.java:99)
03-12 11:24:09.773: E/AndroidRuntime(911): at android.os.Looper.loop(Looper.java:137)
03-12 11:24:09.773: E/AndroidRuntime(911): at android.app.ActivityThread.main(ActivityThread.java:4745)
03-12 11:24:09.773: E/AndroidRuntime(911): at java.lang.reflect.Method.invokeNative(Native Method)
03-12 11:24:09.773: E/AndroidRuntime(911): at java.lang.reflect.Method.invoke(Method.java:511)
03-12 11:24:09.773: E/AndroidRuntime(911): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-12 11:24:09.773: E/AndroidRuntime(911): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-12 11:24:09.773: E/AndroidRuntime(911): at dalvik.system.NativeStart.main(Native Method)
03-12 11:24:09.773: E/AndroidRuntime(911): Caused by: java.lang.NullPointerException
03-12 11:24:09.773: E/AndroidRuntime(911): at me.mikey.my.games.galgjex.Main.onCreate(Main.java:41)
03-12 11:24:09.773: E/AndroidRuntime(911): at android.app.Activity.performCreate(Activity.java:5008)
03-12 11:24:09.773: E/AndroidRuntime(911): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
03-12 11:24:09.773: E/AndroidRuntime(911): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
03-12 11:24:09.773: E/AndroidRuntime(911): ... 11 more
Changed json to: { "student":[{ "id" : "1", "name" : "Piet", "age" : "10" },{ "id" : "2", "name" : "Jaap", "age" : "6" }] } But it still gives an NPE. Or is my json still invalit?