package com.mobileclient.util;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import com.mobileclient.activity.photoListActivity;
import com.mobileclient.app.Declare;
public class HttpUtil {
// 锟斤拷锟斤拷URL
public static final String BASE_URL = "http://192.168.1.2:8080/JavaWebProject/";
public static final String FILE_PATH = Environment.getExternalStorageDirectory().getAbsolutePath() + "/mobileclient";
public static final String Cach_Dir = Declare.context.getCacheDir().getAbsolutePath();
// 锟斤拷锟紾et锟斤拷锟斤拷锟斤拷锟絩equest
public static HttpGet getHttpGet(String url) {
HttpGet request = new HttpGet(url);
return request;
}
// 锟斤拷锟絇ost锟斤拷锟斤拷锟斤拷锟絩equest
public static HttpPost getHttpPost(String url) {
HttpPost request = new HttpPost(url);
return request;
}
// 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷应锟斤拷锟斤拷response
public static HttpResponse getHttpResponse(HttpGet request)
throws ClientProtocolException, IOException {
HttpResponse response = new DefaultHttpClient().execute(request);
return response;
}
// 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷应锟斤拷锟斤拷response
public static HttpResponse getHttpResponse(HttpPost request)
throws ClientProtocolException, IOException {
HttpResponse response = new DefaultHttpClient().execute(request);
return response;
}
// 锟斤拷锟斤拷Post锟斤拷锟襟,伙拷锟斤拷锟接︼拷锟窖拷锟斤拷
public static String queryStringForPost(String url) {
// 锟斤拷锟斤拷url锟斤拷锟紿ttpPost锟斤拷锟斤拷
HttpPost request = HttpUtil.getHttpPost(url);
String result = null;
try {
// 锟斤拷锟斤拷锟接︼拷锟斤拷锟�
HttpResponse response = HttpUtil.getHttpResponse(request);
// 锟叫讹拷锟角凤拷锟斤拷锟斤拷晒锟�
if (response.getStatusLine().getStatusCode() == 200) {
// 锟斤拷锟斤拷锟接�
result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "锟斤拷锟斤拷锟届常锟斤拷";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "锟斤拷锟斤拷锟届常锟斤拷";
return result;
}
return null;
}
// 锟斤拷锟斤拷锟接︼拷锟窖拷锟斤拷
public static String queryStringForPost(HttpPost request) {
String result = null;
try {
// 锟斤拷锟斤拷锟接︼拷锟斤拷锟�
HttpResponse response = HttpUtil.getHttpResponse(request);
// 锟叫讹拷锟角凤拷锟斤拷锟斤拷晒锟�
if (response.getStatusLine().getStatusCode() == 200) {
// 锟斤拷锟斤拷锟接�
result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "锟斤拷锟斤拷锟届常锟斤拷";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "锟斤拷锟斤拷锟届常锟斤拷";
return result;
}
return null;
}
// 锟斤拷锟斤拷Get锟斤拷锟襟,伙拷锟斤拷锟接︼拷锟窖拷锟斤拷
public static String queryStringForGet(String url) {
// 锟斤拷锟紿ttpGet锟斤拷锟斤拷
HttpGet request = HttpUtil.getHttpGet(url);
String result = null;
try {
// 锟斤拷锟斤拷锟接︼拷锟斤拷锟�
HttpResponse response = HttpUtil.getHttpResponse(request);
// 锟叫讹拷锟角凤拷锟斤拷锟斤拷晒锟�
if (response.getStatusLine().getStatusCode() == 200) {
// 锟斤拷锟斤拷锟接�
result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "锟斤拷锟斤拷锟届常锟斤拷";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "锟斤拷锟斤拷锟届常锟斤拷";
return result;
}
return null;
}
public static boolean sendXML(String path, String xml) throws Exception {
byte[] data = xml.getBytes();
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
conn.setDoOutput(true);// 锟斤拷锟酵拷锟絧ost锟结交锟斤拷锟捷o拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(data);
outStream.flush();
outStream.close();
if (conn.getResponseCode() == 200) {
return true;
}
return false;
}
public static byte[] sendGetRequest(String path,
Map<String, String> params, String enc) throws Exception {
StringBuilder sb = new StringBuilder(path);
sb.append('?');
// ?method=save&title=435435435&timelength=89&
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc))
.append('&');
}
sb.deleteCharAt(sb.length() - 1);
URL url = new URL(sb.toString());
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
if (conn.getResponseCode() == 200) {
return readStream(conn.getInputStream());
}
return null;
}
public static boolean sendPostRequest(String path,
Map<String, String> params, String enc) throws Exception {
// title=dsfdsf&timelength=23&method=save
StringBuilder sb = new StringBuilder();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> entry : params.entrySet()) {
sb.append(entry.getKey()).append('=')
.append(URLEncoder.encode(entry.getValue(), enc))
.append('&');
}
sb.deleteCharAt(sb.length() - 1);
}
byte[] entitydata = sb.toString().getBytes();// 锟矫碉拷实锟斤拷亩锟斤拷锟斤拷锟斤拷锟斤拷锟�
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setConnectTimeout(5 * 1000);
conn.setDoOutput(true);// 锟斤拷锟酵拷锟絧ost锟结交锟斤拷锟捷o拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷
// Content-Type: application/x-www-form-urlencoded
// Content-Length: 38
conn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length",
String.valueOf(entitydata.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(entitydata);
outStream.flush();
outStream.close();
if (conn.getResponseCode() == 200)