Android Open Source - httpbreeze Raw Http Request Executor






From Project

Back to project page httpbreeze.

License

The source code is released under:

GNU General Public License

If you think the Android project httpbreeze 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.http.breeze.rest;
//  w w  w . j  a va2 s .c  o m
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Set;

import android.util.Log;

public class RawHttpRequestExecutor extends HttpRequestExecutorTemplate{
  private static final String TAG = RawHttpRequestExecutor.class.getName();

  protected RawHttpRequestExecutor(HttpRequest httpRequest) {
    super(httpRequest);
  }
  
  public static RawHttpRequestExecutor newInstance(HttpRequest httpRequest){
    return new RawHttpRequestExecutor(httpRequest);
  }

  @Override
  public String executeAsGetRequest() throws MalformedURLException, UnsupportedEncodingException,IOException {
    String encodedUrl = this.httpRequest.getEncodedUrl();
    URL requestUrl = new URL(encodedUrl);
    Log.d(TAG, "Making a GET request to : " + encodedUrl);
    HttpURLConnection httpURLConnection = (HttpURLConnection) requestUrl.openConnection();
    httpURLConnection.setRequestMethod(HttpMethod.GET.getDescription());
    
    Set<String> requestHeadersKeys = this.httpRequest.getHeaders().keySet();
    
    for(String headerKey : requestHeadersKeys){
      httpURLConnection.addRequestProperty(headerKey, this.httpRequest.getHeaders().get(headerKey));
    }
    
    BufferedReader responseReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
    if(responseReader != null){
      StringBuilder httpResponse = new StringBuilder();
      String readLine = null;
      while((readLine = responseReader.readLine()) != null){
        httpResponse.append(readLine);
      }
      Log.d(TAG, "Response from GET request to : " + encodedUrl + " is : " + httpResponse.toString());
      return httpResponse.toString();
    }
    return null;
  }

  @Override
  public String executeAsPostRequest() throws MalformedURLException, UnsupportedEncodingException,IOException{
    URL requestUrl = new URL(this.httpRequest.getBaseUrl());
    Log.d(TAG, "Making a POST request to : " + this.httpRequest.getBaseUrl());
    HttpURLConnection httpURLConnection = (HttpURLConnection) requestUrl.openConnection();
    httpURLConnection.setRequestMethod(HttpMethod.POST.getDescription());
    
    
    Set<String> requestHeaders = this.httpRequest.getHeaders().keySet();
    
    for(String headerKey : requestHeaders){
      httpURLConnection.addRequestProperty(headerKey, this.httpRequest.getHeaders().get(headerKey));
    }
    
    String requestParameters = this.httpRequest.getEncodedParameters();
    Log.d(TAG, "Request parameters : " + requestParameters);
    
    httpURLConnection.setDoOutput(true);
    DataOutputStream writer = new DataOutputStream(httpURLConnection.getOutputStream());
    writer.writeBytes(requestParameters);
    writer.flush();
    writer.close();
    
    BufferedReader responseReader = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream()));
    if(responseReader != null){
      StringBuilder httpResponse = new StringBuilder();
      String readLine = null;
      while((readLine = responseReader.readLine()) != null){
        httpResponse.append(readLine);
      }
      Log.d(TAG, "Response from POST request to : " + this.httpRequest.getEncodedUrl() + " is : " + httpResponse.toString());
      return httpResponse.toString();
    }
    return null;
  }

}




Java Source Code List

com.http.breeze.rest.HttpMethod.java
com.http.breeze.rest.HttpRequestExecutorTemplate.java
com.http.breeze.rest.HttpRequestExecutorType.java
com.http.breeze.rest.HttpRequestExecutor.java
com.http.breeze.rest.HttpRequest.java
com.http.breeze.rest.HttpResponseConverter.java
com.http.breeze.rest.JsonResponseConverter.java
com.http.breeze.rest.RawHttpRequestExecutor.java