Android 發送HTTP GET POST 請求以及通過 MultipartEntityBuilder 上傳文件(二)
Android 發送HTTP GET POST 請求以及通過 MultipartEntityBuilder 上傳文件第二版
上次粗略的寫了相同功能的代碼,這次整理修複了之前的一些BUG,結構也大量修改過了,現在應用更加方便點
https://blog.csdn.net/zhouzme/article/details/18940279
直接上代碼了:
ZHttpRequset.java
package com.ai9475.util;
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.HttpRequestBase;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.protocol.HTTP;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
/**
* Created by ZHOUZ on 14-2-3.
*/
public class ZHttpRequest
{
public final String HTTP_GET = "GET";
public final String HTTP_POST = "POST";
/**
* 當前請求的 URL
*/
protected String url = "";
/**
* HTTP 請求的類型
*/
protected String requsetType = HTTP_GET;
/**
* 連接請求的超時時間
*/
protected int connectionTimeout = 5000;
/**
* 讀取遠程數據的超時時間
*/
protected int soTimeout = 10000;
/**
* 服務端返回的狀態碼
*/
protected int statusCode = -1;
/**
* 當前鏈接的字符編碼
*/
protected String charset = HTTP.UTF_8;
/**
* HTTP GET 請求管理器
*/
protected HttpRequestBase httpRequest= null;
/**
* HTTP 請求的配置參數
*/
protected HttpParams httpParameters= null;
/**
* HTTP 請求響應
*/
protected HttpResponse httpResponse= null;
/**
* HTTP 客戶端連接管理器
*/
protected HttpClient httpClient= null;
/**
* HTTP POST 方式發送多段數據管理器
*/
protected MultipartEntityBuilder multipartEntityBuilder= null;
/**
* 綁定 HTTP 請求的事件監聽器
*/
protected OnHttpRequestListener onHttpRequestListener = null;
public ZHttpRequest(){}
public ZHttpRequest(OnHttpRequestListener listener) {
this.setOnHttpRequestListener(listener);
}
/**
* 設置當前請求的鏈接
*
* @param url
* @return
*/
public ZHttpRequest setUrl(String url)
{
this.url = url;
return this;
}
/**
* 設置連接超時時間
*
* @param timeout 單位(毫秒),默認 5000
* @return
*/
public ZHttpRequest setConnectionTimeout(int timeout)
{
this.connectionTimeout = timeout;
return this;
}
/**
* 設置 socket 讀取超時時間
*
* @param timeout 單位(毫秒),默認 10000
* @return
*/
public ZHttpRequest setSoTimeout(int timeout)
{
this.soTimeout = timeout;
return this;
}
/**
* 設置獲取內容的編碼格式
*
* @param charset 默認為 UTF-8
* @return
*/
public ZHttpRequest setCharset(String charset)
{
this.charset = charset;
return this;
}
/**
* 獲取當前 HTTP 請求的類型
*
* @return
*/
public String getRequestType()
{
return this.requsetType;
}
/**
* 判斷當前是否 HTTP GET 請求
*
* @return
*/
public boolean isGet()
{
return this.requsetType == HTTP_GET;
}
/**
* 判斷當前是否 HTTP POST 請求
*
* @return
*/
public boolean isPost()
{
return this.requsetType == HTTP_POST;
}
/**
* 獲取 HTTP 請求響應信息
*
* @return
*/
public HttpResponse getHttpResponse()
{
return this.httpResponse;
}
/**
* 獲取 HTTP 客戶端連接管理器
*
* @return
*/
public HttpClient getHttpClient()
{
return this.httpClient;
}
/**
* 添加一條 HTTP 請求的 header 信息
*
* @param name
* @param value
* @return
*/
public ZHttpRequest addHeader(String name, String value)
{
this.httpRequest.addHeader(name, value);
return this;
}
/**
* 獲取 HTTP GET 控製器
*
* @return
*/
public HttpGet getHttpGet()
{
return (HttpGet) this.httpRequest;
}
/**
* 獲取 HTTP POST 控製器
*
* @return
*/
public HttpPost getHttpPost()
{
return (HttpPost) this.httpRequest;
}
/**
* 獲取請求的狀態碼
*
* @return
*/
public int getStatusCode()
{
return this.statusCode;
}
/**
* 通過 GET 方式請求數據
*
* @param url
* @return
* @throws IOException
*/
public String get(String url) throws Exception
{
this.requsetType = HTTP_GET;
// 設置當前請求的鏈接
this.setUrl(url);
// 新建 HTTP GET 請求
this.httpRequest = new HttpGet(this.url);
// 執行客戶端請求
this.httpClientExecute();
// 監聽服務端響應事件並返回服務端內容
return this.checkStatus();
}
/**
* 獲取 HTTP POST 多段數據提交管理器
*
* @return
*/
public MultipartEntityBuilder getMultipartEntityBuilder()
{
if (this.multipartEntityBuilder == null) {
this.multipartEntityBuilder = MultipartEntityBuilder.create();
// 設置為瀏覽器兼容模式
multipartEntityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
// 設置請求的編碼格式
multipartEntityBuilder.setCharset(Charset.forName(this.charset));
}
return this.multipartEntityBuilder;
}
/**
* 配置完要 POST 提交的數據後, 執行該方法生成數據實體等待發送
*/
public void buildPostEntity()
{
// 生成 HTTP POST 實體
HttpEntity httpEntity = this.multipartEntityBuilder.build();
this.getHttpPost().setEntity(httpEntity);
}
/**
* 發送 POST 請求
*
* @param url
* @return
* @throws Exception
*/
public String post(String url) throws Exception
{
this.requsetType = HTTP_POST;
// 設置當前請求的鏈接
this.setUrl(url);
// 新建 HTTP POST 請求
this.httpRequest = new HttpPost(this.url);
// 執行客戶端請求
this.httpClientExecute();
// 監聽服務端響應事件並返回服務端內容
return this.checkStatus();
}
/**
* 執行 HTTP 請求
*
* @throws Exception
*/
protected void httpClientExecute() throws Exception
{
// 配置 HTTP 請求參數
this.httpParameters = new BasicHttpParams();
this.httpParameters.setParameter("charset", this.charset);
// 設置 連接請求超時時間
HttpConnectionParams.setConnectionTimeout(this.httpParameters, this.connectionTimeout);
// 設置 socket 讀取超時時間
HttpConnectionParams.setSoTimeout(this.httpParameters, this.soTimeout);
// 開啟一個客戶端 HTTP 請求
this.httpClient = new DefaultHttpClient(this.httpParameters);
// 啟動 HTTP POST 請求執行前的事件監聽回調操作(如: 自定義提交的數據字段或上傳的文件等)
this.getOnHttpRequestListener().onRequest(this);
// 發送 HTTP 請求並獲取服務端響應狀態
this.httpResponse = this.httpClient.execute(this.httpRequest);
// 獲取請求返回的狀態碼
this.statusCode = this.httpResponse.getStatusLine().getStatusCode();
}
/**
* 讀取服務端返回的輸入流並轉換成字符串返回
*
* @throws Exception
*/
public String getInputStream() throws Exception
{
// 接收遠程輸入流
InputStream inStream = this.httpResponse.getEntity().getContent();
// 分段讀取輸入流數據
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = -1;
while ((len = inStream.read(buf)) != -1) {
baos.write(buf, 0, len);
}
// 數據接收完畢退出
inStream.close();
// 將數據轉換為字符串保存
return new String(baos.toByteArray(), this.charset);
}
/**
* 關閉連接管理器釋放資源
*/
protected void shutdownHttpClient()
{
if (this.httpClient != null && this.httpClient.getConnectionManager() != null) {
this.httpClient.getConnectionManager().shutdown();
}
}
/**
* 監聽服務端響應事件並返回服務端內容
*
* @return
* @throws Exception
*/
protected String checkStatus() throws Exception
{
OnHttpRequestListener listener = this.getOnHttpRequestListener();
String content;
if (this.statusCode == HttpStatus.SC_OK) {
// 請求成功, 回調監聽事件
content = listener.onSucceed(this.statusCode, this);
} else {
// 請求失敗或其他, 回調監聽事件
content = listener.onFailed(this.statusCode, this);
}
// 關閉連接管理器釋放資源
this.shutdownHttpClient();
return content;
}
/**
* HTTP 請求操作時的事件監聽接口
*/
public interface OnHttpRequestListener
{
/**
* 初始化 HTTP GET 或 POST 請求之前的 header 信息配置 或 其他數據配置等操作
*
* @param request
* @throws Exception
*/
public void onRequest(ZHttpRequest request) throws Exception;
/**
* 當 HTTP 請求響應成功時的回調方法
*
* @param statusCode 當前狀態碼
* @param request
* @return 返回請求獲得的字符串內容
* @throws Exception
*/
public String onSucceed(int statusCode, ZHttpRequest request) throws Exception;
/**
* 當 HTTP 請求響應失敗時的回調方法
*
* @param statusCode 當前狀態碼
* @param request
* @return 返回請求失敗的提示內容
* @throws Exception
*/
public String onFailed(int statusCode, ZHttpRequest request) throws Exception;
}
/**
* 綁定 HTTP 請求的監聽事件
*
* @param listener
* @return
*/
public ZHttpRequest setOnHttpRequestListener(OnHttpRequestListener listener)
{
this.onHttpRequestListener = listener;
return this;
}
/**
* 獲取已綁定過的 HTTP 請求監聽事件
*
* @return
*/
public OnHttpRequestListener getOnHttpRequestListener()
{
return this.onHttpRequestListener;
}
}
在 Activity 中的使用方法(這裏我還是隻寫主體部分代碼):
MainActivity.java
public void doClick(View view)
{
ZHttpRequest get = new ZHttpRequest();
get
.setCharset(HTTP.UTF_8)
.setConnectionTimeout(5000)
.setSoTimeout(5000);
get.setOnHttpRequestListener(new ZHttpRequest.OnHttpRequestListener() {
@Override
public void onRequest(ZHttpRequest request) throws Exception {
}
@Override
public String onSucceed(int statusCode, ZHttpRequest request) throws Exception {
return request.getInputStream();
}
@Override
public String onFailed(int statusCode, ZHttpRequest request) throws Exception {
return "GET 請求失敗:statusCode "+ statusCode;
}
});
ZHttpRequest post = new ZHttpRequest();
post
.setCharset(HTTP.UTF_8)
.setConnectionTimeout(5000)
.setSoTimeout(10000);
post.setOnHttpRequestListener(new ZHttpRequest.OnHttpRequestListener() {
private String CHARSET = HTTP.UTF_8;
private ContentType TEXT_PLAIN = ContentType.create("text/plain", Charset.forName(CHARSET));
@Override
public void onRequest(ZHttpRequest request) throws Exception {
// 設置發送請求的 header 信息
request.addHeader("cookie", "abc=123;456=愛就是幸福;");
// 配置要 POST 的數據
MultipartEntityBuilder builder = request.getMultipartEntityBuilder();
builder.addTextBody("p1", "abc");
builder.addTextBody("p2", "中文", TEXT_PLAIN);
builder.addTextBody("p3", "abc中文cba", TEXT_PLAIN);
if (picPath != null && ! "".equals(picPath)) {
builder.addTextBody("pic", picPath);
builder.addBinaryBody("file", new File(picPath));
}
request.buildPostEntity();
}
@Override
public String onSucceed(int statusCode, ZHttpRequest request) throws Exception {
return request.getInputStream();
}
@Override
public String onFailed(int statusCode, ZHttpRequest request) throws Exception {
return "POST 請求失敗:statusCode "+ statusCode;
}
});
TextView textView = (TextView) findViewById(R.id.showContent);
String content = "初始內容";
try {
if (view.getId() == R.id.doGet) {
content = get.get("https://www.baidu.com");
content = "GET數據:isGet: " + (get.isGet() ? "yes" : "no") + " =>" + content;
} else {
content = post.post("https://192.168.1.6/test.php");
content = "POST數據:isPost" + (post.isPost() ? "yes" : "no") + " =>" + content;
}
} catch (IOException e) {
content = "IO異常:" + e.getMessage();
} catch (Exception e) {
content = "異常:" + e.getMessage();
}
textView.setText(content);
}
其中 picPath 為 SD 卡中的圖片路徑 String 類型,我是直接拍照後進行上傳用的
關於拍照顯示並上傳的代碼部分:https://blog.csdn.net/zhouzme/article/details/18952201
布局頁麵
activity_main.xml
<LinearLayout xmlns:andro
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<Button
android:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginBottom="10dp"
android:text="GET請求"
android:onClick="doClick"
/>
<Button
android:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginBottom="10dp"
android:text="POST請求"
android:onClick="doClick"
/>
<Button
android:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:layout_marginBottom="10dp"
android:text="拍照"
android:onClick="doPhoto"
/>
<TextView
android:
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
/>
<ImageView
android:
android:layout_width="fill_parent"
android:layout_height="250dp"
android:scaleType="centerCrop"
android:src="@drawable/add"
android:layout_marginBottom="10dp"
/>
</LinearLayout>
</ScrollView>
</LinearLayout>
至於服務端我用的 PHP ,隻是簡單的輸出獲取到的數據而已
<?php
echo 'GET:<br>'. "\n";
//print_r(array_map('urldecode', $_GET));
print_r($_GET);
echo '<br>'. "\n". 'POST:<br>'. "\n";
//print_r(array_map('urldecode', $_POST));
print_r($_POST);
echo '<br>'. "\n". 'FILES:<br>'. "\n";
print_r($_FILES);
echo '<br>'. "\n". 'COOKIES:<br>'. "\n";
print_r($_COOKIE);
最後更新:2017-04-03 12:54:58