package com.example.mytest; import org.json.JSONArray; import org.json.JSONObject; import java.io.BufferedReader; import java.io.DataInputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class API extends Thread { // адрес запроса public String url; // параметры, передаваемые с запросом public String param; public Interface context; @Override public void run() { try { URL _url = new URL(url); HttpURLConnection connection = (HttpURLConnection) _url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/json"); connection.setDoOutput(true); connection.setDoInput(true); connection.getOutputStream().write(param.getBytes()); connection.getOutputStream().flush(); connection.connect(); InputStreamReader inputStream = new InputStreamReader(connection.getInputStream()); BufferedReader br = new BufferedReader(inputStream); String response = br.readLine(); JSONObject json = new JSONObject(response); if (json.has("Error")) { if (!json.getString("Error").equals("null")) { System.out.println(json.getString("Error")); return; } if (json.has("News")) { if (!json.getString("News").equals("null")) { JSONArray news = json.getJSONArray("News"); context.Main(news); } } } } catch (Exception e) { e.printStackTrace(); } } }