it is written in gradle:

android { compileSdkVersion 23 buildToolsVersion "25.0.0" useLibrary 'org.apache.http.legacy' dependencies { compile 'org.apache.httpcomponents:httpcore:4.4.5' 

in the manifest:

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.INTERNET" /> 

I can not get the data.

 HttpClient httpclient = new DefaultHttpClient(); HttpPost http = new HttpPost(myURL); List nameValuePairs = new ArrayList(2); nameValuePairs.add(new BasicNameValuePair("login", loginMap.get(0))); nameValuePairs.add(new BasicNameValuePair("password", loginMap.get(1))); try { http.setEntity(new UrlEncodedFormEntity(nameValuePairs)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try { String response = (String) httpclient.execute(http, new BasicResponseHandler()); System.out.println(response); } catch (IOException e) { e.printStackTrace(); } 

The error falls out:

 12-26 12:31:49.114 13673-13673/com.lovelinux.root.tmaxim E/AndroidRuntime: FATAL EXCEPTION: main Process: com.lovelinux.root.tmaxim, PID: 13673 android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147) at java.net.InetAddress.lookupHostByName(InetAddress.java:418) at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252) at java.net.InetAddress.getAllByName(InetAddress.java:215) at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:142) at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169) at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124) at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:658) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:632) at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:621) at com.lovelinux.root.tmaxim.MainActivity$1$override.onClick(MainActivity.java:94) at com.lovelinux.root.tmaxim.MainActivity$1$override.access$dispatch(MainActivity.java) at com.lovelinux.root.tmaxim.MainActivity$1.onClick(MainActivity.java:0) at android.view.View.performClick(View.java:4780) at android.view.View$PerformClick.run(View.java:19866) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

 class httpQUERY extends AsyncTask<Void, Void, Void> { String myURL = "https://адрес"; String params ; byte[] data = null; InputStream is = null; @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected Void doInBackground(Void... paramss) { if (!loginMap.isEmpty()) { params = "login=" + loginMap.get(0) + "&password=" + loginMap.get(1); }else{ System.out.println("Empty array"); return null; } HttpClient httpclient = new DefaultHttpClient(); HttpPost http = new HttpPost(myURL); List nameValuePairs = new ArrayList(2); nameValuePairs.add(new BasicNameValuePair("login", loginMap.get(0))); nameValuePairs.add(new BasicNameValuePair("password", loginMap.get(1))); try { http.setEntity(new UrlEncodedFormEntity(nameValuePairs)); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } try { String response = (String) httpclient.execute(http, new BasicResponseHandler()); System.out.println(response); } catch (IOException e) { e.printStackTrace(); } return null; } @Override protected void onPostExecute(Void result) {//Выполняется после doInBackground super.onPostExecute(result); System.out.println("I am here"); } } 

    1 answer 1

    NetworkOnMainThreadException indicates that you are trying to access a web resource from a UI stream, which you cannot do.

    All requests to the Internet need to be carried out in streams other than the UI stream. In the most primitive case, AsyncTask can be used, but, generally speaking, it is better to use libraries that out of the box can execute queries not in the UI stream (OkHttp, RoboSpice, etc.).

    UPD: If you get this exception and use AsyncTask , then check whether you are doInBackground(...) method directly, if you call it - replace the call line of this method with task.execute(...) , where task is an instance of AsyncTask .

    • This is where I run the class httpQUERY extends AsyncTask<Void, Void, Void>{ } - Ghost
    • @Ghost, Show the full code of the class in which the asynstask is called and indicate the line on which the application falls. - post_zeew
    • String response = (String) httpclient.execute (http, new BasicResponseHandler ()); - Ghost
    • The code is laid out above - Ghost
    • @Ghost, Покажите полный код класса, в котором вызывается асинстаск . And more: look UPD in the answer. - post_zeew