android 论坛 code,Android HTTP Code

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;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值