hamzaalloush

Service_HTTP_Get.java

May 1st, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.96 KB | None | 0 0
  1.     package com.example.hassansbeity.testingsevices;
  2.  
  3.     import android.app.IntentService;
  4.     import android.content.Intent;
  5.     import android.support.annotation.Nullable;
  6.     import android.util.Log;
  7.  
  8.     import java.io.IOException;
  9.     import java.io.InputStream;
  10.     import java.io.InputStreamReader;
  11.     import java.net.URL;
  12.  
  13.     import javax.net.ssl.HttpsURLConnection;
  14.  
  15.     /**
  16.      * Created by hassansbeity on 4/23/17.
  17.      * Service_HTTP_Get: HTTP GET request
  18.      */
  19.  
  20.     public class Service_HTTP_Get extends IntentService {
  21.  
  22.         public static final String CONSTANT_COM="com.Sbeyti.GET";
  23.         public Service_HTTP_Get(){super("Service_HTTP_Get");}
  24.  
  25.         @Override
  26.         protected void onHandleIntent(@Nullable Intent intent) {
  27.             Log.v("ServiceStatus","Service Started");
  28.  
  29.             //final String message= intent.getStringExtra("data");
  30.             Thread thread = new Thread(new Runnable() {
  31.                 public void run() {
  32.                     // do the work that the service needs to do
  33.                     String result=doInBackground();
  34.                     Intent backIntent= new Intent();
  35.                     backIntent.putExtra("data",result);
  36.                     backIntent.setAction(CONSTANT_COM);
  37.                     sendBroadcast(backIntent);
  38.                 }
  39.             });
  40.             thread.start();
  41.         }
  42.         public String doInBackground() {
  43.              InputStream stream = null;
  44.              HttpsURLConnection connection = null;
  45.              String result = null;URL url=null;
  46.             try {
  47.                 url = new URL("https://www.google.com");
  48.                 Log.v("ServiceStatus","Trying to Connect");
  49.                  connection = (HttpsURLConnection) url.openConnection();
  50.                  // Timeout for reading InputStream arbitrarily set to 3000ms.
  51.                  connection.setReadTimeout(3000);
  52.                 // Timeout for connection.connect() arbitrarily set to 3000ms.
  53.                  connection.setConnectTimeout(3000);
  54.                  // For this use case, set HTTP method to GET.
  55.                  connection.setRequestMethod("GET");
  56.                  // Already true by default but setting just in case; needs to be true since this request
  57.                 // is carrying an input (response) body.connection.setDoInput(true);
  58.                 // Open communications link (network traffic occurs here).
  59.                 connection.connect();
  60.                 Log.v("ServiceStatus","Connected");
  61.                 int responseCode = connection.getResponseCode();
  62.                 if (responseCode != HttpsURLConnection.HTTP_OK) {
  63.                  throw new IOException("HTTP error code: " + responseCode);
  64.                  }
  65.             // Retrieve the response body as an InputStream.
  66.               stream = connection.getInputStream();
  67.                 Log.v("ServiceStatus","Get STream");
  68.              if (stream != null) {
  69.             // Converts Stream to String with max length of 500.
  70.              result = readStream(stream, 500);
  71.              }
  72.             } catch (Exception e) {
  73.                 Log.v("ServiceStatus","Error downloading :"+e.getMessage());
  74.  
  75.             }
  76.               finally {
  77.                 // Close Stream and disconnect HTTPS connection.
  78.                 if (stream != null) {
  79.             try {
  80.                 stream.close();
  81.                } catch (Exception e) {
  82.                     Log.v("ServiceStatus","Error downloading :"+e.getMessage());
  83.  
  84.                 }
  85.                 }
  86.                 if (connection != null) {
  87.                     connection.disconnect();
  88.                 }
  89.             }
  90.             return result;
  91.         }
  92.              // covert stream into String
  93.             private String readStream(InputStream stream, int maxLength) throws IOException {
  94.             String result = null;
  95.             // Read InputStream using the UTF-8 charset.
  96.             InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
  97.             // Create temporary buffer to hold Stream data with specified max length.
  98.             char[] buffer = new char[maxLength];
  99.             // Populate temporary buffer with Stream data.
  100.             int numChars = 0;
  101.             int readSize = 0;
  102.             while (numChars < maxLength && readSize != -1) {
  103.                 numChars += readSize;
  104.                 int pct = (100 * numChars) / maxLength;
  105.                 //publishProgress(DownloadCallback.Progress.PROCESS_INPUT_STREAM_IN_PROGRESS, pct);
  106.                 readSize = reader.read(buffer, numChars, buffer.length - numChars);
  107.             }
  108.             if (numChars != -1) {
  109.                 // The stream was not empty.
  110.                 // Create String that is actual length of response body if actual length was less than
  111.                 // max length.
  112.                 numChars = Math.min(numChars, maxLength);
  113.                 result = new String(buffer, 0, numChars);
  114.             }
  115.             return result;
  116.         }
  117.     }
Add Comment
Please, Sign In to add comment