java - 输出值作为 NULL 返回,而不是存储在数据库表中的值

标签 java android mysql

这是根据食物和输入的数量显示膳食碳水化合物含量的代码。 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();
}
}

最佳答案

问题的直接原因很可能是您将数量作为字符串用单引号括起来,而不是将其视为数字。如果是这样,那么以下应该有效:

 $sql = "SELECT * FROM carbcontent WHERE food='$food' &    quantity=$quantity";

但是您应该认真考虑使用准备好的语句,这将使您不必担心查询中包含的数据类型。

您是否可能忘记选择数据库?

$db = mysql_select_db('your_db');

将其放在 PHP 代码中进行查询之前。

关于java - 输出值作为 NULL 返回,而不是存储在数据库表中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43315621/

相关文章:

java - Mysql从sql语句复制数据库

android - 连续登录 Android 和 iOS 移动应用程序的策略

php - 在单个查询中连接两个表。需要查询来执行我的 php 代码

java - 我的 java 连接返回服务器连接失败

java - org.joda.DateTime 返回错误的月份

java - 在 Java 中查明 Sun Directory Server 5.2 中的 LDAP 用户是否被锁定

android - 在填充的 android Canvas 上绘制一个透明圆圈

android - Google Play 应用说明格式

php - MySQL:如果小于 now() 加 1 个月,则选择日期加 6 个月

java - 如何在单个结果集中连接或联合2个sql查询的结果