javaphpandroidopenshiftopenshift-php-cartidges

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.


Solution

  • 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