这是根据食物和输入的数量显示膳食碳水化合物含量的代码。 PHP代码工作正常。当我尝试获取输出时,它显示空值。(Carb:null)
访问getdata.php
<?php
if($_SERVER['REQUEST_METHOD']=='GET'){
$food = $_GET['food'];
$quantity = $_GET['quantity'];
require_once('dbConnect.php');
$sql = "SELECT * FROM carbcontent WHERE food='$food' & quantity='$quantity'";
$r = mysqli_query($con,$sql);
$res = mysqli_fetch_array($r);
$result = array();
array_push($result,array(
"carb"=>$res['carb']
)
);
echo json_encode(array("result"=>$result));
mysqli_close($con);
}
dbConnect.php
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','slidingscale');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
Config.java
public class Config {
public static final String DATA_URL = "http://10.1.56.2 /getData.php?food=&quantity=";
public static final String KEY_CARB ="carb";
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="FOOD"
android:id="@+id/textView" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="QUANTITY"
android:id="@+id/textView2" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="@+id/editText2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="GET"
android:id="@+id/button" />
<TextView
android:id="@+id/textViewResult"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
MainActivity.java
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editText;
private EditText editText2;
private Button button;
private TextView textViewResult;
private ProgressDialog loading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText) findViewById(R.id.editText);
editText2 = (EditText) findViewById(R.id.editText2);
button = (Button) findViewById(R.id.button);
textViewResult = (TextView) findViewById(R.id.textViewResult);
button.setOnClickListener(this);
}
private void getData() {
String food = editText.getText().toString().trim();
if (food.equals("")) {
Toast.makeText(this, "Please enter an food", Toast.LENGTH_LONG).show();
return;
}
String quantity = editText2.getText().toString().trim();
if (quantity.equals("")) {
Toast.makeText(this, "Please enter quantity", Toast.LENGTH_LONG).show();
return;
}
loading = ProgressDialog.show(this,"Please wait...","Fetching...",false,false);
String url = Config.DATA_URL+editText.getText().toString().trim()+editText2.getText().toString().trim();
StringRequest stringRequest = new StringRequest(url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
loading.dismiss();
showJSON(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this,error.getMessage().toString(),Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
private void showJSON(String response){
String carb="";
try {
JSONObject jsonObject = new JSONObject(response);
JSONArray result = jsonObject.getJSONArray(Config.JSON_ARRAY);
JSONObject collegeData = result.getJSONObject(0);
carb = collegeData.getString(Config.KEY_CARB);
} catch (JSONException e) {
e.printStackTrace();
}
textViewResult.setText("\nCarb:\t" +carb);
}
@Override
public void onClick(View v) {
getData();
}
}
答案 0 :(得分:0)
问题的直接原因很可能是您将数量换成单个引号的字符串,而不是将其视为数字。如果是这样,那么以下内容应该有效:
$sql = "SELECT * FROM carbcontent WHERE food='$food' & quantity=$quantity";
但您应该认真考虑使用预准备语句,这样可以免除您对查询中包含的数据类型的担忧。
您是否忘记选择数据库?
$db = mysql_select_db('your_db');
在使用PHP代码进行查询之前将其放入。