1

I have some problem to store arraylist value to mysql database with php. This is some code I have been created.

First, I have some class to store variable its called Constant.java :

public class Constant {

    public static final String FIRST_COLUMN = "First";
    public static final String SECOND_COLUMN = "Second";
    public static final String THIRD_COLUMN = "Third";
    public static final String FOURTH_COLUMN = "Fourth";

}

Second, I have been import that class to MainActivity.java like this :

import static com.testing.informationsystem.Constant.FIRST_COLUMN;
import static com.testing.informationsystem.Constant.SECOND_COLUMN;
import static com.testing.informationsystem.Constant.THIRD_COLUMN;
import static com.testing.informationsystem.Constant.FOURTH_COLUMN;

Third, I store my value to arraylist by this way :

        btnInsert = (Button) findViewById(R.id.btnInsert);
        btnInsert.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                orderValue = txtOrderValue.getText().toString();
                price = txtPrice.getText().toString();

                valueSpinner = spnProductFocus.getSelectedItem().toString();

                HashMap temp = new HashMap();

                if(orderValue.equalsIgnoreCase("")){
                    orderValue = "0";
                }

                if(price.equalsIgnoreCase("")){
                    price = "1000";
                }

                double count = Double.parseDouble(orderValue) + Double.parseDouble(price);
                total = String.valueOf(count);

                if(count.equalsIgnoreCase("")){
                    count = "0";
                }

                temp.put(FIRST_COLUMN, valueSpinner);
                temp.put(SECOND_COLUMN, orderValue);
                temp.put(THIRD_COLUMN, price);
                temp.put(FOURTH_COLUMN, total);

                list.add(temp);
           }
        });

Okay, now I have arraylist contains value I saved from button. And now I will send it through out HTTPOST to PHP and store it to mysql. This is my code to post it to PHP.

void saveOrder(){

        httpclient = new DefaultHttpClient();
        httppost = new HttpPost("http://dcmodular.com/saktisetia/mobile/order.php");
        nameValuePairs = new ArrayList<NameValuePair>(2);

       --nameValuePairs.add(new BasicNameValuePair(arraylist));--
        ----this is my problem about how to post it to PHP---

                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                response=httpclient.execute(httppost);
                Log.d("Value : ", response.toString());
                HttpEntity entity=response.getEntity();
                String feedback = EntityUtils.toString(entity).trim();
                Log.d("Feedback : ", feedback);

    }

Finally, this is my own php file to store it to mysql databases :

<?php

include('connection.php');

--receive arraylist from httppost--

$query = "insert into order(value1) values(value1)";

$execute = mysql_query($query) or die(mysql_error());

if($execute){
    echo "saved";
}else{
    echo "failed";
}

?>

That is my code and my problem how to post that arraylist to PHP. Thank you before for your helping.

5 Answers 5

1

Use HttpUrlConnection as Http Default client is no longer supported. Try this

 public JSONObject function(String value_to_post, String  value_to_post,....){
        try {


            HashMap<String, String> params = new HashMap<>();

            params.put("your_database_field", value_to_post);



            Log.d("request", "starting");

            JSONObject json = jsonParser.makeHttpRequest(
                    your_URL, "POST", params);

            if (json != null) {
                Log.d("JSON result", json.toString());

                return json;
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

call this function from your activity

Sign up to request clarification or add additional context in comments.

Comments

0

$_POST variable will hold your posted data.

4 Comments

Yes, I know that. But, how to pass the arraylist in android class ??
for each item in ArrayList add their BasicNameValuePair instance to nameValuePairs
It's similar to @sanky-jain post.
Sorry, I don't fully understood about that code. I don't know where I used that code.
0

//You have to something similar. //This is just an idean as i dont know how you declared temp and arraylist

for (int i = 0; i < type.length; i++) {

             nameValuePairs.add(new BasicNameValuePair(list.get(i).getkey,list.get(i).getValue));

        }

1 Comment

I declared the arraylist like this : private ArrayList<HashMap> list; and temp like this : HashMap temp = new HashMap();
0

In Android:

postParameters.add(new Pair<>("value1", value1.toString()));

In Php

$str = str_replace(array('[',']'),'',$_POST['value1']);
$str = preg_replace('/\s+/', '', $str);
$value1 = preg_split("/[,]+/", $str);
$i=0;
while($i<count($value1){
$query = "insert into order(value1) values(value1)";
}

10 Comments

Is variable 'your_data.toString()' arraylist ??
"value1" on postParameters is contain all arrayList value
Sorry, How to I declare postParameters ? What is the data type ?
nameValuePairs.add(new BasicNameValuePair("value1", value1.toString()));
but BasicNameValuePair is depreciated from 5.0
|
0

You can use List<NameValuePair> to send POST request to server.

 try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("param1", "data1"));
        nameValuePairs.add(new BasicNameValuePair("param2", "data2"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }

1 Comment

Yes, I used that but how to I post values in ArrayList using that nameValuePairs.add ??

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.