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

Php 如何在android中使用volley库将数据保存到mysql中?,php,android,mysql,android-volley,Php,Android,Mysql,Android Volley,我正在尝试使用本地主机上的volley库将数据保存到mysql数据库中。当我尝试获取json并向mysql添加一些数据时,出现了一个错误,不幸的是,应用程序停止了。显示错误:Java.NullPointerException在此行 Controller.getInstance().addToReqQueue(request); 这是我的密码: public class MainActivity extends AppCompatActivity { EditText firstnam

我正在尝试使用本地主机上的volley库将数据保存到mysql数据库中。当我尝试获取json并向mysql添加一些数据时,出现了一个错误,不幸的是,应用程序停止了。显示错误:
Java.NullPointerException
在此行

Controller.getInstance().addToReqQueue(request);
这是我的密码:

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);
            }
        });
    }
   }
public类MainActivity扩展了AppCompatActivity{
EditText名字、姓氏、年龄;
按钮插入,显示;
文本视图结果;
字符串insertUrl=”http://192.168.56.1/android_post_api/insertStudent.php";
字符串showUrl=”http://192.168.56.1/android_post_api/show.php";
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firstname=(EditText)findViewById(R.id.EditText);
lastname=(EditText)findViewById(R.id.editText2);
年龄=(EditText)findViewById(R.id.editText3);
insert=(按钮)findViewById(R.id.insert);
show=(按钮)findViewById(R.id.showstudents);
结果=(TextView)findViewById(R.id.TextView);
show.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
System.out.println(“ww”);
JsonObjectRequest JsonObjectRequest=新的JsonObjectRequest(Request.Method.POST,
showUrl,新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
System.out.println(response.toString());
试一试{
JSONArray students=response.getJSONArray(“学生”);
for(int i=0;i
您可以参考我的以下代码:

        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();
    }
JSONObject-jsonBody;
试一试{
jsonBody=新的JSONObject();
放置(“标题”,“安卓截击演示”);
jsonBody.put(“作者”、“BNK”);
jsonBody.put(“日期”,“2015/09/06”);
mRequestBody=jsonBody.toString();
StringRequest StringRequest=new StringRequest(Request.Method.POST,url,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
mTextView.setText(响应);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
mTextView.setText(error.toString());
}
}) {
@凌驾
公共字符串getBodyContentType(){
返回“application/json;charset=utf-8”;
}
@凌驾
公共字节[]getBody()抛出AuthFailureError{
试一试{
返回mRequestBody==null?null:mRequestBody.getBytes(“utf-8”);
}捕获(不支持的编码异常uee){
wtf(“尝试使用%s获取%s的字节时不支持编码”,
mRequestBody,“utf-8”);
返回null;
}
}
};
getInstance(this).addToRequestQueue(stringRequest);
}捕获(JSONException e){
e、 printStackTrace();
}

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

        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();
    }
JSONObject-jsonBody;
试一试{
jsonBody=新的JSONObject();
放置(“标题”,“安卓截击演示”);
jsonBody.put(“作者”、“BNK”);
jsonBody.put(“日期”,“2015/09/06”);
mRequestBody=jsonBody.toString();
StringRequest StringRequest=新StringRequest(Request.Method.POST、url、新响应
 Controller.getInstance().addToReqQueue(request);
 rq.add(request);