Mohit Wadhwani
Mohit Wadhwani

Reputation: 45

POST array list from android application to php page

I am new to android and got entangle in problem. what i want is to pass an array object or say an arraylist to a php page so that the same can be inserted into the mysql database.To be more precise i would like to develop an app to sync my contacts with phone numbers to my own server. thanks in advance.

Upvotes: 2

Views: 2885

Answers (3)

Kelo Adler
Kelo Adler

Reputation: 13

Try this

ArrayList <NameValuePair> postparametersSend = new ArrayList <NameValuePair> (); 
      //This is your file php
      String URL = "www.yourserver.com/file.php"; 

postparameters2send.add (new BasicNameValuePair ("param1", nameParam1)); 
postparameters2send.add (new BasicNameValuePair ("param2", nameParam2)); 
postparameters2send.add (new BasicNameValuePair ("param3", nameParam3)); 

// perform a request and response obtenes JSON array 
       JSONArray jdata = con.getserverdata (postparametersSend, URL); 
       
       //since we are working locally on return will almost immediately 
       //To give a little realism we say that the process is stopped for a few seconds to 
       //Observe progressdialog 
      // We can remove if we 
       


// if what we got is not null 
if (jdata! = null && jdata.length ()> 0) {

JSONObject json_data; // create a JSON object 
try {
jdata.getJSONObject json_data = (0); // read the first segment in our case the only 
System.out.println (json_data); 
logstatus = json_data.getInt ("logstatus"); // access the value 
Log.e ("LoginStatus", "logstatus =" + logstatus) // show by log we obtained 
} Catch (JSONException e) {
// TODO Auto-generated catch block 
e.printStackTrace (); 
} 

// validate the value obtained 
if (logstatus == 0) {// [{"logstatus": "0"}] 
Log.e ("logStatus", "invalid"); 
return false; 
} 
else {// [{"logstatus": "1"}] 
Log.e ("logStatus", "valid"); 
return true; 
} 

//} else {invalid json obtained WEB verify part. 
Log.e ("JSON", "ERROR"); 
return false; 
}

To make the variables you should put this in the php file

<? php 

$nameParam1 = $_POST ["nameParam1"]; 
$nameParam2 = $_POST ["nameParam2"]; 
$nameParam3 = $_POST ["nameParam3"]; 

<- Put your code here -> 

?>

Upvotes: 0

Remees M Syde
Remees M Syde

Reputation: 2609

Try like this.

Map<String,Object> productimages = new HashMap<String, Object>();
List<String> datas = new ArrayList<String>();
datas.add("image");
datas.add("small_image");
datas.add("thumbnail");
productimages.put("types",datas);

If your using nemevalue pair use like this.

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();  
nameValuePairs.add(new BasicNameValuePair("colors[]","red"));  
nameValuePairs.add(new BasicNameValuePair("colors[]","white"));  
nameValuePairs.add(new BasicNameValuePair("colors[]","black"));  
nameValuePairs.add(new BasicNameValuePair("colors[]","green")); 

Upvotes: 1

Bart Jan
Bart Jan

Reputation: 77

You should be using an API for ex.Rest API. This API will grant the information from your app by an POST action (from you're android app). (transfer data-objects like an array with JSON) Php can decode the JSON and you analyze you're array in php. You can now send specific data into your tables using MYSQL, PDO,..

If the data you transfer needs to be protected, you should use SSL encryption on you're api acces url

Upvotes: 0

Related Questions