1

I am newbie to native android development. I have created a DB in phpmyadmin using xampp server. Then i created a REST webservice using Yii. I tested the web service on ARC and it's working fine. Now i want to test it on my device. So i searched many articles and found a answer (link). But it doesn't helped me out. Below is my code

public class MainActivity extends AppCompatActivity {

String URLGET = "http://192.168.8.85:8000/app/web/users/";
String result = "";
TextView getData;
EditText userInput;
public static final String LOG_TAG = MainActivity.class.getSimpleName();

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


    getData = (TextView) findViewById(R.id.getData);
    userInput = (EditText) findViewById(R.id.EnterId);

    final Button button = (Button) findViewById(R.id.btn_Submit);


    button.setOnClickListener(new Button.OnClickListener() {


        @Override
        public void onClick(View v) {
            String reqURL = URLGET;

            new RestOperation().execute(reqURL);
            //callWebService(query);
        }
    });

}

public class RestOperation extends AsyncTask<String, Void, Void> {


    final HttpClient httpClient = new DefaultHttpClient();
    String content;
    String error;
    ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
    String data = "";

   // TextView getData = (TextView) findViewById(R.id.getData);
   // EditText userInput = (EditText) findViewById(R.id.EnterId);


    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progressDialog.setTitle("Please Wait...");
        progressDialog.show();

        data = "data=" + URLEncoder.encode(String.valueOf(userInput.getText()));
    }

    @Override
    protected Void doInBackground(String... params) {

        BufferedReader br = null;

        URL url = null;
        try {
            url = new URL(params[0]);


            URLConnection connection = url.openConnection();

            connection.setDoOutput(true);

            OutputStreamWriter outputStreamWriter = new OutputStreamWriter(connection.getOutputStream());

            outputStreamWriter.write(data);
            outputStreamWriter.flush();

            br = new BufferedReader(new InputStreamReader(connection.getInputStream()));

            StringBuilder sb = new StringBuilder();

            String line = null;

            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append(System.getProperty("line.separator"));


            }

            content = sb.toString();

        } catch (MalformedURLException e) {
            error = " Exception: " + e.getMessage();
            e.printStackTrace();
        } catch (IOException e) {
            error = " IOException: " + e.getMessage();
            e.printStackTrace();
        } finally {
            try {
                if(br != null)
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        progressDialog.dismiss();

        if (error != null) {
            getData.setText("Error" + error);
        } else {
            getData.setText(content);


        }
    }
}}

The above ip address is my wireless address. Firstly i was testing it on emulator using localhost but on searching i found that i should use 10.0.2.2. So i use it and it still doesn't works. I gives me the a warning in logcat as shown below

enter image description hereenter image description here

I must be missing some thing that i don't know. I am stuck to it from almost 2-3 days and really don't know what to do.

Any help would be highly appreciated

Community
  • 1
  • 1
Moeez
  • 494
  • 9
  • 55
  • 147
  • Your device and computer should be in the same network. Use IP Address of that network and turn off the firewall. – Mayur Gangurde Feb 08 '17 at 06:51
  • Ok let me do it and i'll tell you – Moeez Feb 08 '17 at 06:52
  • I did what you said i run the app and this error is shown `java.net.ConnectException: failed to connect to /192.168.8.85 (port 8000): connect failed: EHOSTUNREACH (No route to host)` – Moeez Feb 08 '17 at 06:56
  • Hit the URL in the browser check if it's working. – Mayur Gangurde Feb 08 '17 at 06:58
  • No it's not, it's only working with `localhost` – Moeez Feb 08 '17 at 07:00
  • I am using wireless internet so in `cmd` under `Wireless LAN adapter WiFi` the `IPv4 Address` is my `Network Ip` right ? – Moeez Feb 08 '17 at 07:01
  • What is the ip address of your android device, is it 192.168.8.x ? – wrkwrk Feb 08 '17 at 07:03
  • @wrkwrk yes it's `192.168.8.85` – Moeez Feb 08 '17 at 07:03
  • Make sure your web server is listening on 0.0.0.0:8000, and the phone is under the same network (using the same wifi). If the server is listening on 127.0.0.1:8000, it can only be accessed by localhost(127.0.0.1). – wrkwrk Feb 08 '17 at 07:09
  • My phone and my system are under same network. I have already tried to connect it with the `localhost` but i am getting same error again again though it's working on my web browser. – Moeez Feb 08 '17 at 07:20
  • I am using wireless internet so in cmd under Wireless LAN adapter WiFi the IPv4 Address is my Network Ip right ? **Yes. Use it.** – Mayur Gangurde Feb 08 '17 at 07:29
  • @mdg5435 yes i am using it but still unable to connect – Moeez Feb 08 '17 at 07:35

1 Answers1

0

Turn on Wifi Hotspot of your Android phone/router and connect your Laptop to your phone.

Start your server at localhost (I am using wamp server)

Now open command prompt and enter ipconfig command you will get following things

Wireless LAN adapter Wireless Network Connection:
  Connection-specific DNS Suffix  . :
  Link-local IPv6 Address . . . . . : fe80::80bc:e378:19ab:e448%11
  IPv4 Address. . . . . . . . . . . : **192.168.43.76**
  Subnet Mask . . . . . . . . . . . : 255.255.255.0
  Default Gateway . . . . . . . . . : 192.168.43.1

Copy this 192.168.43.76 in your mobile browser.

Note : Please set your network as "Home Network". Setting Home Network means allowing your PC to share stuff with other devices on the same network. If your using Windows 10 go to WiFi > Network properties of current network > Make this PC Discoverable.

Original Answer : How can I access my localhost from my Android device?

Community
  • 1
  • 1
Mayur Gangurde
  • 1,552
  • 12
  • 22
  • What do you mean `Turn on Wifi Hotspot of your Android phone/router` ? Do i have to turn on the `Wifi` or `Wifi Hotspot` these both are different i guess – Moeez Feb 08 '17 at 10:11
  • This answer is for reference. You can use `WiFi Hotspot of your android device` or `WiFi`. Just, make sure your device and computer are in the same network, XAMPP/WAPP is running, Firewall is turned off. – Mayur Gangurde Feb 08 '17 at 10:17
  • Ok i can see the result in web browser, but i want to access my application as i have posted screen shots. – Moeez Feb 08 '17 at 10:19
  • Check now in the device also. – Mayur Gangurde Feb 08 '17 at 10:21
  • `java.io.FileNotFoundException: http://192.168.8.65:8000/app/web/users/` :( – Moeez Feb 08 '17 at 10:36