简体   繁体   English

获取HTTP响应PHP和ANDROID

[英]Getting http response PHP and ANDROID

I'm trying to get HTTP response. 我正在尝试获取HTTP响应。 It doesn't work on client side. 它在客户端不起作用。 Tried to get response with 'Postman' and it worked. 试图获得“邮递员”的回应,并且成功了。

  1. I'm sure everything is OK, from PHP side. 从PHP方面,我确定一切正常。
  2. Maybe it's because classes are deprecated. 也许是因为不推荐使用类。

Android code: ` Android代码:

            //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 "org.apache.http.message.BasicHttpResponse@32857430"`

PHP CODE: ` PHP代码:

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

` `

Any help would be appreciated! 任何帮助,将不胜感激!

I managed to fix the problem: 我设法解决了这个问题:

WHAT I DID WRONG: 我错了什么:

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

WHAT I DID TO FIX 我要解决的问题

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

Not sure why, but putOpt method somehow, fixed it. 不知道为什么,但是putOpt方法以某种方式对其进行了修复。 If you know why, feel free to response. 如果您知道原因,请随时回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM