zoukankan      html  css  js  c++  java
  • 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("
    ");
    					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;
    
    	}
    
  • 相关阅读:
    跨控制器跳转view——RedirectToRoute和RedirectToAction
    Web GIS离线解决方案
    逆变与协变详解
    C#操作符??和?:
    DBNull与Null的区别
    C#用DataTable实现Group by数据统计
    LINQ系列:LINQ to DataSet的DataTable操作
    Repeater数据控件的两个重要事件ItemDataBound 和 ItemCommand
    Visual Studio提示“无法启动IIS Express Web服务器”的解决方法
    ADO.NET 数据库备份等操作
  • 原文地址:https://www.cnblogs.com/gs21Joan/p/3990945.html
Copyright © 2011-2022 走看看