The best way to create a web service on Android

I want to implement a web service on Android. I want to use my device as a server . After doing some research on the Internet, I found several options:

  • SerDroid (a small web server for the Android platform)

  • i-Jetty (an open source web container to run on the Android mobile device platform)

  • KWS (lightweight and fast web server specifically designed for Android mobile devices)

What's better?

Alternatively, can I use REST + JSON to implement a web service on Android? I'm not so upset that there is REST + JSON ...

+4
source share
1 answer

Here is an example of a REST + JSON service

package rainprod.utils.internetservice; import java.io.ByteArrayOutputStream; import java.io.DataInputStream; import java.io.IOException; import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicHeader; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.protocol.HTTP; import org.json.JSONObject; import android.os.Handler; import android.os.Message; public abstract class InternetService extends Handler { public static final int CONNECTING = 1; public static final int UPLOADING = 2; public static final int DOWNLOADING = 3; public static final int COMPLETE = 4; public static final int ERROR = -1; private IInternetService listener; public InternetService(IInternetService listener) { this.listener = listener; } public DefaultHttpClient getDefaultClient() { HttpParams httpParams = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(httpParams, 15000); HttpConnectionParams.setSoTimeout(httpParams, 15000); DefaultHttpClient httpClient = new DefaultHttpClient(httpParams); return httpClient; } public void postJson(JSONObject postObject, String functionName, int responseCode, int subResponseCode, boolean isThreadly) { DefaultHttpClient httpClient = this.getDefaultClient(); HttpPost postRequest = new HttpPost(this.getServiceURLString() + functionName); this.restApiPostRequest(httpClient, postRequest, postObject, responseCode, subResponseCode); } abstract public String getServiceURLString(); public void restApiPostRequest(final DefaultHttpClient httpclient, final HttpPost request, final JSONObject postObject, final int responseCode, final int subResponseCode) { new Thread(new Runnable() { @Override public void run() { try { try { StringEntity se = new StringEntity(postObject .toString(), HTTP.UTF_8); se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); request.setEntity(se); HttpResponse response = httpclient.execute(request); HttpEntity entity = response.getEntity(); if (entity != null) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); DataInputStream dis = new DataInputStream(entity .getContent()); byte[] buffer = new byte[1024];// In bytes int realyReaded; double contentSize = entity.getContentLength(); double readed = 0L; while ((realyReaded = dis.read(buffer)) > -1) { baos.write(buffer, 0, realyReaded); readed += realyReaded; sendProgressMessage((double) contentSize, readed, responseCode, DOWNLOADING); } sendCompleteMessage(new InternetServiceResponse( responseCode, baos, subResponseCode), COMPLETE); } else { sendErrorMessage(responseCode, new Exception("Null"), request.getURI() .toString(), ERROR); } } catch (ClientProtocolException e) { sendErrorMessage(responseCode, e, request.getURI() .toString(), ERROR); } catch (IOException e) { sendErrorMessage(responseCode, e, request.getURI() .toString(), ERROR); } catch (OutOfMemoryError e) { sendErrorMessage(responseCode, new Exception( "Out memory"), request.getURI().toString(), ERROR); } finally { httpclient.getConnectionManager().shutdown(); } } catch (NullPointerException e) { sendErrorMessage(responseCode, e, request.getURI() .toString(), ERROR); } } }).start(); } private void sendProgressMessage(double contentSize, double readed, int responseCode, int progressCode) { this.sendMessage(this.obtainMessage(progressCode, new InternetServiceResponse(contentSize, readed, responseCode))); } protected void sendErrorMessage(int responseCode, Exception exception, String string, int errorCode) { this.sendMessage(this.obtainMessage( errorCode, new InternetServiceResponse(responseCode, exception .getMessage()))); } protected void sendCompleteMessage(InternetServiceResponse serviceResponse, int completeCode) { this.sendMessage(this.obtainMessage(completeCode, serviceResponse)); } @Override public void handleMessage(Message msg) { final InternetServiceResponse bankiServiceResponse = (InternetServiceResponse) msg.obj; switch (msg.what) { case UPLOADING: break; case DOWNLOADING: this.listener.downloadingData(bankiServiceResponse.contentSize, bankiServiceResponse.readed, bankiServiceResponse.responseCode); break; case COMPLETE: this.listener.completeDownload(bankiServiceResponse); break; case ERROR: this.listener.errorHappen(bankiServiceResponse.textData, bankiServiceResponse.responseCode); break; } } } 

And a usage example for UserAPI

 package ru.orionsource.missingcar.api; import rainprod.utils.internetservice.IInternetService; import rainprod.utils.internetservice.InternetService; import ru.orionsource.missingcar.classes.User; public class UserAPI extends InternetService { public UserAPI(IInternetService listener) { super(listener); } @Override public String getServiceURLString() { return "http://ugnali.orionsource.ru/u_api/?apiuser."; } public void userLogin(User user) { this.postJson(user.toJson(), "logonUser", 1, 0, true); } } 

And the call from the source:

 this.userAPI = new UserAPI(this); User selected = new User(); selected.email = this.accounts.get(arg2).name; this.userAPI.userLogin(selected); 
0
source

Source: https://habr.com/ru/post/1315391/


All Articles