4

I am trying to do a user registration through my android app and data is stord in database using php. Everything works fine ie, data is inserted into database but cant see the JSON response in android.

Here is my code

Java

HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(url);

//         Add your data

        try {
            jsonParams.put("user_name", "Steve Jobs");
            jsonParams.put("user_email", "[email protected]");
            jsonParams.put("user_password", "nopassword");
            jsonParams.put("user_phone_number", "1234567890");
            jsonParams.put("club_member_id", "24");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("POST", jsonParams.toString()));
            Log.e("mainToPost", "mainToPost" + nameValuePairs.toString());
            // Use UrlEncodedFormEntity to send in proper format which we need
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            // Execute HTTP Post Request
            HttpResponse response = httpclient.execute(httppost);


        } catch (JSONException e) {

        }

//        HttpClient httpclient = new DefaultHttpClient();
//        HttpPost httppost = new HttpPost(url);
//        ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
//        nameValuePairs.add(new BasicNameValuePair("user_name", "Steve"));
//        nameValuePairs.add(new BasicNameValuePair("user_email", "jjdnjd"));
//        nameValuePairs.add(new BasicNameValuePair("user_password", "dcds"));
//        nameValuePairs.add(new BasicNameValuePair("user_phone_number", "2343"));
//        nameValuePairs.add(new BasicNameValuePair("club_member_id", "24"));
//        Log.e("Params", String.valueOf(nameValuePairs.toString()));
//        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//        HttpResponse response = httpclient.execute(httppost);
        // Creating new JSON Parser
        JSONParser jParser = new JSONParser();

        // Getting JSON from URL
        JSONObject json = jParser.getJSONFromUrl(url);
        Log.e("Data", String.valueOf(json));

PHP

 $output=array();
    $data = json_decode($_POST['POST'], true);
    if(!empty($data)){
           if(user_exists($data['user_email'])){
                $result = db_conncet()->prepare("INSERT INTO `mir_users`(`name`, `password`, `email`, `phone_number`, `club_member_id`) VALUES (?,?,?,?,?)");
                $data['club_member_id']=(isset($data['club_member_id']))?$data['club_member_id']:'NULL';
                $temp=array(
                    $data['user_name'],
                    password_hash($data['user_password'],PASSWORD_BCRYPT),
                    $data['user_email'],
                    $data['user_phone_number'],
                    $data['club_member_id']
                );
                   $result->execute($temp);
                   if($result->rowCount()){
                       $output=array(
                         'status'=>true,
                         'code'=>'103',
                         'message'=>'Registration success'
                         ); 
                   }else{
                       $output=array(
                         'status'=>false,
                         'code'=>'102',
                         'message'=>'Registration error'
                         );
                   }
           }else{
                     $output=array(
                           'status'=>false,
                           'code'=>'102',
                           'message'=>'Email-ID Already taken'
                           );
           }
       echo json_encode($output);
    }

Php script is working fine and the data is inserted into the database,but i can't see output of echo json_encode($output) in my app.

Log.e("Data", String.valueOf(json)); getting null value in log.

I've checked the php error log i wll get this

PHP Notice:  Undefined index: POST in /home/zama2n/public_html/miradmin/api/functions.php on line 21

i think this is the reason for getting empty json response in my app. But the insert query is working fine What is the problem .Please help me?

5
  • what is your functions.php line 21? Commented Jan 8, 2016 at 9:49
  • This line $data = json_decode($_POST['POST'], true); Commented Jan 8, 2016 at 9:50
  • why are you decoding data? i dont see encoded data anywhere in your java code? do you encode namevaluepair into json so that you have to decode it from php? Commented Jan 8, 2016 at 9:52
  • @mrun but the insert quey working fine Commented Jan 8, 2016 at 9:54
  • HttpClient is deprecated use HttpUrlConnection instead. Commented Jan 8, 2016 at 9:59

2 Answers 2

4

You have one url but use it twice.

First with HttpClient you are posting some data but aren't doing anything with HttpResponse response = httpclient.execute(httppost);

After that you call the same url with a json parser. Without data.

All makes no sense.

Where are you talking about if you state that you cannot get a response?

`

Sign up to request clarification or add additional context in comments.

Comments

0

Usually when u need to output json from php you need a header with a content type, might be what you are missing.

Header('Content-Type: application/json; charset: UTF-8');
echo json_encode($output);
die();

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.