从mysql数据库填充微调器值

时间:2018-08-31 12:02:29

标签: android android-spinner

我正在尝试从mysql数据库中为我的android应用程序填充微调器值,但无法使我的代码正常工作,有人可以看到我要去哪里了吗? php似乎工作正常,但我不知道问题出在哪里。

尝试进行此活动时出现java.lang.NullPointerException

这是课程代码:

public class OrderConfirm extends AppCompatActivity {

ArrayList<String> listWaiters;
private Spinner WaiterSpinner;
private ArrayAdapter<String> waiter_adapter;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order_confirm);



        Intent intent = getIntent();
        final String table = intent.getStringExtra("table");
        final String additionalInfo = intent.getStringExtra("additional_info");

        WaiterSpinner = findViewById(R.id.spnWaiters);

        setUpWaiterSpinner();
waiter_adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, listWaiters);
        WaiterSpinner.setAdapter(waiter_adapter);


}

 public void setUpWaiterSpinner() {
        String url = "hidden";
        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject obj = new JSONObject(response);
                    JSONArray array = obj.getJSONArray("waiters");

                    for (int i = 0; i < array.length(); i++) {
                        final JSONObject orderObj = array.getJSONObject(i);
                        String waiterName = orderObj.getString("name");
                        listWaiters.add(waiterName);
                        waiter_adapter.notifyDataSetChanged();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }



            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(OrderConfirm.this, "Oops!" +error.toString(), Toast.LENGTH_LONG).show();
            }
        }){
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {

                Map<String, String> params = new HashMap<>();
                params.put("menuid", "0");
                return params;
            }

        };

        MySingleton.getInstance(this).addToRequestQueue(stringRequest);

    }

以及php代码:

<?php

//include connection file
include("conn.php");

$query="SELECT * FROM rest_users WHERE user_type = 2";
$result= mysqli_query($conn, $query) or die('error: '+mysql.error());

$respone = array();

if(mysqli_num_rows($result) > 0){
    while($row = mysqli_fetch_assoc($result)){
        $response[] = $row;
    }

}
header('Content_Type: application/json;');
echo json_encode(array("waiters" => $response));

mysqli_close($conn);

任何帮助将不胜感激!

0 个答案:

没有答案