Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.hassansbeity.testingsevices;
- import android.app.IntentService;
- import android.content.Intent;
- import android.support.annotation.Nullable;
- import android.util.Log;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.net.URL;
- import javax.net.ssl.HttpsURLConnection;
- /**
- * Created by hassansbeity on 4/23/17.
- * Service_HTTP_Get: HTTP GET request
- */
- public class Service_HTTP_Get extends IntentService {
- public static final String CONSTANT_COM="com.Sbeyti.GET";
- public Service_HTTP_Get(){super("Service_HTTP_Get");}
- @Override
- protected void onHandleIntent(@Nullable Intent intent) {
- Log.v("ServiceStatus","Service Started");
- //final String message= intent.getStringExtra("data");
- Thread thread = new Thread(new Runnable() {
- public void run() {
- // do the work that the service needs to do
- String result=doInBackground();
- Intent backIntent= new Intent();
- backIntent.putExtra("data",result);
- backIntent.setAction(CONSTANT_COM);
- sendBroadcast(backIntent);
- }
- });
- thread.start();
- }
- public String doInBackground() {
- InputStream stream = null;
- HttpsURLConnection connection = null;
- String result = null;URL url=null;
- try {
- url = new URL("https://www.google.com");
- Log.v("ServiceStatus","Trying to Connect");
- connection = (HttpsURLConnection) url.openConnection();
- // Timeout for reading InputStream arbitrarily set to 3000ms.
- connection.setReadTimeout(3000);
- // Timeout for connection.connect() arbitrarily set to 3000ms.
- connection.setConnectTimeout(3000);
- // For this use case, set HTTP method to GET.
- connection.setRequestMethod("GET");
- // Already true by default but setting just in case; needs to be true since this request
- // is carrying an input (response) body.connection.setDoInput(true);
- // Open communications link (network traffic occurs here).
- connection.connect();
- Log.v("ServiceStatus","Connected");
- int responseCode = connection.getResponseCode();
- if (responseCode != HttpsURLConnection.HTTP_OK) {
- throw new IOException("HTTP error code: " + responseCode);
- }
- // Retrieve the response body as an InputStream.
- stream = connection.getInputStream();
- Log.v("ServiceStatus","Get STream");
- if (stream != null) {
- // Converts Stream to String with max length of 500.
- result = readStream(stream, 500);
- }
- } catch (Exception e) {
- Log.v("ServiceStatus","Error downloading :"+e.getMessage());
- }
- finally {
- // Close Stream and disconnect HTTPS connection.
- if (stream != null) {
- try {
- stream.close();
- } catch (Exception e) {
- Log.v("ServiceStatus","Error downloading :"+e.getMessage());
- }
- }
- if (connection != null) {
- connection.disconnect();
- }
- }
- return result;
- }
- // covert stream into String
- private String readStream(InputStream stream, int maxLength) throws IOException {
- String result = null;
- // Read InputStream using the UTF-8 charset.
- InputStreamReader reader = new InputStreamReader(stream, "UTF-8");
- // Create temporary buffer to hold Stream data with specified max length.
- char[] buffer = new char[maxLength];
- // Populate temporary buffer with Stream data.
- int numChars = 0;
- int readSize = 0;
- while (numChars < maxLength && readSize != -1) {
- numChars += readSize;
- int pct = (100 * numChars) / maxLength;
- //publishProgress(DownloadCallback.Progress.PROCESS_INPUT_STREAM_IN_PROGRESS, pct);
- readSize = reader.read(buffer, numChars, buffer.length - numChars);
- }
- if (numChars != -1) {
- // The stream was not empty.
- // Create String that is actual length of response body if actual length was less than
- // max length.
- numChars = Math.min(numChars, maxLength);
- result = new String(buffer, 0, numChars);
- }
- return result;
- }
- }
Add Comment
Please, Sign In to add comment