为什么这两个字符串(company_id& branch_id)在MyRequestQueue_Drivers中等于null?

时间:2017-01-27 03:47:42

标签: java android string api android-volley

**
           volley的参数(company_id&& branch_id)
         等于null虽然我在通过MyRequestQueue_Company之前得到了它的真实数据所以它导致齐射响应是nullpointer异常

为什么myRequestQueue_Drivers中的company_id和branch_id等于null?我该如何解决这个问题?

public class CheckinActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {

            String company_id, branch_id;
            Spinner driver_name_spinner;
            private String User_URL = "http://example.com/api/user?token=";
            private String Drivers_URL = "http://example.com/api/getDrivers?company_id=1&branch_id=15";

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

                driver_name_spinner = (Spinner) findViewById(R.id.driver_name_spinner);

                next = (Button) findViewById(R.id.next);

                SharedPreferences sharedPref = getSharedPreferences("userinfo", getApplicationContext().MODE_PRIVATE);
                final String token = sharedPref.getString("token", "");
                final RequestQueue MyRequestQueue_Company = Volley.newRequestQueue(this);


    //  GET COMPANY_ID AND BRANCH_ID FORM API


                StringRequest MyStringRequest_Company = new StringRequest(Request.Method.GET, User_URL + token, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("response", response);

                        try {
                            JSONObject jsono = null;
                            jsono = new JSONObject(response);
                            JSONObject jarray = jsono.getJSONObject("user");
                            company_id = jarray.getString("company_id");
                            branch_id = jarray.getString("branch_id");

                            Log.d("User Data", "company_id " + company_id + "\tbranch_id " + branch_id);

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

                    }
                }, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //This code is executed if there is an error.
                    }
                });
                MyRequestQueue_Company.add(MyStringRequest_Company);

        // CALL ANOTHER WEBSERVICES TO GET DRIVERS DATA

                RequestQueue MyRequestQueue_Drivers = Volley.newRequestQueue(getApplicationContext());
                StringRequest MyStringRequest_Drivers = new StringRequest(Request.Method.POST, Drivers_URL, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("Drivers Response", response);
                        try {
                            JSONObject jsono = null;
                            jsono = new JSONObject(response);
                            JSONArray jarray = jsono.getJSONArray("Drivers");
                            List<String> drivers;
                            drivers = new ArrayList<String>();
                            for (int i = 0; i < jarray.length(); i++) {
                                JSONObject newobject = jarray.getJSONObject(i);
                                driver_name_txt = newobject.getString("name");
                                Log.d("Driver Name", driver_name_txt);

                                drivers.add(driver_name_txt);
                            }
                            ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_item, drivers);
                            dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                            driver_name_spinner.setAdapter(dataAdapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() { //Create an error listener to handle errors appropriately.
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("Drivers Error",error.toString());
                    }
                }) {
                    protected Map<String, String> getParams() {
                        Map<String, String> MyData = new HashMap<String, String>();

    //   BRANCH_ID AND COMPANY_ID ARE EQUAL NULL !?
                        MyData.put("branch_id", branch_id);
                        MyData.put("company_id", company_id);
                        Log.d("Last User Data", "company_id " + company_id + "\tbranch_id " + branch_id);


                        return MyData;
                    }
                };
                MyRequestQueue_Drivers.add(MyStringRequest_Drivers);

                driver_name_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                    @Override
                    public void onItemSelected(AdapterView<?> parent, View view, int position, long l) {

                        TextView tmpView = (TextView) driver_name_spinner.getSelectedView().findViewById(android.R.id.text1);
                        tmpView.setTextColor(Color.WHITE);
                        driver_name_txt = parent.getItemAtPosition(position).toString();
                        Log.d("driver_name_original", driver_name_txt);
                    }

                    @Override
                    public void onNothingSelected(AdapterView<?> adapterView) {

                    }
                });

1 个答案:

答案 0 :(得分:0)

如果你想使用getParams函数,我建议定义一个标志并修改你的函数,如:

                @Override
                public void onResponse(String response) {
                    Log.d("response", response);

                    try {
                        JSONObject jsono = null;
                        jsono = new JSONObject(response);
                        JSONObject jarray = jsono.getJSONObject("user");
                        company_id = jarray.getString("company_id");
                        branch_id = jarray.getString("branch_id");

                        Log.d("User Data", "company_id " + company_id + "\tbranch_id " + branch_id);
                        requestFinished = true;

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

                }

你的getParams看起来像是:

             protected Map<String, String> getParams() {
                    if(!requestFinished) return null;
                    Map<String, String> MyData = new HashMap<String, String>();

//   BRANCH_ID AND COMPANY_ID ARE EQUAL NULL !?
                    MyData.put("branch_id", branch_id);
                    MyData.put("company_id", company_id);
                    Log.d("Last User Data", "company_id " + company_id + "\tbranch_id " + branch_id);


                    return MyData;

但我建议你改用Observer pattern