Kevin W.
- Forum-Beiträge: 30
10.05.2011, 17:20:35 via Website
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:
Er gibt mir diese Meldung aus:
Mein JSON Code sieht so aus:
Was mach ich falsch?
Bitte schickt mir jetzt nicht den link zu android development seite, da versteh ich nur bahnhof.
Danke
Gruß Kevin
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}
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
Empfohlener redaktioneller Inhalt
Mit Deiner Zustimmung wird hier ein externer Inhalt geladen.
Mit Klick auf den oben stehenden Button erklärst Du Dich damit einverstanden, dass Dir externe Inhalte angezeigt werden dürfen. Dabei können personenbezogene Daten an Drittanbieter übermittelt werden. Mehr Infos dazu findest Du in unserer Datenschutzerklärung.