I have tried looking at several links but I cannot seem to get it right. I am trying to send a JSON object from my android application which contains 'username' and 'password'. But I am not sure if this data is actually been sent to the web-service. I am not too sure if I got the code right for reading JSON object in the php script.
JSONObject jsonObject = new JSONObject();
String userID = "";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(loginURI);
HttpParams httpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
HttpConnectionParams.setSoTimeout(httpParams,10000);
try {
jsonObject.put("username", username);
jsonObject.put("password", password);
JSONArray array = new JSONArray();
StringEntity stringEntity = new StringEntity(jsonObject.toString());
stringEntity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httpPost.setEntity(stringEntity);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity entity = httpResponse.getEntity();
if (entity != null) {
userID = EntityUtils.toString(httpResponse.getEntity());
Log.i("Read from server", userID);
}
}catch (IOException e){
Log.e("Login_Issue", e.toString());
}catch (JSONException e) {
e.printStackTrace();
}
Here is the beginning of the PHP script.
<?php
include('dbconnect.php');
$tablename = 'users';
//username and password sent from android
$username=$_REQUEST['username'];
$password=$_REQUEST['password'];
.....
?>
Can you tell me what I'm doing wrong here please? I cannot seem to figure out.
Thank you