- Forum-Beiträge: 45
14.12.2016, 18:12:00 via Website
14.12.2016 18:12:00 via Website
Hallo,
ich habe ein Problem mit dem Auslesen von Daten aus einer MySQl-Datenbank.
Das Eintragen und komplett Ausgeben von Daten in bzw. aus der Datenbank funktioniert super.
Nun möchte ichzu Übungszwecken eine App erstellen, bei der man einen x-Wert und einen y-Wert eingibt. Diese sollen dann von der Datnebank einzeln ausgelesen werden und als Graph dargetsellt werden.
Mit diesem Code hole ich mir die KOMPLETTE Datenbank:
protected void showList() {
try {
JSONObject jsonObj = new JSONObject(myJSON);
data = jsonObj.getJSONArray(TAG_RESULTS);
for (int i = 0; i < data.length(); i++) {
JSONObject c = data.getJSONObject(i);
String x = c.getString(TAG_X);
String y = c.getString(TAG_Y);
HashMap<String, String> data = new HashMap<String, String>();
data.put(TAG_X, "x=" + x);
data.put(TAG_Y, "y=" + y);
dataList.add(data);
//Hier einsetzen
}
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, dataList, R.layout.list_item,
new String[]{TAG_X, TAG_Y},
new int[]{R.id.x_list, R.id.y_list}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void getData() {
class GetDataJSON extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost(".../get-graph.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
} finally {
try {
if (inputStream != null) inputStream.close();
} catch (Exception squish) {
}
}
return result;
}
@Override
protected void onPostExecute(String result) {
myJSON = result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
An der Stelle "Hier einsetzen" hätte ich nun, um einzelne int-Werte in ein Array zu schreiben, folgendes eingesetzt:
int xParsed = Integer.parseInt(x);
int yParsed = Integer.parseInt(y);
arrayX[i]=xParsed;
arrayY[i]=yParsed;
Leider funktioniert das nicht so wie ich das möchte.
Kann mir bitte jemand helfen, der mehr Ahnung hat als ich Laie
Danke!