2016-05-02 12 views
0

Ich versuche HTTP-Antwort zu bekommen. Es funktioniert nicht auf der Client-Seite. Versucht, Antwort mit "Postman" zu bekommen und es hat funktioniert.Getting HTTP-Antwort PHP und ANDROID

  1. Ich bin mir sicher, dass alles in Ordnung ist, von der PHP-Seite.
  2. Vielleicht, weil Klassen veraltet sind.

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 "[email protected]"` 

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); 
?> 

`

Jede mögliche Hilfe würde geschätzt!

+0

Ihr Code ist auch für SQL-Injection-Angriffe offen. Sie sollten vorbereitete Anweisungen mit gebundenen Parametern verwenden, um dies zu verhindern. – Mike

+0

können Sie einige Hinweise auf diese Informationen geben. Ich bin neu darin :) –

+0

Sicher. Hier gehts: http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – Mike

Antwort

0

ich es geschafft, das Problem zu beheben:

Was habe ich falsch:

//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); 

WAS ICH

//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); 

Nicht sicher FIX DID warum , aber putOpt-Methode irgendwie, repariere es. Wenn Sie wissen, warum, zögern Sie nicht zu antworten.