Akz Li
Akz Li

Reputation: 97

Can not connect to working webservice using android device

I used android device to connect via wifi to localhost of my computer.This is the string i m passing to retrieve values from web service.

I get a error message in logcat OSNEtworkSystem_Connect fail:Timeout. Can any one sugest a solution please.

String tabledata = getServerData[a link]("http://10.0.2.2:52764/Service1.asmx/getTrainTimeTable?      FromSt='"+frm+"'"+" ToSt= '" +t+ "'"+" FromTime= '" +t01+ "' "+"ToTime='"+t02+ "'"+" ArriveTime= '" +at+ "'"+" DepartTime= '" +dt+ "'"+" ReachingTime= '" +rt+ "'"+" TrainId= '"+tid+"'" )[a link];


private String getServerData(String url) {// requet and response happens here
    String data = null;

    try {
        // Send GET request to <service>/GetPlates
        HttpGet request = new HttpGet(url);
        DefaultHttpClient httpClient = new DefaultHttpClient();
        request.setHeader("Accept", "application/json");
        request.setHeader("Content-type", "application/json");
        
        HttpResponse response = httpClient.execute(request);
        HttpEntity responseEntity = response.getEntity();

        // Read response data into buffer
        char[] buffer = new char[(int) responseEntity.getContentLength()];
        InputStream stream = responseEntity.getContent();
        InputStreamReader reader = new InputStreamReader(stream);
        reader.read(buffer);
        System.out.println(new String(buffer));

        stream.close();

        JSONObject o = new JSONObject(new String(buffer));
        data = (String) o.get("d");
        System.out.println(data);

    } catch (Exception e) {
        e.printStackTrace();

    }

    return data;
}

after changing ip in to pc ip connection timeout eror is gone. now im gettin this error. log cat showing error

Upvotes: 0

Views: 163

Answers (2)

Ismail Sahin
Ismail Sahin

Reputation: 2710

If you really mean connect localhost via wi-fi

  • instal a virtual router to your computer. Then via wi-fi connect to your computer with your mobile device.

here is a link for virtual router

  • when you install it, connect with your mobile device to that virtual wi-fi provider. probably the ip that you should connect will be
  • http://192.168.1.1:52764/Service1.asmx/getTrainTimeTable? something(to learn it open cmd-forwindowns enter ipconfig to learn the router device's ip.

Be careful if you have internet connection

  • there will be two major ip addresses one is for your computer that as a client to connect to the internet,
  • the other one is for the virtual router as a servise host which you need to connect connect

Upvotes: 1

Coderji
Coderji

Reputation: 7765

Your problem is that you are using 10:0:2:2 to connect to devices. you need to provide PC's IP address when trying the application on real device.

follow the steps:

1- go to CMD and type ipconfig.

2- Search for IPv4 and copy the address.

3- use it in your code.. this is an example of how it should look like:

http://192.168.0.106:52764/Service1.asmx/getTrainTimeTable?.... // you must have similar IP to this.

4- Turn off the firewall and any anti-virus program

hope this will make your application work in a real device. please give me a feedback of what will happen

Upvotes: 1

Related Questions