Android Saving Data From Json to mysql

Android Saving Data From Json to mysql
0

#1

Hello I Need Help Saving Data To The Following Table to mysql from androd Application

{“data”:[{“id”:“1”,“S_Person”:“M m”,“Amount”:“20”,“Driver”:“Mac”,“Company_Name”:“Ps”,“V_Number”:“T568”,“D_Number”:“005380055”,“Decode”:"",“Bd”:"",“Ks”:“12”,“LD”:“Va”,“PP”:"",“Date”:“2017”}]}


#2

Well, I’m not an expert! But I think you’ll need to do an AJAX request to a php script (or other backend language script).

Normaly I use this method…

Regards


#3

Hello Its An Android App i need to send those data to mysql through json Here Is My Main Activity

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import static practise.mysql_php_json.R.id.salesPerson;

public class MainActivity extends AppCompatActivity {

EditText Sales_Person, Amount, Driver, Company_Name, Vehicle_Number, Driver_Number, Device, Border, Kilometers, TZLD, Port_Point;
Button insert, show;
RequestQueue requestQueue;
String insertUrl = "http://127.0.0.1/v4/insert.php";
String showUrl = "http://127.0.0.1/v4/show.php";
TextView result;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Sales_Person = (EditText) findViewById(salesPerson);
    Amount = (EditText) findViewById(R.id.Amount);
    Driver = (EditText) findViewById(R.id.Driver);
    Company_Name = (EditText) findViewById(R.id.Companyname);
    Vehicle_Number = (EditText) findViewById(R.id.vehiclenumber);
    Driver_Number = (EditText) findViewById(R.id.drivernumber);
    Device = (EditText) findViewById(R.id.device);
    Border = (EditText) findViewById(R.id.border);
    Kilometers = (EditText) findViewById(R.id.kilometers);
    TZLD = (EditText) findViewById(R.id.tzld);
    Port_Point = (EditText) findViewById(R.id.portpoint);
    insert = (Button) findViewById(R.id.insert);
    show = (Button) findViewById(R.id.showects);
    result = (TextView) findViewById(R.id.textView);
    requestQueue = Volley.newRequestQueue(getApplicationContext());
    show.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            System.out.println("ww");
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,
                    showUrl, new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    System.out.println(response.toString());
                    try {
                        JSONArray ects = response.getJSONArray("ects");
                        for (int i = 0; i < ects.length(); i++) {
                            JSONObject ect = ects.getJSONObject(i);
                            String Sales_Person = ect.getString("salesPerson");
                            String Amount = ect.getString("Amount");
                            String Driver = ect.getString("Driver");
                            String Company_Name =  ect.getString("Companyname");
                            String Vehicle_Number = ect.getString ("vehiclenumber");
                            String Driver_Number = ect.getString("drivename");
                            String Device = ect.getString("device");
                            String Border = ect.getString("border");
                            String Kilometers = ect.getString("kilmetres");
                            String TZLD = ect.getString("tzld");
                            String Port_Point = ect.getString("portpoint");

      result.append(Sales_Person + " " + Amount + " " + Driver + "" + Company_Name + " " + Vehicle_Number + " " + Driver_Number + " " + Device  + " " +  Kilometers + "" + TZLD + "" + Port_Point +" " + Border +  "  \n");
                        }
                        result.append("===\n");
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    System.out.append(error.getMessage());

                }
            });
            requestQueue.add(jsonObjectRequest);
        }
    });

    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("Salesperson",Sales_Person.getText().toString());
                    parameters.put("Amount",Amount.getText().toString());
                    parameters.put("Driver",Driver.getText().toString());
                    parameters.put("CompanyName",Company_Name.getText().toString());
                    parameters.put("VehicleNumber",Vehicle_Number.getText().toString());
                    parameters.put("DriverNumber",Driver_Number.getText().toString());
                    parameters.put("Device",Driver.getText().toString());
                    parameters.put("Border",Border.getText().toString());
                    parameters.put("Kilometers",Kilometers.getText().toString());
                    parameters.put("TZLD",TZLD.getText().toString());
                    parameters.put("portpoint",Port_Point.getText().toString());
                    return parameters;
                }
            };
            requestQueue.add(request);
        }

    });

}

#4

I’m sorry, my fault! :sweat_smile:
You are using JAVA, I thought your question are about how to do it with javascript application!

I don’t have acknowledgement to help you! :sweat:


#5

Cool
But I Hope Someone Can Be A Great Help in Here


#6

As I see your are doing POST call to local i.elocal .
What exactly error you are getting ? OR what do you want to achieve with it ?

are you getting response from those API hits ?


#7

Hello kathisai
Am Not sending Yet I Need A Method To Do So
i tryied directly from mysql
here is what result i need from post ,
{“data”:[{“id”:“1”,“S_Person”:“M m”,“Amount”:“20”,“Driver”:“Mac”,“Company_Name”:“Ps”,“V_Number”:“T568”,“D_Number”:“005380055”,“Decode”:"",“Bd”:"",“Ks”:“12”,“LD”:“Va”,“PP”:"",“Date”:“2017”}]}