MySQL Datenbak Abfrage mit JSON

Y

YannickB

Neues Mitglied
0
Hallo,
bin neu hier und habe noch nicht viel Erfahrung mit der Android Programmierung. Ich arbeite normalerweise im Webbereich mit PHP, C ...
Die Grundlagen sind mir bereits bekannt.
Jetzt möchte ich gerne Daten aus einer MySQL Datenbak abfragen, dazu habe ich ein PHP Skript, welches die Daten aus der Datenbak ausliest und als JSON Array ausgibt.
Ich habe schon viel gesucht, aber leider keine Seite gefunden, auf der es auführlich und gut erklärt war.

Wie kann ich die Daten vom Server abrufen und verarbeiten?

Bin auch über Links sehr Dankbar, wenn das Thema verständlich erklärt ist und nicht einfach nur Code ist. Bevorzugt in Deutsch; Englisch ist auch gut :)

Schonmal vielen Dank im Vorraus.

gruß
Yannick
 
Bin mittlerweile schon soweit, dass die Daten abgerufen werden, und als Array Ausgegeben werden. Ich habe die Problemstelle im Quellcode markiert.

Code:
[SIZE=2][COLOR=#7f0055][B][SIZE=2][COLOR=#7f0055]package [/COLOR][/SIZE][/B][/COLOR][/SIZE][SIZE=2]de.yannickb.howto_mysql;[/SIZE]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]java.io.BufferedReader;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]java.io.IOException;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]java.io.InputStream;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]java.io.InputStreamReader;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]org.apache.http.HttpEntity;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]org.apache.http.HttpResponse;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]org.apache.http.client.ClientProtocolException;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]org.apache.http.client.HttpClient;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]org.apache.http.client.methods.HttpGet;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]org.apache.http.impl.client.DefaultHttpClient;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]org.json.JSONArray;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]org.json.JSONException;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]org.json.JSONObject;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]android.app.Activity;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]android.os.Bundle;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]android.util.Log;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]android.widget.TextView;[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]import [/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]android.widget.Toast;[/SIZE][/LEFT]
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][/LEFT]
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]class[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] howto_mysql [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extends[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] Activity {[/SIZE][/LEFT]
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]static[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] InputStream [/SIZE][I][U][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]is[/COLOR][/SIZE][/COLOR][/SIZE][/U][/I][SIZE=2] = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2];[/SIZE][/LEFT]
 
 
 
[LEFT][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]/** Called when the activity is first created. [/COLOR][/SIZE][/COLOR][/SIZE]

[SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf]* [/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]

 
 
 
[LEFT][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]@param[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f5fbf][SIZE=2][COLOR=#3f5fbf] is */[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//@SuppressWarnings("null")[/COLOR][/SIZE][/COLOR][/SIZE]

[SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState) {[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE]
[SIZE=2]setContentView(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]main[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[SIZE=2]getJSONObject([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"http://192.168.100.20/android/get_user.php"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]
[/LEFT]

 
 
 
 
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] String [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TAG[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]=[/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"test"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; [/SIZE][/LEFT]
 
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] getJSONObject(String url) [/SIZE]

[SIZE=2]{ [/SIZE]
[LEFT][SIZE=2]HttpClient httpClient = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] DefaultHttpClient(); [/SIZE]
[SIZE=2]HttpGet httpGet = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] HttpGet(url); [/SIZE]
[SIZE=2]HttpResponse response; [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] { [/SIZE]
[SIZE=2]response = httpClient.execute(httpGet); [/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// [/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]: HTTP-Status (z.B. 404) in [U]eigener[/U] [U]Anwendung[/U] [U]verarbeiten[/U]. [/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]Log.[I]i[/I]([/SIZE][/LEFT]
[/LEFT]

 
 
 
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TAG[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],response.getStatusLine().toString()); [/SIZE][/LEFT]
 
[LEFT][SIZE=2]HttpEntity entity = response.getEntity(); [/SIZE]

[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](entity != [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2])[/SIZE]
[LEFT][SIZE=2]{ [/SIZE]
[SIZE=2]InputStream instream = entity.getContent(); [/SIZE]
[SIZE=2]BufferedReader reader = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] BufferedReader([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] InputStreamReader(instream)); [/SIZE]
[SIZE=2]StringBuilder sb = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] StringBuilder(); [/SIZE]
[SIZE=2]String line = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]; [/SIZE][/LEFT]
[/LEFT]

 
 
 
 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]while[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ((line = reader.readLine()) != [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]null[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2])[/SIZE]

[SIZE=2]{[/SIZE]
[LEFT][SIZE=2]sb.append(line + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]); } [/SIZE]
[SIZE=2]String result=sb.toString(); [/SIZE]
[SIZE=2]Log.[I]i[/I]([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]TAG[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2],result);[/SIZE]
[SIZE=2]instream.close(); [/SIZE][/LEFT]
[/LEFT]

 
 
 
 
[LEFT][SIZE=2]TextView txt = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] TextView([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]);[/SIZE]

[SIZE=2]txt.setText(result);[/SIZE]
[LEFT][SIZE=2]setContentView(txt);[/SIZE]
[SIZE=2]///////////////////////////////////////////////[/SIZE][/LEFT]
[/LEFT]

 
 
 
 
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//[U]bis[/U] [U]hier[/U] [U]klapps[/U] [U]gibt[/U] den [U]json[/U] string [U]aus[/U] :)[/COLOR][/SIZE][/COLOR][/SIZE]

//[{"id":"1","name":"Yannick","sex":"1","birthyear":"1992"}]
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//////////////////////////////////////////////[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
[/LEFT]

 
 
 
 
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//**************die For schleife wird leider nicht durchlaufen***************[/COLOR][/SIZE][/COLOR][/SIZE][/LEFT]
 
[LEFT][SIZE=2]JSONObject json = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] JSONObject(result); [/SIZE]

[SIZE=2]JSONArray nameArray=json.names(); [/SIZE]
[LEFT][SIZE=2]JSONArray valArray=json.toJSONArray(nameArray); [/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]for[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] i=0;i<valArray.length();i++) [/SIZE]
[SIZE=2]{[/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//[/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]: [U]Inhalte[/U] [U]der[/U] Arrays [U]verarbeiten[/U]. ; [/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]} [/SIZE]
[SIZE=2]} [/SIZE]
[SIZE=2]}[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](ClientProtocolException e){ [/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// [/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f] Auto-generated catch block [/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]e.printStackTrace(); [/SIZE]
[SIZE=2]}[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](IOException e){ [/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// [/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f] Auto-generated catch block [/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]e.printStackTrace(); [/SIZE]
[SIZE=2]}[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2](JSONException e){ [/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// [/COLOR][/SIZE][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f9fbf][SIZE=2][COLOR=#7f9fbf]TODO[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f] Auto-generated catch block [/COLOR][/SIZE][/COLOR][/SIZE]
[SIZE=2]e.printStackTrace(); [/SIZE]
[SIZE=2]}[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]catch[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (Exception e){ [/SIZE]
[SIZE=2]e.printStackTrace(); [/SIZE]
[SIZE=2]}[/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]finally[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2]{ [/SIZE]
[SIZE=2]httpGet.abort(); [/SIZE]
[SIZE=2]} [/SIZE]
[SIZE=2]} [/SIZE][/LEFT]
[/LEFT]

 
 
[LEFT][SIZE=2]}[/SIZE][/LEFT]

Ich hoffe mir kann jemand weiterhelfen.
gruß Y.
 
Das ist ja auch ein JSON Array welches ein JSON Objekt beinhaltet ;-)
 

Ähnliche Themen

B
Antworten
3
Aufrufe
1.308
swa00
swa00
B
Antworten
0
Aufrufe
689
basementmedia
B
B
Antworten
4
Aufrufe
496
bb321
B
Zurück
Oben Unten