我有一些问题用php存储arraylist值到mysql数据库。这是我创建的一些代码。
首先,我有一些类来存储变量,它名为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";
}
其次,我已将该类导入MainActivity.java,如下所示:
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;
第三,我通过这种方式将我的价值存储在arraylist中:
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);
}
});
好的,现在我有arraylist包含我从按钮保存的值。现在我将它通过HTTPOST发送到PHP并将其存储到mysql。这是我将它发布到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);
}
最后,这是我自己的php文件,用于将其存储到mysql数据库中:
<?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";
}
?>
这是我的代码和我的问题如何将该arraylist发布到PHP。谢谢你的帮助。
答案 0 :(得分:1)
使用HttpUrlConnection作为Http不再支持默认客户端。 试试这个
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;
}
从您的活动中调用此功能
答案 1 :(得分:0)
$_POST
变量将保留您发布的数据。
答案 2 :(得分:0)
在Android中:
postParameters.add(new Pair<>("value1", value1.toString()));
在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)";
}
答案 3 :(得分:0)
//你必须有类似的东西。 //这只是一个想法,因为我不知道你如何宣称临时和arraylist
for (int i = 0; i < type.length; i++) {
nameValuePairs.add(new BasicNameValuePair(list.get(i).getkey,list.get(i).getValue));
}
答案 4 :(得分:0)
您可以使用List<NameValuePair>
向服务器发送POST请求。
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
}