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");
}