I'm trying to connect my android application to a local host url thanks to wamp server but it doesn't work. My goal here, is to fetch json data and parse these data. For my test, i'm using a device not the emulator and i use permission in AndroidManifest.xml :
<uses-permission android:name="android.permission.INTERNET" />
My url looks like this :
String url = "http://10.0.2.2:8080/tests/PhpProject1/connectionBDD.php";
i tried :
http://localhost/ http://10.0.2.2:8080/ http://10.0.2.2/
But it never worked so far :
java.net.ConnectException: failed to connect to localhost/127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused) failed to connect to /10.0.2.2 (port 8080): connect failed: ETIMEDOUT (Connection timed out) java.net.ConnectException: failed to connect to /10.0.2.2 (port 80): connect failed: ETIMEDOUT (Connection timed out)
Then i tried with a json url test found on the internet : http://headers.jsontest.com/
It worked really good and i got json data at this address. So i guess my code is good and the issue here is my localhost url, i don't know what should be its exact form.. I read many threads about it but i didn't find a solution.
Here my code :
Main activity :
public class MainActivity extends Activity { private String url = "http://10.0.2.2:8080/tests/PhpProject1/connectionBDD.php"; private ListView lv = null; private Button bGetData; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final JsonDownloaderTask task = new JsonDownloaderTask(this); lv = (ListView) findViewById(R.id.list); bGetData = (Button)findViewById(R.id.getdata); bGetData.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { task.execute(url); } }); } public void jsonTaskComplete(JSONArray data){ //todo } }
AsyncTask :
public class JsonDownloaderTask extends AsyncTask<String, String, JSONArray> { MainActivity ma; public JsonDownloaderTask(MainActivity main){ ma = main; } @Override protected JSONArray doInBackground(String... url) { JSONParser jParser = new JSONParser(); // Getting JSON from URL JSONArray jsonArray = null; try { jsonArray = jParser.getJSONFromUrl(url[0]); } catch (IOException e) { e.printStackTrace(); } return jsonArray; } protected void onPostExecute(JSONArray data){ ma.jsonTaskComplete(data); } }
JSONParser :
public class JSONParser { String data = ""; JSONArray jsonArray = null; InputStream is = null; public JSONParser(){} // Method to download json data from url public JSONArray getJSONFromUrl(String strUrl) throws IOException{ try{ URL url = new URL(strUrl); // Creating an http connection to communicate with url HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // Connecting to url urlConnection.connect(); // Reading data from url is = urlConnection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is)); StringBuffer sb = new StringBuffer(); String line = ""; while( ( line = br.readLine()) != null){ sb.append(line); } is.close(); data = sb.toString(); //br.close(); jsonArray = new JSONArray(data); }catch(Exception e){ Log.d("Exception while downloading url", e.toString()); }finally{ is.close(); } return jsonArray; } }
You can access your host machine with the IP address "10.0. 2.2". This has been designed in this way by the Android team. So your webserver can perfectly run at localhost and from your Android app you can access it via "http://10.0.2.2:8080".
To access the server from itself, use http://localhost/ or http://127.0.0.1/ . To access the server from a separate computer on the same network, use http://192.168.X.X where X.X is your server's local IP address. You can find the sever's local IP address (assuming it's Linux) by running hostname -I . 127.0.
Applications running in the Android emulator can connect to local HTTP web services via the 10.0. 2.2 address, which is an alias to your host loopback interface ( 127.0. 0.1 on your development machine).
On your mobile device's browser (any will work), navigate to http://<Local IP Address>:<port number> . For example, if I was serving on localhost:8080 and my local IP address is 123.45. 67.890, on my mobile device's browser I would navigate to http://123.45.67.890:8080 . The http:// is important, don't leave it off.
IP-address 10.0.2.2 is used to fetch data from the emulator. Localhost will always point to the emulator/android device running the application. To let your device fetch data from your pc, it should be in the same network (connected by WiFi to your router) and you should use the local IP-address of your pc (normally a 192.168.1.x-number).
If you try to connect to "localhost", it will resolve to the Android device, not to your own localhost (unless you are running within the emulator). What I recommend for development is to add an overflow menu in the action bar that has an entry named "Settings" that provides a Settings activity for specifying application settings, and to have a "Developer options" entry in "Settings" that lets you specify a custom server address to use. During development, you can use this option to enter a custom server address for your app. (You will need a real server address that is actually reachable over the Internet rather than using localhost for this).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With