12

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.

theapache64
  • 10,926
  • 9
  • 65
  • 108
  • 1
    Have you made sure that the wifi router is connected to internet? i.e Were you able to access the url http://phpgear-shifz.rhcloud.com/v1/test.php from a device/computer connected to the same wifi ? – gipsy Sep 29 '15 at 12:35
  • Yeah am pretty sure about the internet connection and the url is accessible from the device. – theapache64 Sep 29 '15 at 13:38
  • I think you have remaining Android Manifest permission for _ACCESS_NETWORK_STATE_ `` – pRaNaY Sep 29 '15 at 13:56
  • I don't think so, anywayz here's the manifest http://pastebin.com/reqDaPdb – theapache64 Sep 29 '15 at 13:57
  • Why should i have the `Network state permission` ? I think we just need the `Internet` permission to access the internet and the `Network state permission` needed only when we want to check the `STATE` of the internet. isn't ? – theapache64 Sep 29 '15 at 13:59
  • @ShifarShifz Try to open the url in the mobile browser once. If it didn't open there it might be your wifi have some issue(may be the ip is blocked etc) or you may try some different wifi connection for the testing. – avinash Sep 29 '15 at 16:19
  • Have you tried other phones yet? I have tested your code in my phone with my Wifi, it works! – BNK Sep 30 '15 at 08:02
  • You can try uninstalling any app relating to the network in your phone, such as firewall, net monitor, or even reset your phone if needed... then retry. I think your phone has problem, not the wifi networks (because you confirmed having tested with many wifi) – BNK Sep 30 '15 at 08:14
  • One more thing, make sure your tested wifi networks can access Internet – BNK Sep 30 '15 at 08:31

2 Answers2

5

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
Alex Bravo
  • 1,601
  • 2
  • 24
  • 40
Zeerou
  • 182
  • 11
  • I can't connect from any `WiFi` connection . I think people using my `App` won't be changing their `WiFi` configuration for a specific application. What can i do now ? – theapache64 Sep 29 '15 at 14:44
  • I am on wifi and I can see hello message. – Lucas03 Oct 03 '15 at 11:12
  • @Shifar Please check your router DNS settings and it should work. I can see it using the wifi – Ritesh Oct 04 '15 at 03:08
  • @ShifarShifz [screenshot](http://i.imgur.com/EBAD7vz.png?1), your code is working on my phone over WiFi. – derfect Oct 06 '15 at 09:32
  • The DNS will update automatically but can take up to 48 hours in special cases, usually within `4 hours`. You could change the DNS to the DNS of Google (`8.8.8.8` or `8.8.4.4`). This only works if the problem is indeed the DNS. Which I think is **NOT** the case. – Matthijs van Hest Oct 06 '15 at 11:14
4

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.

Kevin Keane
  • 1,506
  • 12
  • 24