I develop an android code for transmit and received between android apps and PHP. The received part which is based on JSON, is properly working. I have tested by set variable manually in PHP code. However, when I have posted the variable from android to php, it cannot receive it. Anyone can tell me the problem ?
Asked
Active
Viewed 536 times
2 Answers
2
@Override
protected String doInBackground(Void... params) {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", <Your username here>));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(<Your URL to php file>);
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "UTF-8"));
HttpResponse response = httpclient.execute(httppost); // Execute Post to URL
String st = EntityUtils.toString(response.getEntity()); // This is the result from php web
Log.d(TK_Configuration.TAG, "In the try Loop" + st); // Still executing
finalResult = st; // You should register a variable for finalResult;
} catch (Exception e) {
Log.d(TK_Configuration.TAG, "Connection error : " + e.toString());
}
return "OK";
}
protected void onPostExecute(String result) {
super.onPostExecute(result);
// After that, you will have final result and process to do with it here
// Below is my simple code, please change it
if(finalResult.equals("1")){
Toast.makeText(context, context.getResources().getString(R.string.upload_bike_success), Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(context, context.getResources().getString(R.string.upload_bike_fail), Toast.LENGTH_SHORT).show();
}
// End
}
Please try this, and one more point, you should use Gson library to decode JSON quickly to Java Object after you got JSON string from server.
Note: Replace TK_Configuration.TAG << by your TAG.

thienkhoi tran
- 341
- 2
- 9
-
what is TK_Configuration tag ? – Mohammad Ali Nematollahi Feb 29 '16 at 04:42
-
Error:(243, 2) error: reached end of file while parsing – Mohammad Ali Nematollahi Feb 29 '16 at 05:14
-
Hi, I saw you have wrong request file http://www.oobac.com/app/insert-db.php << this file use to insert data to database? Right? If you want to get data from database, you use amir.php << Right? – thienkhoi tran Feb 29 '16 at 06:08
-
Do you have any suggestion? – Mohammad Ali Nematollahi Feb 29 '16 at 06:54
-
Which line make the error "Error:(243, 2) error: reached end of file while parsing"? I am working with JSON php and JAVA android now. We can discuss via my skype "trantkhoi" – thienkhoi tran Feb 29 '16 at 07:09
1
you have commented this line it means you are not passing values from Android
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
remove comment from this line.
One more thing, you are passing username
but from php you are trying to fetch value as $user = $_POST['name'];
, both name must be same.

Ravi
- 34,851
- 21
- 122
- 183
-
I modified $user = $_POST['username']; and uncomment httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); However, it does not work – Mohammad Ali Nematollahi Feb 29 '16 at 04:35
-
try to print `username` in php code and check whether you are getting it or not. – Ravi Feb 29 '16 at 04:37
-
should I echo $user in my php file? where I should see it in android or browser? – Mohammad Ali Nematollahi Feb 29 '16 at 04:44
-
you should get it in android side as a response. print `result` variable in log or toast. – Ravi Feb 29 '16 at 04:45
-
-
02-29 13:03:41.828 16241-16278/com.example.alan.mainactivity W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad9e0740, error=EGL_SUCCESS 02-29 13:03:42.505 16241-16278/com.example.alan.mainactivity E/Surface: getSlotFromBufferLocked: unknown buffer: 0xac1a3b20 02-29 13:03:42.510 16241-16241/com.example.alan.mainactivity I/Choreographer: Skipped 44 frames! The application may be doing too much work on its main thread. – Mohammad Ali Nematollahi Feb 29 '16 at 05:05