如何将ratingbar值正确传递给Map

时间:2016-08-28 13:16:08

标签: php android mysql

我应该如何将评级栏值正确地传递给Map,以便可以在线发送到mysql," lastname"它在网上发送的部分代码我的评级栏有问题。它没有显示任何错误,但它不向mysql发送任何值。

public class MainActivity extends AppCompatActivity {

EditText firstname, comment, age;
Button insert, show;
RequestQueue requestQueue;
String insertUrl = "http://192.168.1.2/insertStudent.php";
TextView result;
RatingBar ratingBar;

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

    comment = (EditText) findViewById(R.id.etComment);
    insert = (Button) findViewById(R.id.insert);
    ratingBar = (RatingBar) findViewById(R.id.ratingBar);
    final float rating = ratingBar.getRating();



    requestQueue = Volley.newRequestQueue(getApplicationContext());


    insert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            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("lastname",comment.getText().toString());
                    parameters.put("rating",String.valueOf(rating));
                    return parameters;
                }
            };
            requestQueue.add(request);
        }

    });


}

}

这是我的PHP代码

 <?php

if($_SERVER["REQUEST_METHOD"]=="POST"){
require 'connection.php';
createStudent();
}


function createStudent()
{
global $connect;


$lastname = $_POST["lastname"];
$age = $_POST["age"];

$query = " Insert into student(lastname,rating) values ('$lastname','$rating');";

mysqli_query($connect, $query) or die (mysqli_error($connect));
mysqli_close($connect);

}

&GT;

0 个答案:

没有答案