Question

I'm developing an android app, my first one and the first time I work with network stuff.

In java my code works properly, and in android I have included the uses permission network in the manifest but it still not work I do not know why.

My code in android is

ArrayList<Pair<String,String>> parameters = new ArrayList<Pair<String, String>>();
Pair<String,String> value = new Pair<String, String>("name",cadena);
parameters.add(value);
ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);


URL url = new URL("http://192.168.1.43:8084/DnDServer/addGame"+ Utilidades.getParameter(parameters));
mostrarError(Utilidades.getParameter(parameters));
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.connect();
mostrarError(con.getResponseCode() + "");

getParameters() returns a string like ?param1=value1&param2=value2....

and in the android manifest i have included

<uses-permission android:name="android.permission.INTERNET">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE">
Was it helpful?

Solution

You're performing this operation in the main UI, which is forbidden in Android because it would block the thread until it was processed, resulting probably in an Application Not Responding.

Move the network operation into an AsyncTask. Put the code in the doInBackground() method, as when it will be run, it will automatically create another thread and perform the network operation there.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top