crashes on InputStream is = conn.getInputStream (); how to solve a problem?

try { conn = (HttpURLConnection) new URL(lnk) .openConnection(); conn.setReadTimeout(10000); conn.setConnectTimeout(15000); conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", "Mozilla/5.0"); conn.setDoInput(true); conn.connect(); } catch (Exception e) { e.printStackTrace(); } // получаем ответ try { InputStream is = conn.getInputStream(); BufferedReader br = new BufferedReader( new InputStreamReader(is, "UTF-8")); StringBuilder sb = new StringBuilder(); String bfr_st; while ((bfr_st = br.readLine()) != null) { sb.append(bfr_st); } ansver = sb.toString(); ansver = ansver.substring(0, ansver.indexOf("]") + 1); is.close(); // закроем поток br.close(); // закроем буфер } 

Closed due to the fact that the essence of the question is incomprehensible by the participants Yuriy SPb , aleksandr barakin , zRrr , Grundy , D-side 14 Apr '16 at 16:12 .

Try to write more detailed questions. To get an answer, explain what exactly you see the problem, how to reproduce it, what you want to get as a result, etc. Give an example that clearly demonstrates the problem. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • What does crash mean? Get Exception? If it is, then it is necessary to bring it. - Vartlok
  • No, it does not crash, it opens a white background and freezes, requests to wait for the application to respond or close. and in logcat debug output this string InputStream is = conn.getInputStream (); - Shusharin Gleb
  • Try conn.setReadTimeout(); and conn.setConnectTimeout(); put on 4000 instead of your values. - Yuriy SPb

1 answer 1

You did not attach an error log, so you can only guess that you are trying to access the network from the main thread and you could have NetworkOnMainThreadException

This is solved by accessing the network from a non-mainstream through a Loader, AsynkTask or IntentService or by directly creating a new one.