Created
July 25, 2019 09:58
-
-
Save GitEliteNovice/a1b7dc8a16606ed54f75beb62462eaf2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public void sendGetRequest(String urlString, String accessToken) throws Exception { | |
URL url = new URL(urlString); | |
HttpsURLConnection con = (HttpsURLConnection) url.openConnection(); | |
con.setRequestMethod("GET"); | |
con.setRequestProperty("Authorization", "Bearer " + accessToken); | |
con.setRequestProperty("cache-control", "no-cache"); | |
con.setRequestProperty("X-Restli-Protocol-Version", "2.0.0"); | |
BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream())); | |
StringBuilder jsonString = new StringBuilder(); | |
String line; | |
while ((line = br.readLine()) != null) { | |
jsonString.append(line); | |
} | |
JSONObject jsonObject = new JSONObject(jsonString.toString()); | |
Log.d("Complete json object", jsonObject.toString()); | |
try { | |
linkedInUserId = jsonObject.getString("id"); | |
String country = jsonObject.getJSONObject("firstName").getJSONObject("preferredLocale").getString("country"); | |
String language = jsonObject.getJSONObject("firstName").getJSONObject("preferredLocale").getString("language"); | |
String getFirstnameKey = language + "_" + country; | |
linkedInUserFirstName = jsonObject.getJSONObject("firstName").getJSONObject("localized").getString(getFirstnameKey); | |
linkedInUserLastName = jsonObject.getJSONObject("firstName").getJSONObject("localized").getString(getFirstnameKey); | |
linkedInUserProfile = jsonObject.getJSONObject("profilePicture").getJSONObject("displayImage~").getJSONArray("elements").getJSONObject(0).getJSONArray("identifiers").getJSONObject(0).getString("identifier"); | |
} catch (JSONException e) { | |
e.printStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment