dtpn60029 2014-05-22 15:52
浏览 41
已采纳

将多个数据从Android发布到PHP Web服务

So far I can send one set of data from Android to PHP web service. I'm hoping to send an array, and have a for loop in the web service. Is that possible? Is that a good solution? In this project, I'm trying to sync SQLite with MySQL.

Here's my code

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 + "
");
        }
        text = sb.toString();
    } catch (Exception ex) {

    } finally {
        try {
            reader.close();
        } catch (Exception ex) { }
    }

And this

<?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();
}
?>
  • 写回答

1条回答 默认 最新

  • drh78568 2014-05-22 16:01
    关注

    You could send the input as JSON and then get PHP to decode it.

    So you'd encode all your data in the android app and send it as one big encoded string, possibly using URI encoding or Base64, then in the PHP do:

    $data = json_decode($_POST['data']);
    

    If you use base64 then you'd do:

    $data = json_decode(base64_decode($_POST['data']));
    

    If you just send it as one string

    $data = json_decode(urldecode($_POST['data']));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向