繁体   English   中英

Android / PHP:如何将POST数据从Android发布/获取到PHP

[英]Android/PHP: how to POST/GET JSON data from Android to php

Android / PHP:如何将JSON数据从Android发布/获取到PHP?

目前,我处于将JSONObject数据发送到php的位置,但始终会得到NULL值作为响应。

我想要的是:

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

这是我的代码片段

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

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

这将是您的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);
  }
 ?>

请让我知道是否可以解决您的问题。

暂无
暂无

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

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