Skip to content

Instantly share code, notes, and snippets.

@GitEliteNovice
Created July 25, 2019 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save GitEliteNovice/911fda1c2ff8be63537d5696a3d88df0 to your computer and use it in GitHub Desktop.
Save GitEliteNovice/911fda1c2ff8be63537d5696a3d88df0 to your computer and use it in GitHub Desktop.
private class PostRequestAsyncTask extends AsyncTask<String, Void, Boolean> {
@Override
protected void onPreExecute() {
pd = ProgressDialog.show(NewLinkedInIntegration.this, "", "Loading", true);
}
@Override
protected Boolean doInBackground(String... urls) {
if (urls.length > 0) {
String url = urls[0];
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpost = new HttpPost(url);
try {
HttpResponse response = httpClient.execute(httpost);
if (response != null) {
//If status is OK 200
if (response.getStatusLine().getStatusCode() == 200) {
String result = EntityUtils.toString(response.getEntity());
JSONObject resultJson = new JSONObject(result);
int expiresIn = resultJson.has("expires_in") ? resultJson.getInt("expires_in") : 0;
String accessToken = resultJson.has("access_token") ? resultJson.getString("access_token") : null;
Log.e("Tokenm", "" + accessToken);
if (expiresIn > 0 && accessToken != null) {
Log.i("Authorize", "This is the access Token: " + accessToken + ". It will expires in " + expiresIn + " secs");
Calendar calendar = Calendar.getInstance();
calendar.add(Calendar.SECOND, expiresIn);
long expireDate = calendar.getTimeInMillis();
SharedPreferences preferences = NewLinkedInIntegration.this.getSharedPreferences("user_info", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putLong("expires", expireDate);
editor.putString("accessToken", accessToken);
editor.commit();
return true;
}
}
}
} catch (IOException e) {
Log.e("Authorize", "Error Http response " + e.getLocalizedMessage());
} catch (ParseException e) {
Log.e("Authorize", "Error Parsing Http response " + e.getLocalizedMessage());
} catch (JSONException e) {
Log.e("Authorize", "Error Parsing Http response " + e.getLocalizedMessage());
}
}
return false;
}
@Override
protected void onPostExecute(Boolean status) {
if (pd != null && pd.isShowing()) {
pd.dismiss();
}
if (status) {
SharedPreferences preferences = NewLinkedInIntegration.this.getSharedPreferences("user_info", 0);
accessToken = preferences.getString("accessToken", null);
try {
if (accessToken != null) {
new GetProfileRequestAsyncTask().execute(profileUrl);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment