package com.and;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.apache.http.client.methods.*;
import org.apache.http.entity.ByteArrayEntity;
import org.apache.http.entity.StringEntity;
import org.json.JSONException;
import org.json.JSONObject;
import android.os.Build;
import android.content.Context;
import android.telephony.TelephonyManager;
/**
* 对HTTP请求数据做分装
* @author Administrator
*
*/
public class HttpUtil {
//初始化httpClient
public static HttpClient httpClient = new DefaultHttpClient();
private static Context context;
private static HttpUriRequest request = null;
private static HttpResponse response = null;
private static String deviceId = android.provider.Settings.System.getString(context.getContentResolver(), "android_id");
private static String username = "lhq";
private static String password = "123456";
private static String session = null;
private final static String Session_Key = "xxyyymm3234kkfd";
final static HttpUtil INSTAND = new HttpUtil();
public static String getUsername() {
return username;
}
public static void setUsername(String username) {
HttpUtil.username = username;
}
public static String getPassword() {
return password;
}
public static void setPassword(String password) {
HttpUtil.password = password;
}
/**
* 封装HTTP的get和post请求
* @param url 请求的url
* @param body 参数MAP
* @param post 是否为post请求
* @return
* @throws ClientProtocolException
* @throws IOException
*/
public static Map getDataFromUrl(String url, Map body, Boolean post) throws ClientProtocolException, IOException
{
Map map = new HashMap();;
if(body == null && !post)
{
request = new HttpGet(url);
}
else
{
HttpPost requestPost = new HttpPost(url);
JSONObject jsondata = null;
byte [] buffer = null;
//失败转byte
try
{
jsondata = new JSONObject(body);
//转json
requestPost.setEntity(new StringEntity(jsondata.toString()));
}
catch(Exception e)
{
ByteArrayOutputStream bo = new ByteArrayOutputStream();
ObjectOutputStream os = new ObjectOutputStream(bo);
os.writeObject(body);
buffer =bo.toByteArray();
requestPost.setEntity(new ByteArrayEntity(buffer));
}
request = requestPost;
}
request.setHeader("DeviceID", deviceId);
request.setHeader("username", username);
request.setHeader("password", password);
request.setHeader("Cookie", Session_Key + "=" + session);
response = httpClient.execute(request);
if(response.getStatusLine().getStatusCode() == 200){
String resultData = EntityUtils.toString(response.getEntity());
InputStream in = new ByteArrayInputStream(resultData.getBytes());
String result = parseResult(in);
map = getData(result);
return map;
}
return null;
}
/**
* 得到结果数据流,对数据处理防止数据反复读取报错
* @param in
* @return
*/
public static String parseResult(InputStream in){
String result = null;
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader( new InputStreamReader(in));
String line = null;
try{
while((line = br.readLine()) != null){
sb.append(line);
}
br.close();
}catch(Exception e){
e.printStackTrace();
}finally{
if(result != null){
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return sb.toString();
}
/**
* 处理服务器传过来的字符串的json数据
* @param str
* @return
*/
//{"pass":"4355","name":"12342","wang":"fsf"}
public static Map getData(String str){
String sb = str.substring(1, str.length()-1);
String[] name = sb.split("\\\",\\\"");
String[] nn =null;
Map map = new HashMap();
for(int i= 0;i
nn = name[i].split("\\\":\\\"");
map.put(nn[0], nn[1]);
}
return map;
}
}