1

I want to get JSON data from URL but I have to login first.
So, I have to post username and password to login page and then, I request JSON using GET to other URL.
I thought I had to get a cookie.
What codes do I have to add?

try {

    Log.d(TAG, REQUEST_URL);
    URL url = new URL(REQUEST_URL);
    HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();

    httpURLConnection.setReadTimeout(3000);
    httpURLConnection.setConnectTimeout(3000);
    httpURLConnection.setDoOutput(true);
    //httpURLConnection.setDoInput(true);
    httpURLConnection.setRequestMethod("GET");
    httpURLConnection.setUseCaches(false);
    httpURLConnection.connect();

    int responseStatusCode = httpURLConnection.getResponseCode();

    InputStream inputStream;
    if (responseStatusCode == HttpURLConnection.HTTP_OK) {

        inputStream = httpURLConnection.getInputStream();
    } else {
        inputStream = httpURLConnection.getErrorStream();

    }


    InputStreamReader inputStreamReader = new InputStreamReader(inputStream, "UTF-8");
    BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

    StringBuilder sb = new StringBuilder();
    String line;


    while ((line = bufferedReader.readLine()) != null) {
        sb.append(line);
    }

    bufferedReader.close();
    httpURLConnection.disconnect();

    result = sb.toString().trim();


} catch (Exception e) {
    result = e.toString();
}


Message message = mHandler.obtainMessage(LOAD_SUCCESS, result);
mHandler.sendMessage(message);

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
SoosanShin
  • 85
  • 4
  • Use retrofit to make network calls, you can mange cookies using OkHttp https://stackoverflow.com/questions/34881775/automatic-cookie-handling-with-okhttp-3 – Geo Jul 25 '18 at 06:25
  • `What codes do I have to add` you should ask this to whoever developed the server where you are trying to login. – Vladyslav Matviienko Jul 25 '18 at 06:32

0 Answers0