public void _uploadProduct(final String _product_name, final String _product_description) { url = "https://www.mywebsite.com/add_product.php";
final String productName = _product_name; final String productDescription = _product_description;
// Log the product name and description Log.d("MyApp", "Product Name: " + productName); Log.d("MyApp", "Product Description: " + productDescription);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() { @Override public void onResponse(String response) { // Handle the response from the server try { JSONObject jsonResponse = new JSONObject(response); String message = jsonResponse.getString("message");
// Display a success message to the user Toast.makeText(UploadActivity.this, message, Toast.LENGTH_SHORT).show();
// Clear input fields edittext1.setText(""); edittext2.setText(""); } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { // Handle error responses from the server Toast.makeText(UploadActivity.this, "Error uploading product", Toast.LENGTH_SHORT).show(); Log.e("Upload Error", error.toString()); } }) { // Define the parameters to be sent to the server @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<>(); params.put("product_name", productName); params.put("product_description", productDescription); // Add more parameters if needed return params; } };
// Add the request to the request queue RequestQueue requestQueue = Volley.newRequestQueue(this); requestQueue.add(stringRequest); } |