Android Open Source - garageoPIner-androidApp Garageo P Iner H T T P Client






From Project

Back to project page garageoPIner-androidApp.

License

The source code is released under:

Apache License

If you think the Android project garageoPIner-androidApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.wirthual.garageopiner.communication;
/*w w w.ja v  a2s  . co  m*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import android.util.Base64;

public class GarageoPInerHTTPClient {
  public final static int DEFAULT_PORT = 8000;
  private int TIMEOUT = 500;
  
  protected String urlBase;
  protected String auth;
  
  public GarageoPInerHTTPClient(String host) {
    this.urlBase = "http://" + host + ":" + String.valueOf(DEFAULT_PORT);
  }

  public GarageoPInerHTTPClient(String host, int port) {
    this.urlBase = "http://" + host + ":" + String.valueOf(port);
  }

  public String sendRequest(String method, String path) throws Exception {
    BufferedReader reader = null;
    try {
      URL url = new URL(this.urlBase + path);
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setRequestMethod(method);
      connection.setConnectTimeout(TIMEOUT);
      if (this.auth != null) {
        connection.setRequestProperty("Authorization", this.auth);
      }


      // read the output from the server
      reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      StringBuilder stringBuilder = new StringBuilder();

      String line = null;
      while ((line = reader.readLine()) != null) {
        stringBuilder.append(line).append('\n');
      }
      return stringBuilder.toString();
    } catch (Exception e) {
      e.printStackTrace();
      throw e;
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException ioe) {
          ioe.printStackTrace();
        }
      }
    }
  }
  
  public void setCredentials(String login, String password){
    String s = login + ":" + password;
    this.auth = "Basic " + Base64.encodeToString(s.getBytes(),Base64.DEFAULT);
  }

}




Java Source Code List

com.wirthual.garageopiner.activities.MainActivity.java
com.wirthual.garageopiner.activities.SettingsActivity.java
com.wirthual.garageopiner.activities.TimeTableActivity.java
com.wirthual.garageopiner.communication.CommunicationService.java
com.wirthual.garageopiner.communication.DisplayToast.java
com.wirthual.garageopiner.communication.GarageoPInerHTTPClient.java
com.wirthual.garageopiner.fragments.MainFragment.java
com.wirthual.garageopiner.fragments.TimeTableFragment.java
com.wirthual.garageopiner.utils.ExpandableListAdapter.java
com.wirthual.garageopiner.utils.MyTimeTriggerWidgetProvider.java
com.wirthual.garageopiner.utils.MyTriggerWidgetProvider.java
com.wirthual.garageopiner.utils.NotificationUpdateRunnable.java
com.wirthual.garageopiner.utils.WifiChangedReceiver.java