spinner
php/JSON を使用して、MySQL データベース (経費カテゴリの名前) から単一のフィールドの値を入力しようとしています。これが私のコードです。私が間違っていることを知っている人はいますか?
PHP:
<?php
require("config.inc.php");
$cat_query = "Select * from expense_categories" ;
try {
$cat_stmt = $db ->prepare($cat_query);
$cat_result = $cat_stmt -> execute();
}
catch (PDOException $ex) {
$response["success"] = 0;
$response["message"] = "Database Error!";
die(json_encode($response));
}
$cat_rows = $cat_stmt->fetchAll();
if ($cat_rows) {
$response["success"] = 1;
$response["message"] = "Post Available!";
$response["posts"] = array();
foreach ($cat_rows as $row) {
$post = array();
$post ["category_id"] = $row["category_id"];
$post["Category_Name"] = $row["Category_Name"];
//update our repsonse JSON data
array_push($response["posts"], $post);
}
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "No Post Available!";
die(json_encode($response));
}
?>
実行すると、次のように返されます。
{"success":1,"message":"投稿可能!","posts":[{"category_id":"1","Category_Name":"旅行"},{"category_id":"2"," Category_Name":"食事"}]}
XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Spinner
android:id="@+id/spincat"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10dp"
android:layout_marginTop="99dp" />
</RelativeLayout>
ジャワ
package;
imports;
public class Spinner_Category extends Activity {
private static final String LOGIN_URL = "http://192.168.10.253:88/expenses/spinner_category.php";
private JSONArray jArray = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinner_category);
}
public void updateJSON () {
String POSTS = "posts";
String ID = "category_id";
String CATEGORY = "Category_Name";
JSONParser jsonParser = new JSONParser();
Spinner spin = (Spinner) findViewById (R.id.spincat);
JSONObject json = jsonParser.getJSONFromUrl(LOGIN_URL);
final String[] items = new String[jArray.length()];
try{
jArray = json.getJSONArray(POSTS);
for (int i = 0; i < jArray.length(); i++) {
JSONObject c = jArray.getJSONObject(i);
//gets the content of each tag
items[i]=c.getString(CATEGORY);
items [i]=c.getString(ID);
}
}
catch (JSONException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter =
new ArrayAdapter<String> (Spinner_Category.this, android.R.layout.simple_spinner_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item) ;
spin.setAdapter(adapter);
}
}