【问题标题】:Receiving a null value from android in php file在 php 文件中从 android 接收空值
【发布时间】:2011-08-25 05:33:18
【问题描述】:

我已经在 android 和 php 中编写了代码..同时从 localhost 中的模拟器和 php 发送数据时它工作正常..但是当我将服务器更改为 www.xxx.com/test.php 时,它正在从真正的 android 设备发送一个空值,并且空值保存在 mysql 数据库中..

可能是什么问题?

提前谢谢..

我的代码: 输入流是;

HttpPost httppost = new HttpPost("http://xxx.com/usercheck.php");
HttpClient httpclient = new DefaultHttpClient();

JSONObject json = new JSONObject(); 


    try {

        json.put("username",txtusername.getText().toString()); 
        json.put("password",txtpassword.getText().toString());           

        List<NameValuePair> parameters = new ArrayList<NameValuePair>(2);
        parameters.add(new BasicNameValuePair("userpwd_value",json.toString()));

        httppost.setEntity(new UrlEncodedFormEntity(parameters));  

        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = httpclient.execute(httppost, responseHandler);
        //This is the response from a php application
        String reverseString = response;
            Toast.makeText(this, "response" + reverseString, Toast.LENGTH_LONG).show();
        } catch (ClientProtocolException e) {
            Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
        // TODO Auto-generated catch block
        } catch (IOException e) {
            Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
        // TODO Auto-generated catch block
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

PHP 代码:

<?php
  require_once('config.php');
    //$data = file_get_contents('php://input');//tried this also
   $a=$_POST['userpwd_value']; //accesing value from android
   $b=json_decode($a); //decoding android value using JSON

  $username=$b->{'username'}; //assigning username from android to a variable  
  $password=$b->{'password'};//assigning password from android to a variable  

  //echo $username.$password;
  if($username=="" || $password=="")
    $output[]=array("value"=>"false");
  else
    {   

      $check = mysql_query("select userid,password from xxx where userid='".$username."'");

      $row = mysql_fetch_array($check);

      if($row['userid']==$username && $row['password']==$password)
        {
          $output[]=array("value"=>"true");
        } 
      else 
      { 
          $output[]=array("value"=>"false");
      }
      print(json_encode($output));
   }  
 ?>

【问题讨论】:

  • 请出示您的代码...
  • 发布代码以获得响应:)

标签: php android json


【解决方案1】:

就我而言,我可以这样做,

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);

httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
        // Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
        // get response entity
HttpEntity entity = response.getEntity();

        // convert entity response to string
 if (entity != null) {

    InputStream is = entity.getContent();
    // convert stream to string
    result = convertStreamToString(is); 
    result = result.replace("\n", "");
    }

希望这会对你有所帮助.. 谢谢。

【讨论】:

    【解决方案2】:

    在这种情况下,关键是定位问题,找出问题发生在哪一层。第一步是打印或以其他方式调试 php 文件开头的所有 $_POST 数据,因此您将知道(几乎)原始数据是否以及以何种形式到达服务器。 你可以猜到,接下来要去哪里:

    • 如果数据正确到达 php,您应该调试,稍后在您的 php 中会发生什么
    • 如果没有,请从 Android 最终发布的位置开始调试 Android 脚本中的变量。

    仅仅逐行检查你的代码是不可行的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-17
      • 2012-09-16
      相关资源
      最近更新 更多