0

I'm currently using localhost to develop mobile apps in android studio while connecting via a USB cable. I set up the localhost environment using this method https://stackoverflow.com/a/26539795/4681900 and got it to work on an IP address. The problem is I keep getting Unknown Host Exceptions when trying to make a HTTP call. Sometimes the connection works fine but most times it fails.

Here is a sample code

AsyncHttpClient client = new AsyncHttpClient();
client.get("https://192.xxx.xxx.xxx", new AsyncHttpResponseHandler() {

    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] response) {
        Toast.makeText(getContext(),"Awesome",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, 
       Throwable e) {
       if(errorResponse==null){
           Toast.makeText(getContext(),"No response",Toast.LENGHT_SHORT).show();
           return;
       }
       Toast.makeText(getContext(),"Error connecting",Toast.LENGHT_SHORT).show();
    }

    @Override
    public void onRetry(int retryNo) {
    }
});

Here is part of the exception's stack trace

01-26 08:29:34.103 3789-4880/com.foodplace W/System.err: java.net.UnknownHostException: Unable to resolve host "my-PC": No address associated with hostname 

How can this be fixed? I am using Async Http Client (http://loopj.com/android-async-http/) and WAMP as my localhost server.

Community
  • 1
  • 1

0 Answers0