Android Open Source - Android-Activity-Tracker-for-Dynamics-CRM Network Calls






From Project

Back to project page Android-Activity-Tracker-for-Dynamics-CRM.

License

The source code is released under:

MIT License

If you think the Android project Android-Activity-Tracker-for-Dynamics-CRM 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

// Android Activity Tracker Sample app for Microsoft Dynamics CRM
///*from w  w w.  j  a va2 s  .  c o  m*/
// Copyright (c) Microsoft Corporation
// All rights reserved.
// MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software
// and associated documentation files (the ""Software""), to deal in the Software without
// restriction, including without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
//    The above copyright notice and this permission notice shall be included in all copies
//    or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
// BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.microsoft.activitytracker.Core;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;

import com.microsoft.activitytracker.Classes.ActivityTracker;
import com.microsoft.activitytracker.Classes.Constants;
import com.google.gson.Gson;
import com.google.gson.internal.LinkedTreeMap;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import java.util.Map;

import retrofit.Callback;
import retrofit.RestAdapter;
import retrofit.converter.GsonConverter;
import retrofit.http.Body;
import retrofit.http.GET;
import retrofit.http.Header;
import retrofit.http.Headers;
import retrofit.http.POST;
import retrofit.http.Path;
import retrofit.http.QueryMap;
import retrofit.mime.TypedString;

public class NetworkCalls
{
    private interface CrmService
    {
        @Headers({"Content-Type: text/xml; charset=utf-8"})
        @POST("/XRMServices/2011/Organization.svc/web/")
        void makeSoapPost(@Header("SOAPAction") String soapAction, @Body TypedString body, Callback<Object> callback);

        @Headers({"Accept: application/json"})
        @GET("/XRMServices/2011/OrganizationData.svc/{id}")
        void makeoDataGet(@Path("id") String path, @QueryMap Map<String, String> queries, Callback<LinkedTreeMap<String, Object>> callback);

        @Headers({"Content-Type: application/json", "Accept: application/json"})
        @POST("/XRMServices/2011/OrganizationData.svc/{id}")
        void makeoDataPost(@Path("id") String path, @Body TypedString body, Callback<LinkedTreeMap<String, Object>> callback);
    }

    private static class getAuthorityTask extends AsyncTask<Void, Void, HttpResponse>
    {
        String mEndpoint;

        public getAuthorityTask(String endpoint) {
            mEndpoint = endpoint;
        }

        @Override
        protected HttpResponse doInBackground(Void... params)
        {
            try {
                HttpClient httpClient = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(mEndpoint + "/XRMServices/2011/OrganizationData.svc/web?SdkClientVersion=6.1.0.533");
                httpGet.setHeader("Accept", "application/x-www-form-urlencoded");
                httpGet.setHeader("Authorization", "Brearer");

                return httpClient.execute(httpGet);
            }

            catch(Exception ex) {
                ex.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(HttpResponse response)
        {
        }
    }

    public static Object getAuthority(String endpoint) {
        try {
            AsyncTask authTask = new getAuthorityTask(endpoint).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
            return authTask.get();
        }
        catch (Exception ex) {
            ex.printStackTrace();
        }

        return null;
    }

    /***
     * this is used to make a post to the SOAP endpoint and parses out the SOAP Xml that is returned.
     * @param thisContext the context that you are making this call from
     * @param soapAction the action you want this call to fire (which is passed in the header of the request)
     * @param body the body of the network call you want to send to the SOAP endpoint (make sure you use the soap wrapper and encode the content xml)
     * @param callback callback to invoke when the network call has finished
     */
    public static void soapPostRequest(Context thisContext, String soapAction, String body, Callback<Object> callback)
    {
        SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(thisContext);

        RestAdapter restAdapter = buildSoapRestAdapter(soapAction, appPrefs);

        if (restAdapter != null)
        {
            CrmService crmService = restAdapter.create(CrmService.class);
            crmService.makeSoapPost(soapAction, new TypedString(body), callback);
        }
    }

    /***
     * Based on the soap action build the restadapter. Since both have differently structured xml responses we need
     * choose which parser to use.
     * @param soapAction pass the soap action so we can determine which parser to use with the xml that is returned
     * @param appPrefs app prefs so we can pull the endpoint for the restadapter
     * @return returns the restadapter that we are going to use for this SOAP request
     */
    private static RestAdapter buildSoapRestAdapter(String soapAction, SharedPreferences appPrefs)
    {
        if (soapAction.equals(Constants.SOAP_ACTION_RETRIEVE_MULTIPLE))
        {
            return new RestAdapter.Builder()
//                    .setLogLevel(RestAdapter.LogLevel.FULL)
                    .setEndpoint(appPrefs.getString(Constants.ENDPOINT, ""))
                    .setConverter(new SoapRetrieveMultipleParser())
                    .setRequestInterceptor(ActivityTracker.getRequestInterceptor())
                    .build();
        }
        else if (soapAction.equals(Constants.SOAP_ACTION_EXECUTE))
        {
            return new RestAdapter.Builder()
//                    .setLogLevel(RestAdapter.LogLevel.FULL)
                    .setEndpoint(appPrefs.getString(Constants.ENDPOINT, ""))
                    .setConverter(new SoapExecuteParser())
                    .setRequestInterceptor(ActivityTracker.getRequestInterceptor())
                    .build();
        }
        else
        {
            return null;
        }
    }

    /***
     * Use this method when you want to make a get call to the oData endpoint NOTE: if you are trying to hit the get endpoint, unlike usual
     * this endpoint does restrict you to ONLY get calls
     * @param thisContext the context that you are making this call from
     * @param path the path to add to your current endpoint
     * @param queries map of the parameters to use in the query string
     * @param callback the callback that will be invoked when the network call is finished
     */
    public static void oDataGetRequest(Context thisContext, String path, Map<String, String> queries, Callback<LinkedTreeMap<String, Object>> callback)
    {
        SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(thisContext);

        RestAdapter restAdapter = new RestAdapter.Builder()
//                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setConverter(new GsonConverter(new Gson()))
                .setEndpoint(appPrefs.getString(Constants.ENDPOINT, ""))
                .setRequestInterceptor(ActivityTracker.getRequestInterceptor())
                .build();

        CrmService crmService = restAdapter.create(CrmService.class);
        crmService.makeoDataGet(path, queries, callback);
    }

    /***
     * Use this method when you want to post to the odata endpoint (Means you have a body to your request)
     * @param thisContext the context that you are making this call from
     * @param body the body of the network call you want to send to the oData endpoint
     * @param path the path to add to your current endpoint
     * @param callback the callback that will be invoked when the network call is finished
     */
    public static void oDataPostRequest(Context thisContext, String body, String path, Callback<LinkedTreeMap<String, Object>> callback)
    {
        SharedPreferences appPrefs = PreferenceManager.getDefaultSharedPreferences(thisContext);

        RestAdapter restAdapter = new RestAdapter.Builder()
//                .setLogLevel(RestAdapter.LogLevel.FULL)
                .setConverter(new GsonConverter(new Gson()))
                .setEndpoint(appPrefs.getString(Constants.ENDPOINT, ""))
                .setRequestInterceptor(ActivityTracker.getRequestInterceptor())
                .build();

        CrmService crmService = restAdapter.create(CrmService.class);
        crmService.makeoDataPost(path, new TypedString(body), callback);
    }
}




Java Source Code List

com.microsoft.aad.adal.ADALError.java
com.microsoft.aad.adal.AuthenticationActivity.java
com.microsoft.aad.adal.AuthenticationCallback.java
com.microsoft.aad.adal.AuthenticationCancelError.java
com.microsoft.aad.adal.AuthenticationConstants.java
com.microsoft.aad.adal.AuthenticationContext.java
com.microsoft.aad.adal.AuthenticationException.java
com.microsoft.aad.adal.AuthenticationParameters.java
com.microsoft.aad.adal.AuthenticationRequestState.java
com.microsoft.aad.adal.AuthenticationRequest.java
com.microsoft.aad.adal.AuthenticationResult.java
com.microsoft.aad.adal.AuthenticationSettings.java
com.microsoft.aad.adal.BrokerProxy.java
com.microsoft.aad.adal.CacheKey.java
com.microsoft.aad.adal.ChallangeResponseBuilder.java
com.microsoft.aad.adal.DefaultTokenCacheStore.java
com.microsoft.aad.adal.Discovery.java
com.microsoft.aad.adal.ExceptionExtensions.java
com.microsoft.aad.adal.FileTokenCacheStore.java
com.microsoft.aad.adal.HashMapExtensions.java
com.microsoft.aad.adal.HttpWebRequest.java
com.microsoft.aad.adal.HttpWebResponse.java
com.microsoft.aad.adal.IBrokerProxy.java
com.microsoft.aad.adal.IConnectionService.java
com.microsoft.aad.adal.IDeviceCertificate.java
com.microsoft.aad.adal.IDiscovery.java
com.microsoft.aad.adal.IJWSBuilder.java
com.microsoft.aad.adal.ITokenCacheStore.java
com.microsoft.aad.adal.ITokenStoreQuery.java
com.microsoft.aad.adal.IWebRequestHandler.java
com.microsoft.aad.adal.IdToken.java
com.microsoft.aad.adal.JWSBuilder.java
com.microsoft.aad.adal.Logger.java
com.microsoft.aad.adal.MemoryTokenCacheStore.java
com.microsoft.aad.adal.Oauth2.java
com.microsoft.aad.adal.PRNGFixes.java
com.microsoft.aad.adal.PackageHelper.java
com.microsoft.aad.adal.PromptBehavior.java
com.microsoft.aad.adal.StorageHelper.java
com.microsoft.aad.adal.StringExtensions.java
com.microsoft.aad.adal.TokenCacheItem.java
com.microsoft.aad.adal.UserInfo.java
com.microsoft.aad.adal.WebRequestHandler.java
com.microsoft.aad.adal.package-info.java
com.microsoft.activitytracker.Activities.CheckInActivity.java
com.microsoft.activitytracker.Activities.ItemActivity.java
com.microsoft.activitytracker.Activities.MainActivity.java
com.microsoft.activitytracker.Activities.SetupActivity.java
com.microsoft.activitytracker.Adapters.ActivitiesItemAdapter.java
com.microsoft.activitytracker.Adapters.MainItemAdapter.java
com.microsoft.activitytracker.Classes.ActivityTracker.java
com.microsoft.activitytracker.Classes.Constants.java
com.microsoft.activitytracker.Classes.Entity.java
com.microsoft.activitytracker.Classes.RecentHistorydbHandler.java
com.microsoft.activitytracker.Classes.Utils.java
com.microsoft.activitytracker.Core.NetworkCalls.java
com.microsoft.activitytracker.Core.SoapExecuteParser.java
com.microsoft.activitytracker.Core.SoapRetrieveMultipleParser.java