[英]Android/PHP: how to POST/GET JSON data from Android to php
Android/PHP: how to POST/GET JSON data from Android to php? Android / PHP:如何将JSON数据从Android发布/获取到PHP?
Currently I am stuck at a point where I am sending JSONObject data to php but getting NULL values always in response. 目前,我处于将JSONObject数据发送到php的位置,但始终会得到NULL值作为响应。
I am sending 'username' and 'password' from Android in the form of JSONObject and want to retrieve same data in JSONObject in PHP response. 我正在以JSONObject的形式从Android发送“用户名”和“密码”,并希望在PHP响应中检索JSONObject中的相同数据。
Here is my code snippet 这是我的代码片段
Android_file.java 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();
}
Login_check.php Login_check.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);
}
?>
Please let me know if I am doing something wrong or missing some point? 请让我知道我做错了什么或遗漏了什么吗? thanks in advance!
提前致谢!
I dont know what exactly is wrong with your code but I compared your code to mine which is working fine on diffrent apps and I made some changes to your code and hope that it works: 我不知道您的代码到底有什么问题,但是我将您的代码与我的代码进行了比较,这在不同的应用程序上都可以正常工作,我对您的代码进行了一些更改,并希望它能正常工作:
replace the part: 更换零件:
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);
With: 附:
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));
and this will be your web service (I think the problem is here): 这将是您的Web服务(我认为问题在这里):
<?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);
}
?>
Please let me know if it fixes your problem. 请让我知道是否可以解决您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.