MySQL クエリを使用して PHP ファイルからデータを受信した後、2 つの配列にデータを入力したいと考えています。最初の配列は機能します:
results.add((String) json_data.get("product") + "\n" + json_data.get("owner"));
しかし、2 番目の配列を追加すると、次のようになります。
mysql_id.add((String) json_data.get("id"));
Eclipse に次のエラーが表示されます。
メソッド add(String) は String 型に対して未定義です
利用可能な提案はなく、問題がわかりません。「mysql_id」をどこでも「結果」として宣言していますが、機能しません!
手伝ってくれてありがとう!
完全なコードは次のとおりです。
package de.maartenfricke.coffeebar;
import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
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.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.Toast;
public class Overview extends Activity {
InputStream is;
ArrayList<String> results = new ArrayList<String>();
ArrayList<String> mysql_id = new ArrayList<String>();
JSONObject json_data;
JSONObject json_data_2;
Boolean dataComplete = false;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle zielkorb = getIntent().getExtras();
String text2 = zielkorb.getString("datenpaket1");
Context context = getApplicationContext();
CharSequence text = "Sie haben soeben einen " + text2 + " bestellt";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
getData();
while(!this.dataComplete) {// es soll solange gewartet werden bis die ArrayList ganz gefüllt ist und die Abfrage beendet wurde
//ProgressDialog pd = ProgressDialog.show(Overview.this,"Loading...","Frage Daten von Server ab...");
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//pd.demiss();
this.fillList();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_overview, menu);
return true;
}
public void getData() {
Thread t = new Thread() { //Setzte Tread
private String result = ""; //Setze Variable String für die Ergebnisse
private String mysql_id = ""; //Setze Variable mysql_id für mysql_id
public void run() {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); //Definiere Arrayliste
try {
HttpClient httpclient = new DefaultHttpClient(); //http client ünterstützt das indirekte verbinden mit webseiten ohne Browser und upload, download etc.
HttpPost httppost = new HttpPost("http://192.168.178.35/getAllOrders.php"); //Rufe Link auf
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch(Exception e) { //Wenn ein Fehler auftritt:
Log.e("log_tag", "Fehler bei der http Verbindung "+e.toString());
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
mysql_id=sb.toString();
result=sb.toString();
}
catch(Exception e) { //Wenn ein Fehler auftritt:
Log.e("log_tag", "Error converting result "+e.toString());
}
try {
JSONArray jArray = new JSONArray(result);
for(int i=0;i<jArray.length();i++) {
json_data = jArray.getJSONObject(i);
results.add((String) json_data.get("product") + "\n" + json_data.get("owner"));
//mysql_id.add((String) json_data.get("id"));
}
dataComplete = true;
}
catch(JSONException e) { //Wenn ein Fehler auftritt:
Log.e("log_tag", "Error parsing data "+e.toString());
}
}
};
t.start();
}
public void fillList() {
//this.setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, results));
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
for (int i = 0; i < results.size(); i++) {
String tag = results.get(i);
final CheckBox cb = new CheckBox(this);
cb.setText(tag);
cb.setTag("checkbox");
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView.isChecked()) {
try {
setOrderFinish(cb.getId());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
Toast.makeText(getBaseContext(), "UnChecked: " + buttonView + isChecked,
Toast.LENGTH_SHORT).show();
}
}
});
ll.addView(cb);
}
this.setContentView(sv);
}
public void setOrderFinish(int product_id) throws IOException {
URL url = new URL("http://192.168.178.35/setOrderAsFinished.php?id="+product_id);
URLConnection urlConnection = url.openConnection();
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
try {
Context context = getApplicationContext();
CharSequence text = "Finished: " + product_id;
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} finally {
in.close();
}
}
}