Issue
I am attempting to get a token from a JSON endpoint using the following code:
public class FetchToken extends AsyncTask<String, Void, Void> {
String data = "";
String token = "";
public TokenDelegate delegate = null;
@Override
protected Void doInBackground(String... identity) {
try {
if (identity.length == 1) {
URL url = new URL(identity[0]);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while (line != null) {
line = bufferedReader.readLine();
data = data + line;
}
JSONObject JO = new JSONObject(data);
token = JO.get("token").toString();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
Log.d("token", token);
//delegate.processFinish(token);
super.onPostExecute(aVoid);
}
}
I have tried printing to the debug log after every line, and it seems that the line InputStream inputStream = httpURLConnection.getInputStream();
never finishes, nor does the rest of the code get executed. However, this is taking place inside of an AsyncTask, and the onPostExecute method of that AsyncTask is firing as if the task has been completed, which it doesn't seem as if it has been.
None of the catch blocks are being executed, it seems like, and when I inspect the Network panel in the Android Profiler then the request says it is not passing anything back. However, accessing the JSON endpoint in browser brings everything back correctly.
Hopefully someone knows what the problem may be. I've attempted to google things to no avail. Let me know if more information is needed. Thanks.
Solution
The line InputStream inputStream = httpURLConnection.getInputStream();
was never completing because it was throwing a java.security.cert.CertPathValidatorException
. The error was not being logged because I was not catching this type of error in my try / catch block.
Answered By - Dehodson
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.