Michael
- Forum-Beiträge: 19
05.06.2012, 09:50:53 via Website
05.06.2012 09:50:53 via Website
Hi Leute,
ich bin mir nicht sicher ob dieser Thread hier richtig ist, fals nicht sorry schonmal.
Ich hoffe aber doch das ihr mir helfen könnt.
Zu meinem Problem.
Ich lese in meiner App Datensätze aus einer MySQL DB aus. Das mache ich mit Hifle von PHP.
Meine Funktion sieht so aus:
Mein PHP Script so:
Da meine Anfrage ja mehrere Datensätze liefern kann, würde ich gerne wissen wie ich das ganze in einem Array oder so speichern kann und zurück gebe. Leider kenn ich mich mit PHP nicht besonders aus.
Vielen Dank und Gruß
Michael
ich bin mir nicht sicher ob dieser Thread hier richtig ist, fals nicht sorry schonmal.
Ich hoffe aber doch das ihr mir helfen könnt.
Zu meinem Problem.
Ich lese in meiner App Datensätze aus einer MySQL DB aus. Das mache ich mit Hifle von PHP.
Meine Funktion sieht so aus:
1public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
2
3 // Making HTTP request
4 try {
5 // defaultHttpClient
6 DefaultHttpClient httpClient = new DefaultHttpClient();
7 HttpPost httpPost = new HttpPost(url);
8 httpPost.setEntity(new UrlEncodedFormEntity(params));
9
10 HttpResponse httpResponse = httpClient.execute(httpPost);
11 HttpEntity httpEntity = httpResponse.getEntity();
12 is = httpEntity.getContent();
13
14 } catch (UnsupportedEncodingException e) {
15 e.printStackTrace();
16 } catch (ClientProtocolException e) {
17 e.printStackTrace();
18 } catch (IOException e) {
19 e.printStackTrace();
20 }
21
22 try {
23 BufferedReader reader = new BufferedReader(new InputStreamReader(
24 is, "iso-8859-1"), 8);
25 StringBuilder sb = new StringBuilder();
26 String line = null;
27 while ((line = reader.readLine()) != null) {
28 sb.append(line + "n");
29 }
30 is.close();
31 json = sb.toString();
32 Log.e("JSON", json);
33 } catch (Exception e) {
34 Log.e("Buffer Error", "Error converting result " + e.toString());
35 }
36
37 // try parse the string to a JSON object
38 try {
39 jObj = new JSONObject(json);
40 } catch (JSONException e) {
41 Log.e("JSON Parser", "Error parsing data " + e.toString());
42 }
43
44 // return JSON String
45 return jObj;
46
47 }
2
3 // Making HTTP request
4 try {
5 // defaultHttpClient
6 DefaultHttpClient httpClient = new DefaultHttpClient();
7 HttpPost httpPost = new HttpPost(url);
8 httpPost.setEntity(new UrlEncodedFormEntity(params));
9
10 HttpResponse httpResponse = httpClient.execute(httpPost);
11 HttpEntity httpEntity = httpResponse.getEntity();
12 is = httpEntity.getContent();
13
14 } catch (UnsupportedEncodingException e) {
15 e.printStackTrace();
16 } catch (ClientProtocolException e) {
17 e.printStackTrace();
18 } catch (IOException e) {
19 e.printStackTrace();
20 }
21
22 try {
23 BufferedReader reader = new BufferedReader(new InputStreamReader(
24 is, "iso-8859-1"), 8);
25 StringBuilder sb = new StringBuilder();
26 String line = null;
27 while ((line = reader.readLine()) != null) {
28 sb.append(line + "n");
29 }
30 is.close();
31 json = sb.toString();
32 Log.e("JSON", json);
33 } catch (Exception e) {
34 Log.e("Buffer Error", "Error converting result " + e.toString());
35 }
36
37 // try parse the string to a JSON object
38 try {
39 jObj = new JSONObject(json);
40 } catch (JSONException e) {
41 Log.e("JSON Parser", "Error parsing data " + e.toString());
42 }
43
44 // return JSON String
45 return jObj;
46
47 }
Mein PHP Script so:
1if ($tag == 'all_plans') {
2 // Request type is check all_plans
3 // check for plans
4 $plans = $db->getAllPlans();
5 if ($plans != false) {
6 // plans found
7 // echo json with success = 1
8 $response["success"] = 1;
9 $response["_id"] = $plans["_id"];
10 $response["plans"]["name"] = $plans["name"];
11 $response["plans"]["begindate"] = $plans["begindate"];
12 $response["plans"]["enddate"] = $plans["enddate"];
13 $response["plans"]["interval"] = $plans["interval"];
14 $response["plans"]["active"] = $plans["active"];
15 echo json_encode($response);
16 } else {
17 // no plans not found
18 // echo json with error = 1
19 $response["error"] = 1;
20 $response["error_msg"] = "Incorrect request. No plans found!";
21 echo json_encode($response);
22 }
2 // Request type is check all_plans
3 // check for plans
4 $plans = $db->getAllPlans();
5 if ($plans != false) {
6 // plans found
7 // echo json with success = 1
8 $response["success"] = 1;
9 $response["_id"] = $plans["_id"];
10 $response["plans"]["name"] = $plans["name"];
11 $response["plans"]["begindate"] = $plans["begindate"];
12 $response["plans"]["enddate"] = $plans["enddate"];
13 $response["plans"]["interval"] = $plans["interval"];
14 $response["plans"]["active"] = $plans["active"];
15 echo json_encode($response);
16 } else {
17 // no plans not found
18 // echo json with error = 1
19 $response["error"] = 1;
20 $response["error_msg"] = "Incorrect request. No plans found!";
21 echo json_encode($response);
22 }
1/**
2 * Get all Fahrplans
3 */
4 public function getAllPlans() {
5 $result = mysql_query("SELECT * FROM fahrplan") or die(mysql_error());
6 // check for result
7 $no_of_rows = mysql_num_rows($result);
8 if ($no_of_rows > 0) {
9 $result = mysql_fetch_array($result);
10 $_id = $result['_id'];
11 $name = $result['name'];
12 $begindate = $result['begindate'];
13 $enddate = $result['enddate'];
14 $interval = $result['interval'];
15 $active = $result['active'];
16 return $result;
17 } else {
18 // user not found
19 return false;
20 }
21 }
2 * Get all Fahrplans
3 */
4 public function getAllPlans() {
5 $result = mysql_query("SELECT * FROM fahrplan") or die(mysql_error());
6 // check for result
7 $no_of_rows = mysql_num_rows($result);
8 if ($no_of_rows > 0) {
9 $result = mysql_fetch_array($result);
10 $_id = $result['_id'];
11 $name = $result['name'];
12 $begindate = $result['begindate'];
13 $enddate = $result['enddate'];
14 $interval = $result['interval'];
15 $active = $result['active'];
16 return $result;
17 } else {
18 // user not found
19 return false;
20 }
21 }
Da meine Anfrage ja mehrere Datensätze liefern kann, würde ich gerne wissen wie ich das ganze in einem Array oder so speichern kann und zurück gebe. Leider kenn ich mich mit PHP nicht besonders aus.
Vielen Dank und Gruß
Michael
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.