1

Referring to various examples over stack, I wrote below code to send data to MySQL server table, but nothing is uploading there. Here is code for my Android method.

 public void sendtoserver(final String data) {
        Log.e("LWFAB", "Sending Data to Server");
        //Send data to server now
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    URL bulkdataurl = new URL("http://myphp.php?");
                    HttpURLConnection bulkcon = (HttpURLConnection) bulkdataurl.openConnection();
                    bulkcon.setConnectTimeout(15000);
                    bulkcon.setReadTimeout(15000);
                    bulkcon.setRequestMethod("POST");
                    bulkcon.setDoOutput(true);
                    BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(bulkcon.getOutputStream());
                    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(bufferedOutputStream));
                    bufferedWriter.write(data);
                    bufferedWriter.flush();
                    bufferedWriter.close();
                    bufferedOutputStream.close();
                    bulkcon.connect();
                    //OutputStreamWriter outputStreamWriter = new OutputStreamWriter(bulkcon.getOutputStream());
                    //outputStreamWriter.write(data);
                    //outputStreamWriter.flush();

                    //Get Server Response
                    BufferedReader bufferedReader;
                    bufferedReader = new BufferedReader(new InputStreamReader(bulkcon.getInputStream()));
                    StringBuilder stringBuilder = new StringBuilder();
                    String line = null;

                    while ((line = bufferedReader.readLine()) != null) {
                        stringBuilder.append(line + "\n");
                    }
                    System.out.println(stringBuilder.toString());
                    bufferedReader.close();

                    System.out.println("Sending Bulk Data " + bulkcon);


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

    }
}

I m not getting any error. Below is my php code

<?php
require_once("../dbconnect.php"); 

$json = file_get_contents('php://input');
$obj = json_decode($json, true);
error_log(print_r($obj, true));
$array_data = $obj["data"];
print_r($obj["data"]);


foreach ($array_data as $row) {
    print_r($row);
    $sql = "INSERT INTO test (vid, name,branch,date,time) VALUES ('" . $row["vid"] . "', '" . $row["name"] . "','".$row["branch"]."','".$row["date"]."','".$row["time"]."')";

    echo "$sql<br>";
    $conn->query($sql);
}

$conn->close();
?>

And sample of data being sent as string is as below:

{"data":[{"vid":"1","name":"raj","branch":"mech","date":"2-Sep-2017","time":"13:03:24 PM"}]}

Can u guide if there is something to fix.

10

1 Answer 1

3

You are using cursor.getInt(1) which will return an integer from the cursor. You need to use cursor.getString() in Order to get the string and put them correctly in JSON.

This is a small example:

jsonObject.put("vid", curser.getString(cursor.getColumnIndex("vid")));
Sign up to request clarification or add additional context in comments.

3 Comments

now getting correct format of json, still no data being uploaded? what else left, ps guide.
1- check the URL. is it public ? accissable ? 2- check the connect to db try to add something like this to make sure you are connected : $con = mysql_connect('localhost','root','123456') or die('Cannot connect to the DB'); 3- try to add some headers URLConnection.setRequestProperty("Content-Type", "application/json"); 4- echo something on the server to see if you are recieving the request or not: echo json_encode($json));
glad to help :).

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.