Android Open Source - MobileConnectTestApp Retrieve Userinfo Task






From Project

Back to project page MobileConnectTestApp.

License

The source code is released under:

MIT License

If you think the Android project MobileConnectTestApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.gsma.android.xoperatorapidemo.activity.identity;
//w w w  .  j  av  a 2 s .  com
import java.io.InputStream;
import java.util.HashMap;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.params.ClientPNames;
import org.apache.http.params.HttpParams;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.AsyncTask;
import android.util.Log;

import com.gsma.android.xoperatorapidemo.utils.HttpUtils;
import com.gsma.android.xoperatorapidemo.utils.JsonUtils;

/*
 * this is a background task which initiates the identity process using OpenID Connect
 */
public class RetrieveUserinfoTask extends AsyncTask<Void, Void, JSONObject> {
  private static final String TAG = "RetrieveUserinfoTask";

  String userinfoUri; //
  String access_token; // 
  String redirectUri; //

  OpenIDConnectAbstractActivity initiator;
  
  /*
   * standard constructor - receives information from MainActivity
   */
  public RetrieveUserinfoTask(OpenIDConnectAbstractActivity initiator, String access_token, String userinfoUri) {
    this.initiator = initiator;
    this.access_token = access_token;
    this.userinfoUri = userinfoUri;
  }
  
  @Override
  protected JSONObject doInBackground(Void... params) {
    JSONObject json=null;
        
    Log.d(TAG, "Pausing a short while");
    try {
      Thread.sleep(3000);
    } catch (Exception e) {}

    Log.d(TAG, "requestUri="+userinfoUri);
    
    HttpGet httpRequest = new HttpGet(userinfoUri);
        
    httpRequest.addHeader("Accept", "application/json");
    httpRequest.addHeader("Authorization", "Bearer "+access_token);

    HttpClient httpClient = HttpUtils.getHttpClient();
    HttpParams httpParams = httpRequest.getParams();
    httpParams.setParameter(ClientPNames.HANDLE_REDIRECTS,Boolean.TRUE);
    httpRequest.setParams(httpParams);

    try {
      HttpResponse httpResponse = httpClient.execute(httpRequest);
      
      Log.d(TAG, "Request completed with status="+httpResponse.getStatusLine().getStatusCode());
  
      /*
       * obtain the headers from the httpResponse. Content-Type and
       * Location are particularly required
       */
      HashMap<String, String> headerMap = HttpUtils
          .getHeaders(httpResponse);
      String contentType = headerMap.get("content-type");
      String location = headerMap.get("location");
  
      /*
       * the status code from the HTTP response is also needed in
       * processing
       */
      int statusCode = httpResponse.getStatusLine().getStatusCode();
  
      Log.d(TAG, "status=" + statusCode + " CT=" + contentType + " Loc="
          + location + " JSON?" + HttpUtils.isJSON(contentType)
          + " HTML?" + HttpUtils.isHTML(contentType));
      
      HttpEntity httpEntity = httpResponse.getEntity();
      InputStream is = httpEntity.getContent();
      
      String responseData=JsonUtils.readString(is);
      Log.d(TAG, "Converting response data "+responseData);
      json=new JSONObject(responseData);
      
    } catch (java.io.IOException ioe) {
      Log.e(TAG, "IOException "+ioe.getMessage());
      json=new JSONObject();
      try {
        json.put("Exception", "IOException");
        json.put("Message", ioe.getMessage());
      } catch (JSONException e) {
      }
    } catch (JSONException je) {
      Log.e(TAG, "JSONException "+je.getMessage());
      json=new JSONObject();
      try {
        json.put("Exception", "JSONException");
        json.put("Message", je.getMessage());
      } catch (JSONException e) {
      }
    }
    
    return json;
  }
  
  @Override
  protected void onPostExecute(JSONObject response) {
    initiator.processUserinfoResponse(response);
  }

}




Java Source Code List

com.gsma.android.xoperatorapidemo.activity.MainActivity.java
com.gsma.android.xoperatorapidemo.activity.SettingsActivity.java
com.gsma.android.xoperatorapidemo.activity.discovery.ActiveDiscoveryTask.java
com.gsma.android.xoperatorapidemo.activity.discovery.DiscoveryProcessEndpoints.java
com.gsma.android.xoperatorapidemo.activity.discovery.DisplayDiscoveryWebsiteActivity.java
com.gsma.android.xoperatorapidemo.activity.discovery.PassiveDiscoveryTask.java
com.gsma.android.xoperatorapidemo.activity.discovery.ProcessDiscoveryToken.java
com.gsma.android.xoperatorapidemo.activity.identity.AuthorizationCompleteActivity.java
com.gsma.android.xoperatorapidemo.activity.identity.DisplayIdentityWebsiteActivity.java
com.gsma.android.xoperatorapidemo.activity.identity.OpenIDConnectAbstractActivity.java
com.gsma.android.xoperatorapidemo.activity.identity.RetrieveTokenTask.java
com.gsma.android.xoperatorapidemo.activity.identity.RetrieveUserinfoTask.java
com.gsma.android.xoperatorapidemo.discovery.Api.java
com.gsma.android.xoperatorapidemo.discovery.DeveloperOperatorSetting.java
com.gsma.android.xoperatorapidemo.discovery.DiscoveryData.java
com.gsma.android.xoperatorapidemo.discovery.DiscoveryDeveloperOperatorSettings.java
com.gsma.android.xoperatorapidemo.discovery.DiscoveryServingOperatorSettings.java
com.gsma.android.xoperatorapidemo.discovery.DiscoveryStartupSettings.java
com.gsma.android.xoperatorapidemo.discovery.LinkConstants.java
com.gsma.android.xoperatorapidemo.discovery.Link.java
com.gsma.android.xoperatorapidemo.discovery.Response.java
com.gsma.android.xoperatorapidemo.discovery.ServingOperatorSetting.java
com.gsma.android.xoperatorapidemo.identity.UserinfoAddress.java
com.gsma.android.xoperatorapidemo.identity.Userinfo.java
com.gsma.android.xoperatorapidemo.logo.LogoCacheItem.java
com.gsma.android.xoperatorapidemo.logo.LogoCache.java
com.gsma.android.xoperatorapidemo.logo.LogoLoaderTask.java
com.gsma.android.xoperatorapidemo.logo.LogoResponseArray.java
com.gsma.android.xoperatorapidemo.logo.LogoResponse.java
com.gsma.android.xoperatorapidemo.utils.HttpUtils.java
com.gsma.android.xoperatorapidemo.utils.JsonUtils.java
com.gsma.android.xoperatorapidemo.utils.KeyValuePair.java
com.gsma.android.xoperatorapidemo.utils.ParameterList.java
com.gsma.android.xoperatorapidemo.utils.PhoneState.java
com.gsma.android.xoperatorapidemo.utils.PhoneUtils.java
com.gsma.android.xoperatorapidemo.utils.PreferencesUtils.java