Handling various php responses after requesting javascript http message

I am developing an Android application that uses PHP / MySQL to send data from the application to the server for registration / login. I have already written Javascript and PHP files to send and receive JSON data and insert it into the MySQL database. I have a problem in how to handle different answers from PHP.

Exemple:

<?php

   //decode json object
   $json = file_get_contents('php://input');
   $obj = json_decode($json);

   //variables
   $user_firstname = $obj->{'user_firstname'};
   $user_lastname = $obj->{'user_lastname'};
   $user_email = $obj->{'user_email'};
   $user_password = $obj->{'user_password'};

   if([variables] != null){
      //check for an existing email in db
      mysql_query("Check if email exists");

      if(email exist){
         //pass a response to java file
         return user_exists;
         die();
      }else{
         //success
         return success;
      }
   }

?>

All I want to do is handle these different return values ​​in order to interact with the user inside the Android application.

+3
source share
2 answers

, HTTP. , php script HTTP 200 - OK HTTP 409 Conflict, . API RESTfull. Android , .

DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httpget = new HttpPost("http://www.google.com/");

HttpResponse response = httpclient.execute(httpget);
int statusCode = response.getStatusLine().getStatusCode();
+2

json-, json_encode(), , java-. Content-Type, header() . HTTP. - :

$responseArray = array('response' => 'success'); // or 'user_exists'
$responseJson = json_encode($responseArray);
header('HTTP/1.1 200 OK'); // or 4xx FAIL_TEXT on failure
header('Content-Type: application/json');
echo $responseJson;

java-.

+1

All Articles