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);
}