2019-04-14 06:45
浏览 83


I am trying to populate the Postgres database through an android application. I am using Volley and Php to sent the data to the Postgres database. The Post request works well through browser i.e. database gets populated, which is checked through Postman extension of chrome. But when I do it through android I am unable to do so. I do not get any error as well.

Please find the below code where I send the x and y post parameters to the URL.

submitbutton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
            public void onResponse(String response) {

                builder.setTitle("Server Response");
                builder.setMessage("Response: "+response);
                builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogInterface, int i) {
                        Toast.makeText(Soil_Info.this, "Clear fields here", Toast.LENGTH_SHORT).show();

                AlertDialog alertDialog =builder.create();
                Toast.makeText(Soil_Info.this, "Successfull", Toast.LENGTH_SHORT).show();


                , new Response.ErrorListener() {
            public void onErrorResponse(VolleyError error) {

                Toast.makeText(Soil_Info.this, "Error Occured", Toast.LENGTH_SHORT).show();


            protected Map<String, String> getParams() throws AuthFailureError {

                Map <String,String> params=new HashMap<>();

                params.put("x", "aisha");
                params.put("y", "shah");
                return params;


        stringRequest.setRetryPolicy(new RetryPolicy() {
            public int getCurrentTimeout() {
                return 50000;

            public int getCurrentRetryCount() {
                return 50000;

            public void retry(VolleyError error) throws VolleyError {

Below is the PHP code

require "connection.php";

if(isset($_REQUEST["x"]) && isset($_REQUEST["y"]))
    $names = $_REQUEST["x"];
    $surnames = $_REQUEST["y"];

    // echo $names ;
    // echo $surnames ;

    $query = "INSERT INTO emp (first, last) VALUES ('$names','$surnames')";
    echo $query;

    $result = pg_query($conn, $query);

    if (!$result) {
        die('An error occurred during query.');
    } else {
      echo ".....Successfull.....";
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);



String URL = "";

1条回答 默认 最新

相关推荐 更多相似问题