Android 请求
import java.net.URLEncoder; import java.util.ArrayList; import java.util.Arrays; import com.google.gson.Gson; /** * json字符串 和对象互换 工具类 * 依赖Gson包 * @see com.google.gson.Gson * @author devil * */ public class JsonUtil { /** * 将json字符串转换成对象 * * @param json * @param type * @return */ public static <T> T parse(String json, Class<T> type) { Gson gson = new Gson(); T t = null ; try { t = gson.fromJson(json, type) ; } catch (Exception e) { e.printStackTrace() ; return null ; } return t; } /** * 将json转成数组 * * @param json * @param type * @return */ public static <T> T[] parseArr(String json, Class<T[]> type) { return parse(json, type); } /** * 将json转成集合 * * @param json * @param type * @return */ public static <T> ArrayList<T> parseList(String json, Class<T[]> type) { return new ArrayList<T>(Arrays.asList(parse(json, type))); } /** * 将对象转成json字符串 * * @param o * @return */ public static String format(Object o) { Gson gson = new Gson(); return gson.toJson(o); } /** * 将对象转成json字符串 并使用url编码 * @param o * @return */ public static String formatURLString(Object o) { try { return URLEncoder.encode(format(o), "utf-8"); }catch (Exception e) { return null; } } } HttpPost httpPost = new HttpPost(url); try { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("username", username)); httpPost.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8)); DefaultHttpClient httpclient = new DefaultHttpClient(); HttpConnectionParams.setConnectionTimeout(httpPost.getParams(), 10000);//设置连接超时 HttpConnectionParams.setSoTimeout(httpPost.getParams(), 10000); //设置请求超时 HttpResponse httpResponse =httpclient.execute(httpPost); if(httpResponse.getStatusLine().getStatusCode()==200){//成功响应了请求 public static String postHttpResponseText(String url, String post) { BufferedReader reader = null; HttpURLConnection conn = null; try { URL httpUrl = new URL(url); HttpURLConnection httpConn = (HttpURLConnection) httpUrl .openConnection(); // //设置连接属性 httpConn.setDoOutput(true);// 使用URL 连接进行输出 httpConn.setDoInput(true);// 使用 URL 连接进行输入 httpConn.setUseCaches(false);// 忽略缓存 httpConn.setRequestMethod("POST");// 设置URL请求方法 String requestString = post; // 设置请求属性 // 获得数据字节数据,请求数据流的编码,必须和下面服务器端处理请求流的编码一致 byte[] requestStringBytes = requestString.getBytes("UTF-8"); httpConn.setRequestProperty("Content-length", "" + requestStringBytes.length); httpConn.setRequestProperty("Content-Type", "application/json"); httpConn.setRequestProperty("Connection", "Keep-Alive");// 维持长连接 httpConn.setRequestProperty("Charset", "UTF-8"); // 建立输出流,并写入数据 OutputStream outputStream = httpConn.getOutputStream(); outputStream.write(requestStringBytes); outputStream.close(); // 获得响应状态 int responseCode = httpConn.getResponseCode(); if (HttpURLConnection.HTTP_OK == responseCode) {// 连接成功 // // 当正确响应时处理数据 StringBuffer buffer = new StringBuffer(); String readLine = null; // 处理响应流,必须与服务器响应流输出的编码一致 reader = new BufferedReader(new InputStreamReader( httpConn.getInputStream(), "UTF-8")); while ((readLine = reader.readLine()) != null) { //buffer.append(readLine).append("\n"); buffer.append(readLine); } reader.close(); return buffer.toString(); } } catch (Exception e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } if (conn != null) { conn.disconnect(); } } return null; }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。