Android Open Source - openpizza-android R E S T Service






From Project

Back to project page openpizza-android.

License

The source code is released under:

MIT License

If you think the Android project openpizza-android 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 de.openpizza.android.service.restapi;
/* w w w .  j ava2s .  co m*/
import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;

/**
 * Abstakte Basisklasse fr die Implementierung von Service Schnittstellen
 * 
 * @author flops
 * 
 * @param <T>
 *            class of callback handler
 */
public abstract class RESTService<T> {

  private static final String API_URL = "http://openpizza.herokuapp.com/";
  protected RESTServiceHandler<T> serviceHandler;
  protected Context context;

  public RESTService(Context context) {
    this.context = context;
  }

  /**
   * Sende get request an den Server
   * 
   * @return Antwort des Servers bei Erfolg, sonst null
   */
  public String getData(String suburl) {
    HttpClient httpClient = null;
    try {
      // Create a new HttpClient and Post Header
      httpClient = new DefaultHttpClient();
      HttpGet httpGet = new HttpGet(API_URL + suburl);
      httpGet.addHeader("Accept", "application/json");
      HttpResponse response = httpClient.execute(httpGet);

      return EntityUtils.toString(response.getEntity());
    } catch (ClientProtocolException e) {
      // TODO Auto-generated catch block
    } catch (IOException e) {
      // TODO Auto-generated catch block
    } finally {
      if (httpClient != null) {
        httpClient.getConnectionManager().shutdown();
      }
    }
    return null;
  }

  /**
   * Sende post request an den Server
   * 
   * @param identifier
   * @param data
   * @return Antwort des Servers bei Erfolg, sonst null
   */
  public String postData(String identifier, String data) {
    // Create a new HttpClient and Post Header
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(API_URL + identifier);

    try {
      httpPost.addHeader("Accept", "application/json");
      httpPost.addHeader("Content-Type", "application/json");

      StringEntity entity = new StringEntity(data);
      httpPost.setEntity(entity);

      // Execute HTTP Post Request
      HttpResponse response = httpClient.execute(httpPost);
      return EntityUtils.toString(response.getEntity());
    } catch (ClientProtocolException e) {
      // TODO Auto-generated catch block
    } catch (IOException e) {
      // TODO Auto-generated catch block
    } finally {
      if (httpClient != null) {
        httpClient.getConnectionManager().shutdown();
      }
    }
    return null;
  }

  /**
   * Sende put request an den Server
   * 
   * @param identifier
   * @param data
   * @return Antwort des Servers bei Erfolg, sonst null
   */
  public String putData(String identifier, String data) {
    // Create a new HttpClient and Post Header
    HttpClient httpClient = new DefaultHttpClient();
    HttpPut httpPut = new HttpPut(API_URL + identifier);

    try {
      httpPut.addHeader("Accept", "application/json");
      httpPut.addHeader("Content-Type", "application/json");

      StringEntity entity = new StringEntity(data);
      httpPut.setEntity(entity);

      // Execute HTTP Post Request
      HttpResponse response = httpClient.execute(httpPut);
      return EntityUtils.toString(response.getEntity());
    } catch (ClientProtocolException e) {
      // TODO Auto-generated catch block
    } catch (IOException e) {
      // TODO Auto-generated catch block
    } finally {
      if (httpClient != null) {
        httpClient.getConnectionManager().shutdown();
      }
    }
    return null;
  }
}




Java Source Code List

de.openpizza.android.Category.java
de.openpizza.android.activitys.shopOverview.ShopOverviewActivity.java
de.openpizza.android.activitys.shopOverview.ShopOverviewCoordinator.java
de.openpizza.android.activitys.shopOverview.ShopOverviewFragment.java
de.openpizza.android.activitys.shopOverview.shopList.ShopListArrayAdapter.java
de.openpizza.android.activitys.shopOverview.shopList.ShopListItem.java
de.openpizza.android.activitys.shopOverview.shopList.ShopListView.java
de.openpizza.android.activitys.shopOverview.shopList.ShopList.java
de.openpizza.android.activitys.shop.ShopView.java
de.openpizza.android.ordermodul.CreateOrder.java
de.openpizza.android.ordermodul.DummyProvider.java
de.openpizza.android.ordermodul.ModelChangedListener.java
de.openpizza.android.ordermodul.NicknameHandler.java
de.openpizza.android.ordermodul.OrderBean.java
de.openpizza.android.ordermodul.OrderFacade.java
de.openpizza.android.ordermodul.OrderSyncAdapter.java
de.openpizza.android.ordermodul.Order.java
de.openpizza.android.ordermodul.SendOrder.java
de.openpizza.android.service.OrderContentService.java
de.openpizza.android.service.OrderService.java
de.openpizza.android.service.ShopIdService.java
de.openpizza.android.service.ShopsService.java
de.openpizza.android.service.data.DeliveryAddress.java
de.openpizza.android.service.data.OrderContentRequest.java
de.openpizza.android.service.data.OrderContentResponse.java
de.openpizza.android.service.data.OrderRequest.java
de.openpizza.android.service.data.OrderResponse.java
de.openpizza.android.service.data.Product.java
de.openpizza.android.service.data.Shop.java
de.openpizza.android.service.restapi.RESTServiceCall.java
de.openpizza.android.service.restapi.RESTServiceHandler.java
de.openpizza.android.service.restapi.RESTService.java
de.openpizza.android.views.LoginActivity.java
de.openpizza.android.views.OrderActivity.java
de.openpizza.android.views.ProductView.java
de.openpizza.android.views.SendOrderActivity.java
de.openpizza.android.views.ShopView.java
de.openpizza.android.views.antihost.LinkActivity.java
de.openpizza.android.views.antihost.OrderActivityAntihost.java
de.openpizza.android.views.antihost.ShopViewAntihost.java
de.openpizza.android.views.host.OrderActivityHost.java
de.openpizza.android.views.host.ShopViewHostEdit.java
de.openpizza.android.views.host.ShopViewHost.java
de.openpizza.android.views.shopview.CategoryFragment.java
de.openpizza.android.views.shopview.ShopViewTabsPagerAdapter.java
de.openpizza.android.views.shopview.ShowViewFragment.java