Receiving a null value from android in php file
I have written a code in android as as well as in php..while sending data from emulator and php in localhost it is working fine..but when i change the server to www.xxx.com/test.php, it is sending a null value from a real android device and null value is saving in the mysql database..
What might be the problem?
Thanks in advance..
My Code: InputStream is;
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 Code:
<?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));
}
?>
In my case I can do this way,
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", "");
}
Hope this will help you.. Thnx.
In a case like this, the key is localizing the problem, finding out on which layer it occurs. The first step would be to print or otherwise debug all $_POST data in the beginning of php file, so you'll know if and in what form the (almost)raw data gets to server. You can guess, where to go next:
- if tha data arrives properly to php, you should debug, what happens to it later in your php
- if it doesn't, go debug variables in your Android script, starting from the point where Android finally posts.
Just checking your code, line-by-line, is not the way to go.
精彩评论