0

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 + "\n");
        }
        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 Answer 1

1

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']));
Sign up to request clarification or add additional context in comments.

Comments

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.