简体   繁体   中英

How to access and retrieve Salesforce objects via Volley API calls in Android?

I'm trying to access and retrieve SFDC Objects via an Android Application. How would we be able to do so? I'm told that we can make use of SFDC's REST APIs like below and make CRUD operations like below: https://yourorg.com/services/data/v48.0/sobjects/custom-object

But I'm no sure how to go on with the authentication part. Help me out! Below is the code I've written so far

package com.example.shoppingstore;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;

public class SalesforceRESTAPI {
    String accessToken = "my access token";
    public void getCategories(final Context context){
        String URL = "my URL";
        RequestQueue requestQueue = Volley.newRequestQueue(context);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
                Request.Method.GET,
                URL,
                null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        //the categories are returned
                        Log.e("Response REST", response.toString());
                        Toast.makeText(context, "Worked", Toast.LENGTH_SHORT).show();
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        //error occured while retriving the categories
                        Log.e("Response REST", error.toString());
                        Toast.makeText(context, error.toString(), Toast.LENGTH_SHORT).show();
                    }
                }
        ) {
            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String>  params = new HashMap<String, String>();
                params.put("Type", "oAuth2.0");
                params.put("Authentication", accessToken);
                params.put("Accept-Encoding", "gzip, deflate, br");
                params.put("Connection", "keep-alive");
                params.put("Accept", "*/*");
                return params;
            }
        };
        requestQueue.add(jsonObjectRequest);

    }
}

Uh, this can be a massive topic. How do you plan to authenticate. Will you have 1 service account that connects to SF from all mobile devices? If you do - how are you going to store credentials? What if somebody hacks into the package and you need to mass change password?

Or will you let people login to SF using their own Salesforce username and password. Maybe not even display a form for them to type stuff in, you'd just show them SF login page and on success they're redirected back to your app. This is especially cool when they don't really have a SF password, the company has configured Single Sign On and they login to SF via their Google/Windows/Facebook/what-have-you account...

These things have been solved in the past. If you haven't done so already - read up about OAuth2 and what OAuth2 flows you can do with Salesforce.

I know, it's just a bunch of links but try to read through if not execute at least the trailheads. Few hours of reading and planning can save you weeks of coding yourself into a corner.

I mean I've never written an app in my life but chances are Android SDK & articles here already have everything you need: https://developer.salesforce.com/developer-centers/mobile/ . Looks like SDK even comes with sample apps: https://developer.salesforce.com/docs/atlas.en-us.mobile_sdk.meta/mobile_sdk/android_sample_app.htm

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM