public class HomeFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
com.ixlas.linearlayout.databinding.FragmentHomeBinding binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
TextView textView = binding.txtHome;
textView.setText("Bla Bla Bla");
try {
HomeFragment.gsonResponse();
} catch (Exception e) {
e.printStackTrace();
}
return root;
}
public static void gsonResponse() throws Exception
{
URL url = new URL("https://fleet-api.taxi.yandex.net/v1/parks/driver-profiles/list");
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
httpConn.setRequestMethod("POST");
httpConn.setRequestProperty("X-Client-ID", "******");
httpConn.setRequestProperty("X-Api-Key", "******");
httpConn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
httpConn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(httpConn.getOutputStream());
writer.write("{\n \"query\": {\n \"park\": {\n \"id\": \"******\"\n }\n }\n}");
writer.flush();
writer.close();
httpConn.getOutputStream().close();
InputStream responseStream = httpConn.getResponseCode() / 100 == 2
? httpConn.getInputStream()
: httpConn.getErrorStream();
Scanner s = new Scanner(responseStream).useDelimiter("\\A");
String response = s.hasNext() ? s.next() : "";
System.out.println(response);
Log.d("TEXT", response);
}
}