Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't connect through WiFi, but possible through Mobile Data

I've a php file named test.php stored in my Openshift server (http://phpgear-shifz.rhcloud.com/v1/test.php) with the below code.

<?php
echo "Hello";

Task

I am trying to download the text from an Android application.

Problem

I am getting a java.net.UnknownHostException: Unable to resolve host "phpgear-shifz.rhcloud.com": No address associated with hostname while connecting through a WiFi network, but everything is fine with Mobile Data.

Android Activity Code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        final TextView tvTest  = (TextView) findViewById(R.id.tvTest);

        new AsyncTask<Void, Void, String>() {
            @Override
            protected String doInBackground(Void... params) {

                try {
                    final URL url = new URL("http://phpgear-shifz.rhcloud.com/v1/test.php");
                    final BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
                    final StringBuilder sb = new StringBuilder();
                    String line;
                    while((line = br.readLine())!=null){
                        sb.append(line).append("\n");
                    }

                    br.close();
                    return sb.toString();

                } catch (IOException e) {
                    e.printStackTrace();
                    return "Error: "  + e.getMessage();
                }
            }

            @Override
            protected void onPostExecute(String result) {
                tvTest.setText(result);
            }
        }.execute();

    }

RESPONSES

on WiFi

onWiFi

on Mobile Data

onMobileData

Question

1) Why I can't connect through the WiFi network where Mobile Data is perfectly fine ?

2) How to solve this problem ?

NOTE: Sometime it's getting connected, sometime won't.

like image 434
theapache64 Avatar asked Sep 27 '15 07:09

theapache64


People also ask

Why some websites are not opening in Wi-Fi but work on mobile data?

Try resetting your router. Why are some sites not opening on my computer with my Wifi but do with mobile hotspot? Because your Wifi and your mobile hotspot route through different ISPs and it sounds like either the router/modem or the ISP for your WiFi may be blocking those sites while your mobile hotspot's ISP isn't.

Why wont my phone connect to Wi-Fi but everything else will?

If your Android phone won't connect to Wi-Fi, you should first make sure that your phone isn't on Airplane Mode, and that Wi-Fi is enabled on your phone. If your Android phone claims it's connected to Wi-Fi but nothing will load, you can try forgetting the Wi-Fi network and then connecting to it again.

Why is my data working but not my internet?

Restart your device. It might sound simple, but sometimes that's all it takes to fix a bad connection. If restarting doesn't work, switch between Wi-Fi and mobile data: Open your Settings app and tap Network & internet or Connections. Depending on your device, these options may be different.

Why is my phone using mobile data instead of Wi-Fi?

Some third-party apps are designed to consume mobile data even with Wi-Fi connected. Some third-party apps, such as online banking apps, may still consume mobile data even if they are connected to a Wi-Fi network. This issue occurs on all Android phones and cannot be resolved by changing the settings on your phone.


2 Answers

Your DNS doesn't know the IP address of the requested site.

You are experiencing problems, because the DNS of your Wifi connection cannot convert a hostname to an IP address. And your data carrier is using different DNS which has associated IP address to hostname.

Try to change your DNS server address on your Wifi router or use direct IP address of the website if available.

Here are some google DNS server addresses

  • 8.8.8.8
  • 8.8.4.4
like image 176
Zeerou Avatar answered Sep 30 '22 06:09

Zeerou


You may have an IPv4 vs IPv6 problem. Many mobile data plans use IPv6, while most WiFi installations currently use IPv4, so you may be switching more than just the network layer; you may actually be switching layer 3 protocols.

The DNS entry for phpgear-shifz.rhcloud.com points to an IPv4 address (only), so it should work on WiFi. But maybe your mobile device uses an IPv6 DNS server and can't resolve the name via IPv4?

Another possibility: your mobile device may have a more general problem in the IPv4 stack. Your mobile data may be using one of the 6-to-4 transition technologies and thus bypass your local IPv4 problem.

I noticed another problem with the DNS name phpgear-shifz.rhcloud.com although I doubt it is related.

That DNS entry is actually a CNAME entry that points to another CNAME entry, which in turn points to an A record on Amazon. Double indirections of CNAMEs are a violation of the DNS RFCs, although most resolver should handle it anyway. Also, if this was the problem, it should affect both WiFi and mobile data equally.

like image 44
Kevin Keane Avatar answered Sep 30 '22 04:09

Kevin Keane