【问题标题】:Cannot see JSON response from php in android在android中看不到来自php的JSON响应
【发布时间】:2016-04-12 22:50:00
【问题描述】:

我正在尝试通过我的 android 应用程序进行用户注册,并且使用 php 将数据存储在数据库中。一切正常,即数据已插入数据库,但在 android 中看不到 JSON 响应。

这是我的代码

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

Php 脚本工作正常,数据已插入数据库,但在我的应用程序中看不到 echo json_encode($output) 的输出。

Log.e("Data", String.valueOf(json)); 在日志中获取空值。

我已经检查了 php 错误日志我会得到这个

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

我认为这是在我的应用程序中获得空 json 响应的原因。但是插入查询工作正常 什么问题。请帮帮我?

【问题讨论】:

  • 你的functions.php第21行是什么?
  • 这一行$data = json_decode($_POST['POST'], true);
  • 你为什么要解码数据?我在您的 java 代码中的任何地方都没有看到编码数据?您是否将 namevaluepair 编码为 json 以便您必须从 php 中对其进行解码?
  • @mrun 但插入查询工作正常
  • HttpClient 已弃用,请改用 HttpUrlConnection。

标签: php android mysql json


【解决方案1】:

通常当你需要从 php 输出 json 时,你需要一个内容类型的标头,这可能是你缺少的。

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

【讨论】:

    【解决方案2】:

    您有一个url,但使用了两次。

    首先使用 HttpClient,您发布了一些数据,但没有对 HttpResponse response = httpclient.execute(httppost); 做任何事情

    之后,您使用 json 解析器调用相同的 url。没有数据。

    一切都毫无意义。

    如果你说你不能得到回应,你在说什么?

    `

    【讨论】:

      猜你喜欢
      • 2013-03-08
      • 2015-02-14
      • 2015-11-24
      • 2012-05-04
      • 1970-01-01
      • 2019-05-09
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      相关资源
      最近更新 更多