0

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;
        }
    }

}
Axel
  • 3,331
  • 11
  • 35
  • 58
  • Did you set the localhost IP address to `192.168.1.100` yourself? Is this the ip address you see when you run `ipconfig` in cmd prompt? (assuming you are using windows). Also, [this link](https://stackoverflow.com/questions/6760585/accessing-localhostport-from-android-emulator) might help. – Danoram Nov 09 '17 at 01:27
  • It's a bit unclear, you're asking about localhost, but use network ip. But it seems you have to modify a **HOST** header to your request and set it to `codemobiles.com`, please refer to https://stackoverflow.com/questions/43156023/what-is-http-host-header. – Northern Poet Nov 09 '17 at 02:09
  • i am not getting any response when i use localhost. this is my ip shown in ipconfig and it is working in browser. and only when i give this url it is taking some delay for output. still it is showing blank toast. – arjun prince Nov 09 '17 at 08:26
  • Is there any other simple code for the same purpose? My requirement is i have to connect to a url and pass some values. and receive a reply from server and display it. which is the best method? – arjun prince Nov 09 '17 at 08:34
  • `this is my ip shown in ipconfig and it is working in browser.`. Your ip? Or the ip of your pc? Or the ip of your Android device? Is there an Android device in play? You did tell nothing of your setup! Working in browser? A browser on your Android device? A browser on a pc? A browser on another pc? All pretty unclear. – greenapps Nov 09 '17 at 08:44
  • `catch (Exception e) { return null; }`. You know nothing if you do that. Not even printing the stack trace. Add printing the stacktrace and change to `return "IOException: " + e.getMessage();`. – greenapps Nov 09 '17 at 08:49
  • if i paste the same url in my pc browser and change php code to echo instead of return. it is echoing successfully. i received this ip from ipconfig (wan)command from my pc. can u suggest any other url or connection method please ? – arjun prince Nov 09 '17 at 08:58
  • Thanks a lot. now i am getting error message that unable to connect to that ip. i changed it to localhost, even now it is showing unable to connect to 127.0.01:80. which format i have to use to connect to local host – arjun prince Nov 09 '17 at 09:17

0 Answers0