Alfresco is a popular Enterprise Content Management (ECM) and Business Process Management (BPM) tool, widely used as a web application. With the increasing involvement of mobile devices in business use cases, the dependency of mobile devices in daily work has also been increased. As, we may be aware of the Alfresco Content Service Android Application available on the playstore, but sometimes client requirement leads us to develop a standalone application for the specific purpose like scanning barcodes from the mobile device and storing it to the Alfresco Repository or some other specific requirement.
Alfresco is based on RESTful APIs and can be communicated with other platforms via REST APIs easily. We can use Alfresco login REST API to integrate it with Android Application. This blog is a step-by-step guide to integrate Alfresco login API with Android.
Alfresco development services Related blog: Edit Documents Online (ONLYOFFICE Integration)
Create one New Project in Android Studio and follow below steps.
Step 1: Add the following code inside activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fitsSystemWindows="true"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingTop="56dp" android:paddingLeft="24dp" android:paddingRight="24dp"> <!-- Logo --> <ImageView android:src="@drawable/logo" android:layout_width="wrap_content" android:layout_height="72dp" android:layout_marginBottom="24dp" android:layout_gravity="center_horizontal" /> <!-- Siteurl --> <EditText android:id="@+id/et_login_siteurl" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" android:hint="Site Url" /> <!-- Username --> <EditText android:id="@+id/et_login_username" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" android:hint="Username"/> <!-- Password --> <EditText android:id="@+id/et_login_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="textPassword" android:hint="Password"/> <!-- LogIn Button --> <Button android:id="@+id/btn_login" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:layout_marginBottom="24dp" android:padding="12dp" android:text="Login"/> </LinearLayout> </ScrollView>
Step 2: Edit AndroidManifest.xml.
- Inside <manifest>….. </manifest> add following line
<uses-permission android:name="android.permission.INTERNET" />
- Inside <application …..> add following line
android:usesCleartextTraffic="true"
- Inside <application> ……</application> add foolowing line
<uses-library android:name="org.apache.http.legacy" android:required="false"/>
Step 3: Add the following code to build.gradle(Module: app) file.
- Inside dependencies{} add following lines :
implementation('com.omertron:themoviedbapi:4.2') { exclude group: 'org.apache.httpcomponents', module: 'httpclient' exclude module: 'junit' } implementation 'org.apache.httpcomponents:httpclient-android:4.3.5.1' implementation 'commons-io:commons-io:2.4'
- Inside android{} add the following line :
useLibrary 'org.apache.http.legacy'
Step 4: Add the following code inside MainActivity.java
public class MainActivity extends AppCompatActivity { static EditText et_login_siteurl, et_login_username, et_login_password; Button btn_login; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); et_login_siteurl = (EditText)findViewById(R.id.et_login_siteurl); et_login_username = (EditText)findViewById(R.id.et_login_username); et_login_password = (EditText)findViewById(R.id.et_login_password); btn_login = (Button)findViewById(R.id.btn_login); btn_login.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try { userLogin(); } catch (Exception e) { e.printStackTrace(); } } }); } public void userLogin(){ final String siteurl = et_login_siteurl.getText().toString(); final String username = et_login_username.getText().toString(); final String password = et_login_password.getText().toString(); UserLogin u = new UserLogin(); u.execute("http://"+siteurl+"/alfresco/service/api/login?u="+username+"&pw="+password+"&format=json"); } class UserLogin extends AsyncTask<String, Void, String>{ @Override protected String doInBackground(String... params) { try{ HttpClient httpclient; httpclient = new DefaultHttpClient(); HttpResponse response; String responseString = null; JSONObject jsonReader = null; response = httpclient.execute(new HttpGet(params[0])); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpStatus.SC_OK) { ByteArrayOutputStream out = new ByteArrayOutputStream(); response.getEntity().writeTo(out); out.close(); responseString = out.toString(); Log.d("------ResponseString--", responseString); jsonReader = new JSONObject(responseString); Intent i = new Intent(MainActivity.this, NextActivity.class); //NextActivity.class shows the next screen you want to go //after login startActivity(i); } }catch(Exception e){ e.printStackTrace(); } return null; } } }
Above is the View of App created by above code. In the Site Url section, add the server URL and username and password in the App and click on the Login button which will redirect the App to the next page.
If you have any questions or feedback, feel free to contact us or comment on this blog. To more about the Alfresco Services for the different business scenario, please contact us.
Thanks,