user12247057
user12247057

Reputation:

how Send information to server with volley?

I want to send the information to the server using the volley library and save it to the table. But after running the app no ​​data is stored in the table.

Can you tell which part is wrong? (I did most of the things that were said on the site but my problem was not resolved)

cart.java

  public class cart extends AppCompatActivity {
Button Add;   
private static final String url="http://localhost/test/addpost.php";
Faculty items;
JSONArray myarray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cart);
    Add=findViewById(R.id.btn_basket_pay);
    dbHandler dbh = new dbHandler(this);
    dbh.open();
    items=new Faculty();
    JSONObject JSONestimate = new JSONObject();
    myarray = new JSONArray();
    RecyclerView recyclerView = (RecyclerView) findViewById(R.id.rec);
    final FacultyAdapter facultyAdapter = new FacultyAdapter(this,dbh.display());
    recyclerView.setLayoutManager(new LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false));
    recyclerView.setAdapter(facultyAdapter);
    dbh.close();

    for (int i = 0; i < facultyAdapter.getItemCount(); i++) {

        try {
            JSONestimate.put("name:" + String.valueOf(i + 1), facultyAdapter.getJSONObject());
            myarray.put(facultyAdapter.getJSONObject());

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    Log.d("JSONobject: ", JSONestimate.toString());
    Log.d("JSONArray : ", myarray.toString());


    Add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
            StringRequest stringRequest=new StringRequest(Request.Method.GET, url, new 
            Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    Log.d("onResponse", "o");
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("onErrorResponse", "o");
                }
            }){
                @Override
                public Map<String,String> getParams(){
                    Map<String,String> params=new HashMap<>();
                  params.put("jsonarray",myarray.toString());
                    return  params;

                }
            };
            queue.add(stringRequest);
            Toast.makeText(cart.this,"success",Toast.LENGTH_SHORT).show();
        }
    });
   }}

I checked the php code with the postman program it worked correctly.

addpost.php

         <?php
    if($_SERVER['REQUEST_METHOD']=='GET'){

require_once('dbConnect.php');

$name = $_GET['name'];
$price= $_GET['price'];
$id = $_GET['id'];

$response = array();
$server_ip = gethostbyname(gethostname());
$sql = "INSERT INTO tblcart (id,name,price) VALUES ('$id','$name','$price')";   
try {
    if (mysqli_query($cnn,$sql)) {
        $response = "done";
    }else{
    $response = "error";
    }
} catch (Exception $e) {
        $response = "error";
} 
echo $response;
  }
   ?>    

Upvotes: 0

Views: 71

Answers (1)

YJ.XU
YJ.XU

Reputation: 11

The url should be changed to ""http://10.0.2.2/test/addpost.php" if you want to run android virtual decive on your computer.If you use "localhost",the android virtual device will access itself.I haved this situation yesterday too. Hope useful!

Upvotes: 1

Related Questions