JSON Probleme

  • Antworten:5
Kevin W.
  • Forum-Beiträge: 30

10.05.2011, 17:20:35 via Website

Hallo Leute, ich hab ein kleines Problem ich hab schon öfters mit JSON in PHP und JS Programmiert aber in Android komm ich nicht klar.

Zuerst mal mein Code:
1package de.kevins391;
2
3import android.app.Activity;
4import android.os.Bundle;
5import org.apache.http.HttpResponse;
6import org.apache.http.client.HttpClient;
7import org.apache.http.client.methods.HttpGet;
8import org.apache.http.impl.client.DefaultHttpClient;
9import org.apache.http.HttpEntity;
10import java.io.BufferedReader;
11import java.io.InputStream;
12import java.io.InputStreamReader;
13import org.apache.http.client.ClientProtocolException;
14import java.io.IOException;
15import android.widget.TextView;
16import android.util.Log;
17import org.json.JSONArray;
18import org.json.JSONObject;
19import org.json.JSONException;
20
21public class Start extends Activity {
22 /** Called when the activity is first created. */
23 String app_name;
24
25 @Override
26 public void onCreate(Bundle savedInstanceState) {
27 super.onCreate(savedInstanceState);
28 setContentView(R.layout.main);
29
30 app_name = "kevins391-log";
31 getJSONObject("http://mobile.meineseite.de/users.php?id=73", app_name);
32 }
33
34
35 public void getJSONObject(String url, String app_name) {
36 HttpClient httpClient = new DefaultHttpClient();
37 HttpGet httpGet = new HttpGet(url);
38 HttpResponse response;
39
40 try {
41 response = httpClient.execute(httpGet);
42 HttpEntity entity = response.getEntity();
43
44 Log.i(app_name, response.getStatusLine().toString());
45
46 if(entity != null) {
47 InputStream instream = entity.getContent();
48 BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
49 StringBuilder sb = new StringBuilder();
50 String line = null;
51
52 while ((line = reader.readLine()) != null) {
53 sb.append(line + "n");
54 }
55
56 String result = sb.toString();
57 Log.i(app_name, result);
58 instream.close();
59
60 try{
61 JSONObject json = new JSONObject(result);
62 String uid = json.getJSONObject("user_id").toString();
63
64 Log.i(app_name, uid);
65 }catch(JSONException e){
66 Log.e(app_name, "Error parsing data " + e.toString());
67 }
68 }
69 } catch(ClientProtocolException e) {
70 e.printStackTrace();
71 } catch(IOException e) {
72 e.printStackTrace();
73 } catch(Exception e) {
74 e.printStackTrace();
75 } finally {
76 httpGet.abort();
77 Log.i(app_name, "Connection error: " + url);
78 }
79 }
80}

Er gibt mir diese Meldung aus:
105-10 17:18:05.081: ERROR/kevins391-log(11049): Error parsing data org.json.JSONException: Value 73 at user_id of type java.lang.String cannot be converted to JSONObject

Mein JSON Code sieht so aus:
1{"user_id":"73","username":"sxbi","name":"Sebastian X.","email":"sebastian.x@meinemail.com","cb_geschlecht":"men","cb_nationalitaet":"ger"}

Was mach ich falsch?
Bitte schickt mir jetzt nicht den link zu android development seite, da versteh ich nur bahnhof.

Danke
Gruß Kevin

Antworten
Gelöschter Account
  • Forum-Beiträge: 80

10.05.2011, 17:28:03 via Website

Hallo,

probier mal:
1String uid = json.getString("user_id");
2//oder:
3String uid = (String) json.get("user_id");

Antworten
Kevin W.
  • Forum-Beiträge: 30

10.05.2011, 17:40:09 via Website

Hallo bin selbst drauf gekommen:

1String username = json.getString("username");

Vielleicht hat trotzdem noch jemand ne hilfreiche Seite zu Android JSON...

Danke Gruß Kevin

Antworten
Kevin W.
  • Forum-Beiträge: 30

10.05.2011, 18:33:20 via Website

ManuMaticx
Hallo,

probier mal:
1String uid = json.getString("user_id");
2//oder:
3String uid = (String) json.get("user_id");

Ups danke.
Bin selbst schon drauf gekommen nur nicht aktualisiert :) danke

Antworten
Markus B.
  • Forum-Beiträge: 636

10.05.2011, 20:57:45 via Website

Hi, also der JSON Parser in Android ist doch eigentlich ziemlich simple aufgebaut :)
Also ich habe mir diese Thematik mit diesem Beispiel angeeignet.
Evtl. bringt das noch etwas mehr Licht ins dunkel.

Gruß,
Markus

Antworten
Kevin W.
  • Forum-Beiträge: 30

14.05.2011, 22:36:37 via Website

Habs hinbekommen :)

1JSONArray json = new JSONArray(result);
2
3 for(int i=0; i < json.length(); i++) {
4 JSONObject json_data = json.getJSONObject(i);
5 String columnOneString = json_data.get("date").toString();
6 String columnTwoString = json_data.get("title").toString();
7
8 fillList(columnOneString, columnTwoString);
9 }

Antworten