- Forum-Beiträge: 187
09.06.2014, 21:29:52 via Website
09.06.2014 21:29:52 via Website
Ich weiss nicht öb dies möglich ist, meien english kenntnisse sind zu schlecht, so das mir google das ausgespuckt hat was ich vorhabe.
Also, grundsätzlich geht es darum das ich eine activity gestaltet habe die einzig dafür gemacht ist mit php und Mysql zu synchronisieren.
Das heisst in der hauptactivty starte ich die abfrage und als rückgabe kommt dan ein JSON ARRAY an: Was ich hatte war
String fd_bn = null;
String fd_pw = null;
try{
JSONArray jArray = new JSONArray(result);
JSONObject json_data=null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
fd_bn=json_data.getString("bn"
fd_pw=json_data.getString("pw"
Log.i("zeige bn", fd_bn);
Log.i("zeige pw", fd_pw);
}
Nun möchte ich das alle Strings automatisch erstellt werden.
Also für die Spalte strasse, wird ein String strasse erstellt(falls mehrfach strasse[]), mit dem enthaltenen wert. Keine ahnung wie man so etwas nennt. Ich weiss das ich nachher einen grossen teil immer auf die selbe art aufrufen möchte... und das würde das ganze enorm vereinfachen.
Der restliche Code sieht so aus, ich habe ihn noch nicht probiert, ich poste ihn aber mal, nicht das ihr denkt das ich einfach nur faul bin
package com.awad.awad_glasrechner;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.ParseException;
import android.util.Log;
public class phpMySQL_jsonArray {
public ArrayList<NameValuePair> creatPost(String[][] post){
List<NameValuePair> nameValuePairs;
nameValuePairs = new ArrayList<NameValuePair>();
for(int i=0;i<post.length;i++){
nameValuePairs.add(new BasicNameValuePair(post[i][0],post[i][1].trim()));
}
return (ArrayList<NameValuePair> nameValuePairs;
}
public String whereClause(String[][] where) {
String whereClause = null;
for(int i=0;i<where.length;i++){
if(i!=0){ whereClause += "&&";}
whereClause += where[i][0] + " = '"+ where[i][1] + "' ";
}
return whereClause;
}
public String ArrayToString(JSONArray jArray) throws JSONException{
String bn = null;
JSONObject json_data = null;
for(int i=0;i<jArray.length();i++){
json_data = jArray.getJSONObject(i);
bn =json_data.getString("bn"
???????????????????????
}
return bn;
}
public JSONArray sendPostEntrys(String uri,ArrayList<NameValuePair> post){
InputStream is = null;
StringBuilder sb=null;
String result=null;
Log.i("mysql_connect","start"
try{
HttpClient httpclient=new DefaultHttpClient();
HttpPost httppost= new HttpPost(uri);
httppost.setEntity(new UrlEncodedFormEntity(post));
HttpResponse response=httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Log.e("log_tag", "Error in http connection"+ e.toString());
}
//convert response to string
try{
InputStreamReader iSR = new InputStreamReader(is,"UTF-8"
BufferedReader reader = new BufferedReader(iSR,8);
sb = new StringBuilder();
sb.append(reader.readLine());
String line="0";
while ((line = reader.readLine()) != null) {
sb.append(line );
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error in read"+e.toString());
}
//convert response to string
if(result != null && !result.isEmpty()){
Log.i("result is true","Array wird erstellt"
//paring data
try{
JSONArray jArray = new JSONArray(result);
return jArray;
}catch(JSONException e1){
Log.e("benutzer gefunden","Array wird erstellt"
}catch (ParseException e1){
e1.printStackTrace();
}
}else{
Log.i("leere Rückgabe","Resultat ist leer"
}
JSONArray jArray = new JSONArray();
return jArray;
}
}