0

I'm trying to get HTTP response. It doesn't work on client side. Tried to get response with 'Postman' and it worked.

  1. I'm sure everything is OK, from PHP side.
  2. Maybe it's because classes are deprecated.

Android code: `

            //Connect to mysql.
            HttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost("www.example.com/register.php");

            //JSON object.
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("username", register_username);
            jsonObject.put("password", register_password);

            //Entity builder to send value.
            EntityBuilder entityBuilder = EntityBuilder.create();
            entityBuilder.setText(jsonObject.toString());
            httpPost.setEntity(entityBuilder.build());

            //Getting response
            HttpResponse response = httpClient.execute(httpPost);
            String responseBody = EntityUtils.toString(response.getEntity());
            //When I try to print it out, I get value of "org.apache.http.message.BasicHttpResponse@32857430"`

PHP CODE: `

JSON EXAMPLE:
<?php
$con=mysqli_connect("localhost","tx","xxx","txxxt");

$data = json_decode(file_get_contents('php://input'));
$username = $data->{'username'};
$password = $data->{'password'};


$query = mysqli_query($con, "SELECT * FROM user WHERE username='$username'");

if(mysqli_num_rows($query) > 0){
$response = new stdClass();
$response->status="Already exists";
$response->code=0;
echo json_encode($response );}

else{
$response = new stdClass();
$response->status="Registered";
$response->code=1;
echo json_encode($response );

$sql_query = "insert into user values('$username','$password', 'null', 'null' ,'null');";
mysqli_query($con, $sql_query) or die (mysqli_error($con));

}


mysqli_close($con);
?>

`

Any help would be appreciated!

Benas.M
  • 340
  • 5
  • 14
  • Your code is also open to SQL injection attacks. You should use prepared statements with bound parameters to prevent that. – Mike May 02 '16 at 17:47
  • can you give some references to this information. I'm new at it :) – Benas.M May 02 '16 at 17:49
  • Sure. Here you go: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Mike May 02 '16 at 18:27

1 Answers1

0

I managed to fix the problem:

WHAT I DID WRONG:

   //JSON object.
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("username", register_username);
            jsonObject.put("password", register_password);

            //Entity builder to send value.
            EntityBuilder entityBuilder = EntityBuilder.create();
            entityBuilder.setText(jsonObject.toString());
            httpPost.setEntity(entityBuilder.build());

            //Getting response
            HttpResponse response = httpClient.execute(httpPost);
            String responseBody = EntityUtils.toString(response.getEntity());
            JSONObject responseObject = new JSONObject(responseBody);

WHAT I DID TO FIX

  //JSON object.
            JSONObject jsonObject = new JSONObject();
            jsonObject.putOpt("username", register_username);
            jsonObject.putOpt("password", register_password);

            //Entity builder to send value.
            EntityBuilder entityBuilder = EntityBuilder.create();
            entityBuilder.setText(jsonObject.toString());
            httpPost.setEntity(entityBuilder.build());

            //Getting response
            HttpResponse response = httpClient.execute(httpPost);
            String responseBody = EntityUtils.toString(response.getEntity());
            JSONObject responseObject = new JSONObject(responseBody);

Not sure why, but putOpt method somehow, fixed it. If you know why, feel free to response.

Benas.M
  • 340
  • 5
  • 14