I have an android login page which will connect localhost url, pass json object and accept a json object from server and toast it. But now I have only done receiving part on url request. I came to know that we have to use IP instead of localhost in android from stackoverflow itself. But I didn't get any reply from the server. It is showing blank. But if I use external url I get it correctly. I have commented that url just above it. And if I decode json and echo it in the browser it works fine. But I am not getting it in my app.
This is my Android Code:
public class FeedTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
try {
OkHttpClient client = new OkHttpClient();
RequestBody postData = new FormBody.Builder()
.add("type","json")
.build();
Request request = new Request.Builder()
//.url("http://codemobiles.com/adhoc/feed/youtube_feed.php")
.url("http://192.168.1.100/patronheedV1.1.1/App/Request/abc")
.post(postData)
.build();
Response response = client.newCall(request).execute();
String result = response.body().string();
return result;
} catch (Exception e) {
return null;
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_LONG).show();
}
}
And this is my PHP code:
<?php
class App {
public function Request() {
if ( isset($_POST) ){
$obj = (object) ['aString' => 'Working'];
return json_encode($obj);
} else {
$obj = (object) ['aString' => 'Notworking'];
return json_encode($obj);
}
}
public function fetchRequest() {
if ( isset($_POST['Request']) && isset($_POST['Token']) ){
$user = $_POST['User'];
return $user;
} else {
return NULL;
}
}
}