Unable to see json response from php in android

I am trying to do user registration through my Android application, and the data is in the database using php. Everything works fine, i.e. data is inserted into the database but cannot 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", "steave@apple.com");
            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);
    }

The PHP script works fine, and the data is inserted into the database, but I do not see the output echo json_encode($output)in my application.

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

I checked the php error log, I will 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 a blank json response in my application. But the insert request works fine. What's the problem. Please help me?

+4
source share
2

url, .

HttpClient , HttpResponse response = httpclient.execute(httppost);

URL- json. .

.

, , ?

`

+4

, json php, , , , .

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

Source: https://habr.com/ru/post/1623401/


All Articles