When I click on the button in MainActivity, I need to request data. There is a special address at which they lie in the json view

Does it seem to me or is it difficult in java?)

Rights for the Internet, I have indicated. Everything with the problem code)

URL url = new URL("http://hashcode.ru").openStream() 

Here underlines the red line with the address .... (((

    2 answers 2

      public static String doGet(String url) throws Exception { URL obj = new URL(url); HttpURLConnection connection = (HttpURLConnection) obj.openConnection(); //add reuqest header connection.setRequestMethod("GET"); connection.setRequestProperty("User-Agent", "Mozilla/5.0" ); connection.setRequestProperty("Accept-Language", "en-US,en;q=0.5"); connection.setRequestProperty("Content-Type", "application/json"); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuffer response = new StringBuffer(); while ((inputLine = bufferedReader.readLine()) != null) { response.append(inputLine); } bufferedReader.close(); // print result Log.d(TAG,"Response string: " + response.toString()); return response.toString(); } 

    You can use it like this:

      new AsyncTask<Void, String, String>() { @Override protected String doInBackground(Void... voids) { String s = ""; try { s = doGet(myURL); } catch (Exception e) { e.printStackTrace(); } return s; } @Override protected void onPostExecute(final String result) { runOnUiThread(new Runnable() { @Override public void run() { tvRez.setText(result); } }); } }.execute(); 
    • How to use this doGet function? I do this - it highlights in red tvRez.setText (doGet (myURL)); - Alex Lizenberg
    • try {tvRez.setText (doGet (myURL)); } catch (Exception e) {e.printStackTrace (); } - Kirill Stoianov
    • The red is gone, but when I started up the studio I hung up. On the phone, the application started, but it does not plow. And the studio is still hanging ... Is it possible that in android requests by the rules should not be done in asynchronous tasks? Maybe because of this - Alex Lizenberg
    • @ AlexLizenberg yes, there is something like NetworkOnMainThread, I added AsyncTask - Kirill Stoianov to the reply
    • one
      Krunt with all figured out, I will count now. Thank you - Alex Lizenberg

    Did using thread:

     public class JsonTask extends AsyncTask<String,String,String> { private final String URL = "google.com"; private ArrayList<NameValuePair> postParams = new ArrayList<>(); private JSONObject jsonObject = new JSONObject(); @Override protected void onPreExecute() { super.onPreExecute(); } @Override protected String doInBackground(String... params) { postParams.add(new BasicNameValuePair("ParamKey", "ParamValue")); jsonObject = JSONParser.makeHttpRequest(URL, "GET", postParams); return jsonObject.toString(); } } 

    And here is the parser itself for parsing JSON:

     public class JSONParser { static InputStream is = null; static JSONObject jObj = null; static String json = ""; public static JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) { try { if (method == "POST") { DefaultHttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new UrlEncodedFormEntity(params)); HttpResponse httpResponse = httpClient.execute(httpPost); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } else if (method == "GET") { DefaultHttpClient httpClient = new DefaultHttpClient(); String paramString = URLEncodedUtils.format(params, "utf-8"); url += "?" + paramString; HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); is = httpEntity.getContent(); } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { System.out.println("Internet Error.") } try { BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); StringBuilder sb = new StringBuilder(); String line = null; while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } is.close(); json = sb.toString(); } catch (Exception e) { System.out.println("Buffer Error : Error converting result " + e.toString()); } try { jObj = new JSONObject(json); } catch (JSONException e) { System.out.println("JSON Parser: + Error parsing data " + e.toString()); } return jObj; } } 
    • Can you give an example of how to use all this line? For example, to request an URL and how to address the elements later - Alex Lizenberg
    • And by the way, I emphasize static red everywhere - Alex Lizenberg
    • Yes, there is an example here link - Dima Pashko
    • Chet zamudreno) like that, tired of dealing with these red bugs - Alex Lizenberg