【发布时间】: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));
}
?>
【问题讨论】:
-
请出示您的代码...
-
发布代码以获得响应:)