I'm trying to create an android application using a database on a remote server using php and json. The query I'm running suppose to return one row as an answer and it seems that there is a problem because the json array always returns empty. This is my JSON class code:
public class JSONClass extends AsyncTask<String,Void,String>{
public JSONArray res = null;
private String link;
private Context context;
public JSONClass(Context context, int queryNo, String params) {
this.context = context;
link = "http://pickupfriend.fulba.com/android_project/query" + queryNo + ".php" + params;
}
@TargetApi(Build.VERSION_CODES.KITKAT)
@Override
protected String doInBackground(String... arg0) {
try{
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(link);
post.addHeader("Content-Type", "application/x-www-form-urlencoded");
HttpResponse response = client.execute(post);
HttpEntity entity = response.getEntity();
String result = EntityUtils.toString(entity, "utf-8");
res = new JSONArray(result);
}catch(Exception e){
}
return "";
}
}
And this is how I use the class: EditText userNam = (EditText)findViewById(R.id.userNamTxt); EditText pass = (EditText)findViewById(R.id.pssTxt);
String uName,Pss;
uName = userNam.getText().toString();
Pss = pass.getText().toString();
String str = "?uname=" + uName + "&password=" + Pss;
JSONClass jClass = (JSONClass) new JSONClass(this, 1, str).execute();
res = jClass.res;
The php link is: "http://pickupfriend.fulba.com/android_project/query1.php?uname=itzick&password=maccabi" and it returns this result: "[{"UserID":"1"}]". When I check the results as a jsonArray (my res), its length is always 0. Can someone please tell me where I'm making a mistake? Thank you in advance!