HttpRequest
Web サーバーにある php スクリプトにを送信しようとしています。特定のカテゴリのすべての料理を取得したいと思います。料理は、クラスがデータを解析するJSON
形式で返されます。JSONParser
以下はdoInBackground
、ファイルを要求する私の方法です。
@Override
protected Boolean doInBackground(String... params) {
// location of file to retrieve
String url = "http://www.jdiadt.com/getAllDishes.php";
JSONObject json = jParser.makeHttpRequest(url, "POST", cat);
// For testing purposes - Ensure all data was received.
Log.d("SINGLE DISH DETAILS: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
dishes = json.getJSONArray(TAG_DISHES);
for (int i = 0; i < dishes.length(); i++) {
JSONObject d = dishes.getJSONObject(i);
MenuItem m = new MenuItem();
m.setName(d.getString(TAG_NAME));
m.setPrice(Double.parseDouble(d.getString(TAG_PRICE)));
Log.d("MENUITEM " + i + ":", " NAME:" + m.getName()
+ " PRICE:" + m.getPrice());
starters.add(m);
}
} else {
return false;
}
return true;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
これはクラスのmakeHttpRequest()
メソッドです:JSONParser
public JSONObject makeHttpRequest(String url, String method, String cat) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
List <NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("cat", cat));
try {
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
これは私が取得しようとしているスクリプトです:
$response = array();
//Variables for connecting to your database.
//These variable values come from your hosting account.
$hostname = "xxx.xxx.xxx.xxx";
$username = "xxxxxx";
$dbname = "xxxxxxx";
//These variable values need to be changed by you before deploying
$password = "xxxxxxxxxxx";
$usertable = "dishes";
$yourfield = "dish_name";
//Connecting to your database
mysql_connect($hostname, $username, $password) OR DIE ("Unable to
connect to database! Please try again later.");
mysql_select_db($dbname);
$cat = $_POST['cat'];
// get all dishes from dishes table
$result = mysql_query("SELECT * FROM $usertable where dish_cat = '$cat' ") or die(mysql_error());
// check for empty result
if (mysql_num_rows($result) > 0) {
// looping through all results
// dishes node
$response["dishes"] = array();
while ($row = mysql_fetch_array($result)) {
// temp user array
$dishes = array();
$dishes["dish_id"] = $row["dish_id"];
$dishes["dish_name"] = $row["dish_name"];
$dishes["dish_desc"] = $row["dish_desc"];
$dishes["dish_price"] = $row["dish_price"];
// push single dish into final response array
array_push($response["dishes"], $dishes);
}
// success
$response["success"] = 1;
// echoing JSON response
echo json_encode($response);
} else {
// no dishes found
$response["success"] = 0;
$response["message"] = "No dishes found";
// echo no users JSON
echo json_encode($response);
}
次の行に注意してください。
$cat = $_POST['cat']
それは料理のカテゴリを取得する必要があり、SQLステートメントは正常に実行されるはずです....現在、「料理が見つかりません」というメッセージが返されます。
どんな助けでも大歓迎です。何が間違っているのかわかりません。