W
W
Warog2017-04-07 17:57:15
Android
Warog, 2017-04-07 17:57:15

Can't get JSON when run on phone. The code works on PC. What's the matter?

My goal is to get JSON for further processing. When launched on a PC, everything works, but when I try to run the application on the phone, it stops working. When checking, I saw that when the program is executed, it reaches the line c.connect();and immediately jumps to , skipping all the lines of code between them. I don't understand what's the matter. Here is the code:} finally {

BufferedReader reader = null;
                try {
                    URL url = new URL("Любой адрес");
                    HttpsURLConnection c = (HttpsURLConnection) url.openConnection();
                    c.connect();
                    reader = new BufferedReader(new InputStreamReader(c.getInputStream()));
                    StringBuilder buf = new StringBuilder();
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        buf.append(line + "\n");
                    }
                       return(buf.toString());

                    textView.setText(buf);
                } finally {
                    if (reader != null) {
                        reader.close();
                    }
                }

            }catch(IOException e){
                e.printStackTrace();
            }

Thank you in advance.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
G
GavriKos, 2017-04-07
@Warog

99% of what you run in the UI thread.
There should be an exception in the log that this cannot be done.
All operations with the network are done only in a parallel stream. There are manuals in Google, it's full.

D
Denis Zagaevsky, 2017-04-07
@zagayevskiy

+1 for UI thread
also need to specify permission INTERNET in manifest

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question