0

I have an android app which is making api requests to my server running Spring MVC. The RestController works fine when I make a request from the browser but it responds with 404 when I am making requests from android. Not sure why

Here is code snippet from Android app making requests

public class AsyncFetch extends AsyncTask<Pair<String, String>, String, String> {
public ProgressDialog pdLoading;
private HttpURLConnection conn;
private String urlStr;
private String requestMethod = "GET";
public AsyncFetch(String endpoint, Context ctx)
{
    pdLoading  = new ProgressDialog(ctx);
    Properties reader = PropertiesReader.getInstance().getProperties(ctx, "app.properties");
    String host = reader.getProperty("host", "10.0.2.2");
    String port = reader.getProperty("port", "8080");
    String protocol = reader.getProperty("protocol", "http");
    String context = reader.getProperty("context", "");
    this.urlStr = protocol+"://"+host+":"+port+context+endpoint;
}

@Override
protected void onPreExecute() {
    super.onPreExecute();

    //this method will be running on UI thread
    pdLoading.setMessage("\tLoading...");
    pdLoading.setCancelable(false);
    pdLoading.show();

}

@Override
protected String doInBackground(Pair<String, String>... params) {
    URL url;
    try {

        url = new URL(urlStr);


    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return e.toString();
    }
    try {

        conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(READ_TIMEOUT);
        conn.setConnectTimeout(CONNECTION_TIMEOUT);
        conn.setRequestMethod(requestMethod);
        conn.setDoOutput(true);

    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
        return e1.toString();
    }

    try {



        int response_code = conn.getResponseCode();

        // Check if successful connection made`enter code here`
        if (response_code == HttpURLConnection.HTTP_OK) {

            // Read data sent from server
            InputStream input = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(input));
            StringBuilder result = new StringBuilder();
            String line;

            while ((line = reader.readLine()) != null) {
                result.append(line);
            }

            // Pass data to onPostExecute method
            return (result.toString());

        } else {

            return ("unsuccessful");
        }

    } catch (IOException e) {
        e.printStackTrace();
        return e.toString();
    } finally {
        conn.disconnect();
    }

}

Spring MVC Controller

@RestController
public class ApiController {
    @RequestMapping(value = "homefeed",  method=RequestMethod.GET)
    public String homefeed(@RequestParam(value="userId", required = false) Integer id, @RequestParam(value="search", required = false) String search, @RequestParam(value="page", required = false, defaultValue = "0") Integer page) { ... }
}
  1. localhost:8080/api/homefeed -- works
  2. 127.0.0.1:8080/api/homefeed -- works
  3. My Public IP:8080/api/homefeed -- does not works
  4. 10.0.2.2:8080/api/homefeed -- android emulator to localhost -- does not work
  5. 10.0.2.2:8080/Some resource other than the api endpoint -- works

Any help is highly appreciable, have wasted quiet some time in debugging.

user2745862
  • 103
  • 3
  • 8
  • where is your server is it hosted in cloud.try adding consumes application/json in your controller . – Robert Ellis Nov 23 '16 at 09:07
  • Hi Ramana, nope the server is running locally .. I am afraid consumes application/json is not fix, if that was the case anyways It wouldnt had worked from the browser. I am able to make browser requests to the api. The problem has to be something with spring mvc not working for host address besides localhost/127.0.0.1 since when i try with :8080/api/homefeed it doesnt work. – user2745862 Nov 23 '16 at 09:20
  • the ip you posted there does not seem to be public address .if is it public adress and your server is up and running i will be able to access it .please up your instance if it is not i will try and access it .i think it is your machine ipadress try what is my ip in the internet that is your public ip – Robert Ellis Nov 23 '16 at 09:33
  • Are you referring to this IP 10.0.2.2?? That is the IP one has to use from Android Emulator to access the localhost, since within the emulator environment localhost would mean emulator IP Address and not the machine IP address where the emulator is running. You are correct my public IP is 122.176.142.XXX, could you please tell me how to make my tomcat listen to my public IP instead of localhost ? – user2745862 Nov 23 '16 at 10:33

0 Answers0