1

Connect an Android Device To a Web Service on Local Host

Following my previous thread , im now able to connect my Android Device to my local host using wamp

But still i cannot connect to my symfony server and get my API datas

I sarted symfony's internal server : "Server running on http://127.0.0.1:8000"

I used Internet permission on my AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
<uses-permission android:name="android.permission.DOWNLOAD_WITHOUT_NOTIFICATION" />

My MainActivity.java code

package com.example.cbmedandroid;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity implements OnClickListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        findViewById(R.id.my_button).setOnClickListener(this);
    }

    @Override
    public void onClick(View arg0) {
        Button b = (Button)findViewById(R.id.my_button);
        b.setClickable(false);
        new LongRunningGetIO().execute();
    }

    private class LongRunningGetIO extends AsyncTask <Void, Void, String> {
        protected String getASCIIContentFromEntity(HttpEntity entity) throws    IllegalStateException, IOException {
            InputStream in = entity.getContent();
            StringBuffer out = new StringBuffer();
            int n = 1;
            while (n>0) {
                byte[] b = new byte[4096];
                n =  in.read(b);
                if (n>0) out.append(new String(b, 0, n));
            }
            return out.toString();
        }

        @Override
        protected String doInBackground(Void... params) {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpGet httpGet = new HttpGet("http://192.168.43.13:8000/api/horaire.json");
            String text = null;
            try {
                HttpResponse response = httpClient.execute(httpGet, localContext);
                HttpEntity entity = response.getEntity();
                text = getASCIIContentFromEntity(entity);
            } catch (Exception e) {
                return e.getLocalizedMessage();
            }
            return text;
        }

        protected void onPostExecute(String results) {
            if (results!=null) {
                EditText et = (EditText)findViewById(R.id.my_edit);
                et.setText(results);
            }
            Button b = (Button)findViewById(R.id.my_button);
            b.setClickable(true);
        }
    }
}

When i launch the application and click to the button . It load during 40 sec and i get this "Connection to http://192.168.43.13:8000 refused"

192.168.43.13 is my pc adress

What should i do to fix this . thanks.

Community
  • 1
  • 1
Kane Samba
  • 269
  • 1
  • 5
  • 18
  • Do you have another PC to test if your connection to "http://192.168.43.13:8000" works at all? – Alvin Bunk Jun 01 '16 at 23:06
  • No Unfortunately , But it should work Normally i guess , since Im able to connect to my localhost on My Android web navigator . – Kane Samba Jun 02 '16 at 00:38

2 Answers2

2

FINALLY! i have found the solution to my problem

when running the built-in php server . We need to specify this command

php bin/console server:run 0.0.0.0:8000

(8000 is my port , you can put yours)

so that Any device or host (ip) could access

if you put 127.0.0.1 only your localhost will be allowed

That's why i couldn't get the Api even i was connected to my localhost via wifi hotspot

It's ok now

Kane Samba
  • 269
  • 1
  • 5
  • 18
  • 1
    Indeed, if you want something to be externally accessible, you have to listen on INADDR_ANY (all 0's) or the specific external interface, rather than the loopback interface. – Chris Stratton Jun 02 '16 at 18:31
1

Are you able to install this cURL App for Android?

Then use on your Android (is this a real phone or an emulator) open a cURL window and then enter:

cURL http://192.168.43.13:8000/

I tried this same kind of setup with a real Android phone and the above indicated cURL app and put in my Symfony web URL (on another PC), and the Android shows the correct html response back that I'm expecting.

At least this will help you verify functionality first.

Edit below this line:

Here is the code you might use since HttpClient was deprecated:

URL url = new URL("http://192.168.43.13:8000/api/horaire.json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//conn.setRequestMethod("GET");

// read the response
System.out.println("Response Code: " + conn.getResponseCode());
InputStream in = new BufferedInputStream(conn.getInputStream());
String response = org.apache.commons.io.IOUtils.toString(in, "UTF-8");
System.out.println(response);

Not sure if you need to use the "setRequestMethod". But try out this change and see if that works for you.

Alvin Bunk
  • 7,621
  • 3
  • 29
  • 45
  • Hi , i downloaded cURL but infortunalety i have an Error while installing it on my phone. However some people says that the problem is with the Http request connection who is deprecated in Android 5.1.1 and they suggest to use HttpUrlConnection with java.net.HttpUrl Connection . Indeed my request is deprecated . Could you help me to edit my code with this new http request ? Thanks – Kane Samba Jun 02 '16 at 12:54
  • 1
    Is that in the `doInBackground` function? – Alvin Bunk Jun 02 '16 at 14:38
  • 1
    I edited my answer to include code for the `doInBackground` code which uses "HttpClient" that is deprecated. – Alvin Bunk Jun 02 '16 at 15:07
  • 1
    Kane, if you think any of my comments are helpful, you should "upvote" them. Just letting you know, since you may be new to Stackoverflow. Thank you! – Alvin Bunk Jun 02 '16 at 18:25