gpt4 book ai didi

php - 安卓/PHP : how to POST/GET JSON data from Android to php

转载 作者:行者123 更新时间:2023-11-30 02:09:49 26 4
gpt4 key购买 nike

Android/PHP:如何从 Android 到 PHP POST/GET JSON 数据?

目前我被困在将 JSONObject 数据发送到 php 的位置,但是总是在响应中获取 NULL 值。

我想要什么:

我正在以 JSONObject 的形式从 Android 发送“用户名”和“密码”,并希望在 PHP 响应中检索 JSONObject 中的相同数据。

这是我的代码 fragment

Android_file.java

DefaultHttpClient httpClient=new DefaultHttpClient();
HttpPost httpPost=new HttpPost(register_user);
JSONObject jsonObject=new JSONObject();
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
try {

jsonObject.put("username",username.getText().toString());
jsonObject.put("password",password.getText().toString());
StringEntity se=new StringEntity("json="+jsonObject.toString());
//httpPost.addHeader("content-type", "application/x-www-form-urlencoded");
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
httpPost.setEntity(se);
String req=se.toString();

HttpResponse response=httpClient.execute(httpPost);


if(response!=null){
InputStream is=response.getEntity().getContent();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(is));
StringBuilder sb=new StringBuilder();
String line=null;
while((line=bufferedReader.readLine())!=null){
sb.append(line+"\n");
}
this.message=sb.toString();
//Toast.makeText(getBaseContext(), message, Toast.LENGTH_LONG).show();
}

} catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

登录检查.php

<?php
include('../core/init.php');

// $contents = file_get_contents('php://input');
// $contents = utf8_encode($contents);
if($_POST['json'])
{
$data=json_decode(stripslashes($_POST['json']));
$username=$data['username'];
$password=$data['password'];
$response=array('username'=>$username,'password'=>$password);
echo json_encode($response);

}
else{
$no_data=array('none'=>'no data received');
echo json_encode($no_data);
}

?>

如果我做错了什么或遗漏了某些要点,请告诉我?提前致谢!

最佳答案

我不知道你的代码到底出了什么问题,但我将你的代码与我的代码进行了比较,后者在不同的应用程序上运行良好,我对你的代码进行了一些更改,希望它能正常工作:

替换部分:

        jsonObject.put("username",username.getText().toString());
jsonObject.put("password",password.getText().toString());
StringEntity se=new StringEntity("json="+jsonObject.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json"));
httpPost.setEntity(se);

与:

        List<NameValuePair> params = new ArrayList<NameValuePair>(); 
params.add(new BasicNameValuePair("username", username.getText().toString()));
params.add(new BasicNameValuePair("password",password.getText().toString()));
httpPost.setEntity(new UrlEncodedFormEntity(params));

这将是您的网络服务(我认为问题出在这里):

<?php
include('../core/init.php');

// $contents = file_get_contents('php://input');
// $contents = utf8_encode($contents);
if(isset($_POST['username']) && isset($_POST['password']))
{
$username=$_POST['username'];
$password=$_POST['password'];
$response = array();
$response['username'] = $username;
$response['password'] = $password;
echo json_encode($response);
}
else{
$no_data=array();
$no_data['none']= 'no data received';
echo json_encode($no_data);
}
?>

如果它解决了您的问题,请告诉我。

关于php - 安卓/PHP : how to POST/GET JSON data from Android to php,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30260928/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com