Question:
Upload data to my website database from an Android app

To upload data from an Android app to your website's database, you typically follow a client-server architecture where the Android app acts as the client and communicates with a server-side script or API to interact with the database. 


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);

}


Credit:> Stackoverflow


Suggested blogs:

>5 easy ways to repair the .NET Framework on Windows

>Set up Node.js & connect to a MongoDB Database Using Node.js

>8 Steps to use Redux Persist in React Native

>Build a minimal ASP.Net Core API with Android Studio for Mac

>How to delete duplicate names from Array in Typescript?







Ritu Singh

Ritu Singh

Submit
0 Answers