(FRONT) FRONT (2025)

Return to Simple HTTP service HTTPService

   1:  package com.voipplus.mmsclient.httpService;
   2:  
   3:  import android.app.Service;
   4:  import android.content.Intent;
   5:  import android.os.Binder;
   6:  import android.os.IBinder;
   7:  import android.util.Log;
   8:  
   9:  import androidx.annotation.Nullable;
  10:  
  11:  import java.io.BufferedReader;
  12:  import java.io.IOException;
  13:  import java.io.InputStream;
  14:  import java.io.InputStreamReader;
  15:  import java.net.HttpURLConnection;
  16:  import java.net.URL;
  17:  
  18:  public class HTTPService extends Service {
  19:      private static final String TAG = "HTTPService";
  20:  
  21:      public class LocalBinder extends Binder {
  22:          public HTTPService getService() {
  23:              return HTTPService.this;
  24:          }
  25:      }
  26:  
  27:      private final IBinder binder = new LocalBinder();
  28:  
  29:      @Nullable
  30:      @Override
  31:      public IBinder onBind(Intent intent) {
  32:          return binder;
  33:      }
  34:  
  35:      @Override
  36:      public int onStartCommand(Intent intent, int flags, int startId) {
  37:          return START_STICKY;
  38:      }
  39:  
  40:      public HTTPResult makeRequest(String urlString) {
  41:          HttpURLConnection urlConnection = null;
  42:          BufferedReader reader = null;
  43:          String jsonString = null;
  44:          boolean success = false;
  45:  
  46:          try {
  47:              URL url = new URL(urlString);
  48:              urlConnection = (HttpURLConnection) url.openConnection();
  49:              urlConnection.setRequestMethod("GET");
  50:              urlConnection.connect();
  51:  
  52:              InputStream inputStream = urlConnection.getInputStream();
  53:              if (inputStream == null) {
  54:                  return new HTTPResult(null, false);
  55:              }
  56:  
  57:              reader = new BufferedReader(new InputStreamReader(inputStream));
  58:              StringBuilder buffer = new StringBuilder();
  59:              String line;
  60:              while ((line = reader.readLine()) != null) {
  61:                  buffer.append(line).append("
  62:  ");
  63:              }
  64:  
  65:              if (buffer.length() == 0) {
  66:                  return new HTTPResult(null, false);
  67:              }jsonString = buffer.toString();
  68:              success = true;
  69:  
  70:          } catch (IOException e) {
  71:              Log.e(TAG, "Error making HTTP request", e);
  72:          } finally {
  73:              if (urlConnection != null) {
  74:                  urlConnection.disconnect();
  75:              }
  76:              if (reader != null) {
  77:                  try {
  78:                      reader.close();
  79:                  } catch (final IOException e) {
  80:                      Log.e(TAG, "Error closing stream", e);
  81:                  }
  82:              }
  83:          }
  84:  
  85:          return new HTTPResult(jsonString, success);
  86:      }
  87:  }
  88:  

Return to Simple HTTP service




AndroidMosaic context:



Comments ( )
<00>  <01>  <02>  <03>  <04>  <05>  <06>  <07>  <08>  <09>  <10>  <11>  <12>  <13>  <14>  <15>  <16>  <17>  <18>  <19>  <20>  <21>  <22>  <23>  <24>  <25
Link to this page: http://www.vb-net.com/AndroidMosaic/HTTPService.htm
<TAGS>  <ARTICLES>  <FRONT>  <CORE>  <MVC>  <ASP>  <NET>  <DATA>  <TASK>  <XML>  <KIOSK>  <NOTES>  <SQL>  <LINUX>  <MONO>  <FREEWARE>  <DOCS> <TRAVELS> <FLOWERS> <RESUME> < THANKS ME>