I am trying to retrieve some values from the server database through Php in Android. There are rows in the server that match the conditions in the where clause in php, but the json array is returned empty. My php code is below. I think the $username variable is not receiving the values as when I initialise $username="11111", the json array is dispalyed.Please explain me step by step where I am going wrong.
<?php
mysql_connect("localhost","uname","pswd");
mysql_select_db("demo");
$username= (isset($_POST['receivenumber'])) ? $_POST['receivenumber'] : '';
$q=mysql_query("SELECT `TaskId` FROM `addtasknew` where `receivenumber` = '$username'") or die(mysql_error());
$output=array();
while($e=mysql_fetch_assoc($q))
$output[]=$e;
print (json_encode($output));
mysql_close();
?>
Android code
final String name=c.get("ph");
AsyncTask<Void, Void,Void> t = new AsyncTask<Void, Void,Void>()
{
@Override
protected Void doInBackground(Void... arg0) {
String result = null;
InputStream is = null;
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.mydomainname.org/task/senttask.php");
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("receivenumber",name.toString()));
//nameValuePairs.add(new BasicNameValuePair("receivenumber","9595959595"));
HttpResponse response = httpclient.execute(httppost);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpEntity entity = response.getEntity();
is = entity.getContent();
Log.e("log_tag", "connection success "+nameValuePairs);
}
catch(Exception e)
{
Log.e("log_tag", "Error in http connection "+e.toString());
Toast.makeText(getActivity(), "Connection fail", Toast.LENGTH_SHORT).show();
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,HTTP.UTF_8),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
result=sb.toString();
Log.e("log_tag", "result "+result.toString());
}
catch(Exception e)
{
Log.e("log_tag", "Error converting result "+e.toString());
Toast.makeText(getActivity(), " Input reading fail", Toast.LENGTH_SHORT).show();
}
try
{
JSONArray jArray = new JSONArray(result);
String s="",s1,s2,s3,s4,s5;
Log.w("Lengh",""+jArray.length());
for(int i=0;i<jArray.length();i++){
// final ListView lview=(ListView) rootView.findViewById(R.id.listViewmytask);
JSONObject json_data = jArray.getJSONObject(i);
s=json_data.getString("TaskId");
Log.e("taskid from server","s");
}
}
catch(JSONException e)
{
Log.e("log_tag", "Error parsing data "+e.toString());
Toast.makeText(getActivity(), "JsonArray fail", Toast.LENGTH_SHORT).show();
}
return null;
}
};
t.execute();