3

I am sending information to a server via WIFI and everything works great.Now i want to send information to a server with mobile data too, and i do not know why only works with WIFI, with mobile data trows an exception of failed to connect to server.

this is the part that fail with mobile data; with WIFI works perfectly:

int length=values.length();
    HttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams,9000);
    HttpConnectionParams.setSoTimeout(httpParams, 9000);

    HttpClient client = new DefaultHttpClient(httpParams);

    String url = saveData+"?Length="+length+"&Table="+temp;        
    HttpPost request = new HttpPost(url);
    request.setEntity(new ByteArrayEntity(values.toString().getBytes("UTF8")));        
    request.setHeader("json", values.toString());
    HttpResponse response = client.execute(request);
    HttpEntity entity = response.getEntity();
    // If the response does not enclose an entity, there is no need
    Log.d("test 7","test 7 last");
    if (entity != null) {
        InputStream instream = entity.getContent();
        String result =  RestClient.convertStreamToString(instream);
        Log.d("here",""+result);
        if(result.equals("success")&& ReadyOff==false){
            Ready=true;
        }else{
            Ready=false;
            ReadyOff=true;
        }
        Log.d("sent","valor de ready"+Ready);
    }

so i am doing something wrong? `

Tobiel
  • 1,543
  • 12
  • 19
  • change `9000` to `30000`.... its very probable that your current timeout of 9 seconds is way too short. – petey Jun 25 '13 at 19:07
  • 3
    Is your server routable from the public Internet? If it's something running on your development machine or otherwise behind a NAT/firewall, the answer would probably be that it is not, but rather only visible to other machines on your internal wifi. – Chris Stratton Jun 25 '13 at 19:07
  • Does the Browser work to open the same URL over mobile data? – Diego Torres Milano Jun 25 '13 at 19:15
  • @ChrisStratton, thanks for your answer you save my day :D, is exactly as you say,my server for now is not accessible from public internet. – Tobiel Jun 25 '13 at 20:15

1 Answers1

5

You cannot contact your server from the device's mobile network unless it is routable from the public Internet.

A server running on your development machine or otherwise behind a NAT/firewall would typically only be accessible from your local network / wifi.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117