到目前为止,我可以将一组数据从Android发送到PHP Web服务。我希望发送一个数组,并在Web服务中有一个for循环。那可能吗?这是一个好的解决方案吗?在这个项目中,我正在尝试将SQLite与MySQL同步。
这是我的代码
String username, userid;
username = "Kate";
userid = "3";
String data = "";
try {
data = URLEncoder.encode("username", "UTF-8") + "="
+ URLEncoder.encode(username, "UTF-8");
data += "&" + URLEncoder.encode("userid", "UTF-8") + "="
+ URLEncoder.encode(userid, "UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String text = "";
BufferedReader reader = null;
try {
URL url = new URL(
address);
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(
conn.getOutputStream());
wr.write(data);
wr.flush();
// Get the response
reader = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
text = sb.toString();
} catch (Exception ex) {
} finally {
try {
reader.close();
} catch (Exception ex) { }
}
这个
<?php
include '../inc/connect.php';
include '../inc/class/mysql.class.php';
$name = urldecode($_POST['username']);
$user = urldecode($_POST['userid']);
print " ==== POST DATA =====
Name : $name
Email : $email
User : $user
Pass : $pass";
$ins = mysql_query("INSERT INTO users (UserName, FullName) VALUES ('$name','$user')") or die(mysql_error());
if ($ins) {
echo json_encode(array("result"=>"success","result_txt"=>"Branch sucessfully added."));
exit();
}
?>
答案 0 :(得分:1)
您可以将输入作为JSON发送,然后让PHP对其进行解码。
所以你在android应用程序中对所有数据进行编码并将其作为一个大的编码字符串发送,可能使用URI编码或Base64,然后在PHP中执行:
$data = json_decode($_POST['data']);
如果您使用base64,那么您可以:
$data = json_decode(base64_decode($_POST['data']));
如果您只是将其作为一个字符串发送
$data = json_decode(urldecode($_POST['data']));