Android mobile client / server communication using the Hessian binary protocol

  Android, Hessdroid, Hessian, Tutorial

Create the Android client

Contents

Objects are  being transferred from the server to a client. Therefore,  clients also needs all these classes and interfaces in order to deal with those objects. In our case, you need to have Google Android Studio setup and running. Four steps to success:

  1. Create a new empty activity project using the wizard.
  2. Copy the CustomerAPI.java and Customer.java files from your server project to your Android project.
  3. Include the hessdroid.jar file to your project’s build path
  4. Add the INTERNET permission to your manifest.xml file <uses-permission>

Here is the code for your main activity:

package de.neuromechanics.hessianproject;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import com.caucho.hessian.client.HessianProxyFactory;

import java.net.MalformedURLException;
import java.util.concurrent.ExecutionException;

public class MainActivity extends AppCompatActivity {
    TextView tvResult;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tvResult = (TextView) findViewById(R.id.tvResult);
        TxCustomer task = new TxCustomer();
        try {
            task.execute().get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }
    private class TxCustomer extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String[] params) {
            String url = "http://your_server:port/CustomerServiceImpl/hello";
            HessianProxyFactory factory = new HessianProxyFactory();
            factory.setHessian2Reply(false);
            final CustomerAPI customer;
            try {
                customer = (CustomerAPI) factory.create(CustomerAPI.class, url, getClassLoader());
            } catch (MalformedURLException e) {
                e.printStackTrace();
                return "ERROR";
            }
            return customer.hello();
        }

        @Override
        protected void onPostExecute(String message) {
            tvResult.setText(message);
        }
    }
}

LEAVE A COMMENT