You need to get the result of the GET query in Android and return the value from the method. Resolved! Thank you all for participating!

 public void sayHello(View view)throws IOException { Thread httpThread = new Thread(new Runnable() { public void run() { String mybla = sendGet(); } }); httpThread.start(); } private String sendGet(){ try{ String mystr = "http://www.pravda.com.ua"; URL obj = new URL(mystr); HttpURLConnection con = (HttpURLConnection) obj.openConnection(); con.setRequestMethod("GET"); con.setRequestProperty("User-Agent", "Mozilla/5.0"); con.setRequestProperty("Accept-Charset", "UTF-8"); InputStream response = con.getInputStream(); Scanner s = new Scanner(response).useDelimiter("\\A"); String result = s.hasNext() ? s.next() : ""; return result; } catch (Exception e) { return e.toString(); } } 
  • Use the library for this. For example OkHttp - YuriySPb
  • one
    A standard can not be used? - Mikhail121
  • Yes, it seems, it is possible, but why, if there are any specials who make it better? .. - YuriySPb

2 answers 2

Do everything right. In order to read a stream and get a String from it, you can use, for example, the Apache commons IOUtils ( off site ). With her help:

 String result = IOUtils.toString(inputStream); 

If you prefer a method without third-party libraries (which I welcome, because it allows you to learn something new about the language in which you are new), then do something like this:

 Scanner s = new Scanner(inputStream).useDelimiter("\\A"); String result = s.hasNext() ? s.next() : ""; 

UPD :

Wrap in

 new Thread(new Runnable() { public void run() { //вызовите ваш код для сетевого запроса } }).start(); 

When your query returns the result inside your Thread , in order to change the interface element you will need to do this from the UI thread. To do this, use Context.runOnUiThread(); .

  • Work without third-party libraries is something that is needed, but when executing the code, it gives an error android.os.NetworkOnMainThreadException - Mikhail121
  • As someone already said, android does not allow to perform network requests in the main thread. Use AsyncTask , or Thread . - Daniel Shatz
  • Master =) teach)) You can tell in more detail how to use @? - Mikhail121
  • @Mikhail edited the question - Daniel Shatz
  • ok, and then how to display the result of the query on TextView? - Mikhail121

Code using OkHttp

 OkHttpClient client = new OkHttpClient.Builder().build(); String url = ...; Request.Builder request = new Request.Builder(); request.url(url); request.get(); Response response = client.newCall(request.build()).execute(); String response = response.body().string(); System.out.println(response ); 

We must remember that on android network requests can be done with API14 only outside the UI stream. For example through AsynkTask, Thread etc

  • one
    If the request does not require interaction with the UI stream, OkHTTP has its built-in tools for asynchronous requests. Volley, as I recall, initially all requests are sent in asynchronous mode. - pavlofff
  • This code will be executed in the thread where it is called from, as far as I can see. - Daniel Shatz
  • @pavlofff, hmm, enqueue didn’t know about enqueue , thanks) - Yuriy SPb