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

3.8k Views Asked by At

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.

2

There are 2 best solutions below

5
Zeerou On BEST ANSWER

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
0
Kevin Keane On

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.