1

The code as below can be sucessfully to get the JSON from SharePoint on-premise.

When I update the program try to access the SharePoint Online

url = https://testing.onmicrosoft.com/_api/web/GetFolderByServerRelativeUrl('Documents')/files?$top=1
NTCredentials ntc = new NTCredentials("userName", "password", "", "");//<--no domain

HttpHost target = new HttpHost("onmicrosoft.com", 443, "https");//<--I not sure is it correct

and run the program again , it will return the error for me (unauthorized error)

how can be successful to access the SharePoint online from Android??

private class HttpAsyncTask extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... urls) {

        return GET(urls[0]);
    }


    @Override
    protected void onPostExecute(String result) {
        Toast.makeText(getBaseContext(), "Received!", Toast.LENGTH_LONG).show();
        etResponse.setText("Result is:"+result);
   }
}


public static String GET(String url){
    InputStream inputStream = null;
    String result = "";
    try {


        HttpClient httpclient = new DefaultHttpClient();
        ((AbstractHttpClient) httpclient).getAuthSchemes().register("ntlm", new AuthSchemeFactory() {
               @Override
               public AuthScheme newInstance(HttpParams hp) {
                return new NTLMScheme(new JCIFSEngine());
               }
              });
        NTCredentials ntc = new NTCredentials("username", "password", "", "domainName");
        ((AbstractHttpClient) httpclient).getCredentialsProvider().setCredentials(AuthScope.ANY, ntc);
        HttpGet httpGet= new HttpGet(url);
     HttpHost target = new HttpHost("testing.com", 80, "http");
        HttpResponse httpResponse = httpclient.execute(target,httpGet);
        try {
        // receive response as inputStream
        inputStream = httpResponse.getEntity().getContent();

        // convert inputstream to string
        if(inputStream != null)
            result = convertInputStreamToString(inputStream);
        else
            result = "Did not work!";

    } catch (Exception e) {
        Log.d("InputStream:", e.getLocalizedMessage());
    }

    }catch(Exception e){

          Log.d("InputStream:", e.getLocalizedMessage());
    }
    return result;
}

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.linear);
    etResponse = (EditText) findViewById(R.id.etResponse);
    tvIsConnected = (TextView) findViewById(R.id.tvIsConnected);
    if(isConnected()){
        tvIsConnected.setBackgroundColor(0xFF00CC00);
        tvIsConnected.setText("You are conncted");
    }
    else{
        tvIsConnected.setText("You are NOT conncted");
    }
    new HttpAsyncTask().execute("http://testing.com/_api/web/GetFolderByServerRelativeUrl('Documents')/files?$top=1");
}
2

1 Answer 1

1

Disclaimer: I'm one of the devs of the Office 365 SDK for Android.

As @Anders points, you please refer to the Office 365 SDK for Android to see how to authenticate against a Sharepoint Online.

You need to authenticate against Azure Active Directory, and in order to do that, you have to create an Active Directory Application and set the correct permissions. Once you have the registered application, you'll have a Client ID and redirect URL. With this two pieces of information you can ask for an Oauth2 token and access your shareopoint site.

The token request is being handled by another library, ADAL : https://github.com/AzureAD/azure-activedirectory-library-for-android

Please refer to this sample: https://github.com/OfficeDev/Office-365-SDK-for-Android/tree/master/samples/read-sp-list-sample that fits you're scenario or download all the samples at once : https://office365androidsamples.blob.core.windows.net/samples/office365-android-sdk-samples.zip

Thanks, Marcos

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.