如何在android中使用volley库将数据保存到mysql中?

时间:2022-10-22 20:18:21

I'm try to save data into mysql database using volley library from localhost . when i try to fetch json and add some data to mysql there is occur an error , the app is unfortunately stop. Showing Error : Java.NullPointerException at this line

我尝试使用localhost的volley库将数据保存到mysql数据库中。当我尝试获取json并向mysql添加一些数据时,会发生错误,应用程序很遗憾地停止。显示错误:此行的Java.NullPointerException

Controller.getInstance().addToReqQueue(request);

Here is my code :

这是我的代码:

public class MainActivity extends AppCompatActivity {

    EditText firstname, lastname, age;
    Button insert, show;
    TextView result;
    String insertUrl = "http://192.168.56.1/android_post_api/insertStudent.php";
    String showUrl = "http://192.168.56.1/android_post_api/show.php";

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

        firstname = (EditText) findViewById(R.id.editText);
        lastname = (EditText) findViewById(R.id.editText2);
        age = (EditText) findViewById(R.id.editText3);
        insert = (Button) findViewById(R.id.insert);
        show = (Button) findViewById(R.id.showstudents);
        result = (TextView) findViewById(R.id.textView);
        show.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("ww");
                JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                        showUrl, new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        System.out.println(response.toString());
                        try {
                            JSONArray students = response.getJSONArray("students");
                            for (int i = 0; i < students.length(); i++) {
                                JSONObject student = students.getJSONObject(i);

                                String firstname = student.getString("firstname");
                                String lastname = student.getString("lastname");
                                String age = student.getString("age");

                                result.append(firstname + " " + lastname + " " + age + " \n");
                            }
                            result.append("===\n");

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

                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        System.out.append(error.getMessage());

                    }
                });
                Controller.getInstance().addToReqQueue(jsonObjectRequest);
            }
        });

        insert.setOnClickListener(new View.OnClickListener(){

            @Override
            public void onClick(View v) {
                StringRequest request = new StringRequest(Request.Method.POST, insertUrl, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        System.out.println(response.toString());

                    }
                }, new Response.ErrorListener() {

                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                }) {
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String,String> parameters = new HashMap<String, String>();
                        parameters.put("firstname",firstname.getText().toString());
                        parameters.put("lastname",lastname.getText().toString());
                        parameters.put("age",age.getText().toString());
                        return parameters;
                    }

                };
                Controller.getInstance().addToReqQueue(request);
            }
        });
    }
   }

2 个解决方案

#1


0  

You can refer to my following code:

您可以参考我的以下代码:

        JSONObject jsonBody;
        try {
            jsonBody = new JSONObject();
            jsonBody.put("Title", "Android Volley Demo");
            jsonBody.put("Author", "BNK");
            jsonBody.put("Date", "2015/09/06");
            mRequestBody = jsonBody.toString();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                mTextView.setText(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mTextView.setText(error.toString());
            }
        }) {
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }

            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                            mRequestBody, "utf-8");
                    return null;
                }
            }
        };
        MySingleton.getInstance(this).addToRequestQueue(stringRequest);
    } catch (JSONException e) {
        e.printStackTrace();
    }

#2


0  

use this instead : at the begning of your insertion function add the following line of code

改为使用它:在插入函数的开头添加以下代码行

  RequestQueue rq= Volley.newRequestQueue(this);

and last remove this line of code from your function

并最后从您的函数中删除此行代码

 Controller.getInstance().addToReqQueue(request);

and add this instead

并添加此项

 rq.add(request);

#1


0  

You can refer to my following code:

您可以参考我的以下代码:

        JSONObject jsonBody;
        try {
            jsonBody = new JSONObject();
            jsonBody.put("Title", "Android Volley Demo");
            jsonBody.put("Author", "BNK");
            jsonBody.put("Date", "2015/09/06");
            mRequestBody = jsonBody.toString();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                mTextView.setText(response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                mTextView.setText(error.toString());
            }
        }) {
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }

            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                            mRequestBody, "utf-8");
                    return null;
                }
            }
        };
        MySingleton.getInstance(this).addToRequestQueue(stringRequest);
    } catch (JSONException e) {
        e.printStackTrace();
    }

#2


0  

use this instead : at the begning of your insertion function add the following line of code

改为使用它:在插入函数的开头添加以下代码行

  RequestQueue rq= Volley.newRequestQueue(this);

and last remove this line of code from your function

并最后从您的函数中删除此行代码

 Controller.getInstance().addToReqQueue(request);

and add this instead

并添加此项

 rq.add(request);