Android Open Source - android-json-http Http Request






From Project

Back to project page android-json-http.

License

The source code is released under:

Apache License

If you think the Android project android-json-http 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.open.jsonhttp;
//  ww  w.  j a v a 2 s . c o m
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
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.HttpUriRequest;
import org.apache.http.conn.ConnectTimeoutException;
import org.apache.http.entity.ContentProducer;
import org.apache.http.entity.EntityTemplate;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.os.Message;
import android.util.Log;

/**
 * HTTP?????
 * 
 * @author yinghui.hong
 */
public class HttpRequest {

  private static final String TAG = HttpRequest.class.getSimpleName();

  private static final int TIMEOUT = 3000;

  private static final String ENCODEING = HTTP.UTF_8;

  /**
   * ?GET?????
   * 
   * @param url
   *            ????????
   * @return Message??
   */
  public static Message doGet(String url) {
    Log.i(TAG, "GET:REQUEST URL IS --- " + url);
    HttpGet httpGet = new HttpGet(url);
    return executeRequest(httpGet);
  }

  /**
   * ?POST???????????
   * 
   * @param url
   *            ????????
   * @param file
   *            ?????
   * @return Message??
   */
  public static Message doPost(String url, final File file) {
    Log.i(TAG, "POST:REQUEST URL IS --- " + url);
    HttpPost httpPost = new HttpPost(url);
    FileEntity entity = new FileEntity(file, "binary/octet-stream");
    httpPost.setEntity(entity);
    return executeRequest(httpPost);
  }

  /**
   * ?POST????????JSON??
   * 
   * @param url
   *            ????????
   * @param requestBody
   *            ????????????? JSON?
   * @return Message??
   */
  public static Message doPost(String url, final String requestBody) {
    Log.i(TAG, "POST:REQUEST URL IS --- " + url);
    HttpPost httpPost = new HttpPost(url);
    ContentProducer contentProducer = new ContentProducer() {
      public void writeTo(OutputStream outstream) throws IOException {
        Writer writer = new OutputStreamWriter(outstream, ENCODEING);
        writer.write(requestBody);
        writer.flush();
      }
    };
    HttpEntity entity = new EntityTemplate(contentProducer);
    httpPost.setEntity(entity);
    return executeRequest(httpPost);
  }

  /**
   * ????
   * 
   * @param httpUrlRequest
   *            the request to execute
   * @return Message ??
   */
  private static Message executeRequest(HttpUriRequest httpUrlRequest) {

    HttpClient httpClient = new DefaultHttpClient();
    Message message = Message.obtain();
    try {
      httpClient.getParams().setParameter(
          CoreConnectionPNames.CONNECTION_TIMEOUT, TIMEOUT);
      httpClient.getParams().setParameter(
          CoreConnectionPNames.SO_TIMEOUT, TIMEOUT);
      HttpResponse httpResponse = httpClient.execute(httpUrlRequest);
      int responseCode = httpResponse.getStatusLine().getStatusCode();
      HttpEntity httpEntity = httpResponse.getEntity();
      message.arg1 = responseCode;
      if (responseCode == HttpStatus.SC_OK) {
        message.obj = EntityUtils.toString(httpEntity, ENCODEING);
      }
    } catch (ConnectTimeoutException e) {
      // ????????????
      Log.e(TAG,
          "******************ConnectTimeoutException****************\n"
              + e.toString());
      message.arg1 = HttpResCode.TIME_OUT;
    } catch (Exception e) {
      // IOException/ParseException/ClientProtocolException??????????
      Log.e(TAG, "*********************ServerError ******************\n"
          + e.toString());
      message.arg1 = HttpResCode.SERVER_ERROR;
    } finally {
      // ??????????
      httpClient.getConnectionManager().shutdown();
    }
    return message;
  }

  /**
   * GET????????????
   * 
   * @param uri
   * @return
   * @throws ConnectTimeoutException
   * @throws IOException
   */
  public static InputStream getInputStream(String uri)
      throws ConnectTimeoutException, IOException {
    InputStream inputStream = null;
    HttpClient httpClient = new DefaultHttpClient();
    httpClient.getParams().setParameter(
        CoreConnectionPNames.CONNECTION_TIMEOUT, TIMEOUT);
    httpClient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,
        TIMEOUT);
    HttpGet httpGet = new HttpGet(uri);
    HttpResponse httpResponse = httpClient.execute(httpGet);
    int responseCode = httpResponse.getStatusLine().getStatusCode();
    HttpEntity httpEntity = httpResponse.getEntity();
    if (responseCode == HttpStatus.SC_OK) {
      inputStream = httpEntity.getContent();
    }
    return inputStream;
  }
}




Java Source Code List

com.example.App.java
com.example.MainActivity.java
com.example.constant.Request.java
com.example.constant.Result.java
com.example.constant.Url.java
com.example.request.GeocodeReqBean.java
com.example.response.GeocodeResBean.java
com.example.response.table.AddressComponent.java
com.example.response.table.GeocodeTable.java
com.example.response.table.Geometry.java
com.example.response.table.LocationRectangle.java
com.example.response.table.Location.java
com.example.util.AccessApiUtil.java
com.open.jsonhttp.AsyncTask.java
com.open.jsonhttp.HttpRequest.java
com.open.jsonhttp.HttpResCode.java
com.open.jsonhttp.OnRequestHttpListener.java
com.open.jsonhttp.RequestBeanFile.java
com.open.jsonhttp.RequestBean.java
com.open.jsonhttp.ResponseBean.java
com.open.jsonhttp.WebServiceListener.java
com.open.jsonhttp.util.NetwrokUtil.java
com.open.jsonhttp.util.ParseUtil.java
com.open.jsonhttp.util.RequestUtil.java
com.open.jsonhttp.util.StringUtil.java
com.open.jsonhttp.util.ThreadUtil.java