现在的位置: 首页 > 综合 > 正文

android HttpURLConnection網絡連接

2018年05月09日 ⁄ 综合 ⁄ 共 2494字 ⁄ 字号 评论关闭
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Iterator;
import java.util.Map;
import org.json.JSONObject;
import cn.uninor.app.util.MyFileOut;

public class MyHttpURLConnection {


	public static String get(String url) {
		StringBuffer sBuffer = new StringBuffer();
		
		try {
			URL u = new URL(url);
			InputStream in = null;
			HttpURLConnection conn = (HttpURLConnection) u.openConnection();
			conn.setDoInput(true);
			conn.setRequestMethod("GET");
			conn.setRequestProperty("Content-Type","application/json; charset=utf-8");
			if(conn.getResponseCode() == 400){
				InputStream erris = conn.getErrorStream();  
				MyFileOut.writeOutputStrem(erris);
			}else if(conn.getResponseCode() == 200){
				byte[] buf = new byte[1024];
				in = conn.getInputStream();
				for (int n; (n = in.read(buf)) != -1;) {
					sBuffer.append(new String(buf, 0, n, "UTF-8"));
				}
			}
			in.close();
			conn.disconnect();
		}catch (FileNotFoundException e1) {
			e1.printStackTrace();
		}catch (Exception e) {
			e.printStackTrace();
		}
		
		return sBuffer.toString();
	}
	
	
	public static JSONObject post(String url, Map<String, Object> params) {
		StringBuffer sBuffer = map2StringBuffer(params);
		JSONObject object = postJson(url,sBuffer.toString());
		return object;
	}
	
	public static JSONObject postJson(String url, String params) {
		JSONObject object = null;
		try {
			URL u = new URL(url);
			InputStream in = null;
			byte[] buffer = params.getBytes();
			
			HttpURLConnection conn = (HttpURLConnection) u.openConnection();
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setRequestMethod("POST");
			conn.setRequestProperty("Content-Length", buffer.length + "");
			conn.setRequestProperty("Content-Type","application/json;charset=utf-8");
			OutputStream out = conn.getOutputStream();
			out.write(buffer);
			out.close();
			if(conn.getResponseCode() == 400){
				InputStream erris = conn.getErrorStream();  
				MyFileOut.writeOutputStrem(erris);
			}else if(conn.getResponseCode() == 200){
				in = conn.getInputStream();
				StringBuffer sBuffer = new StringBuffer();
				byte[] buf = new byte[1024];
				for (int n; (n = in.read(buf)) != -1;) {
					sBuffer.append(new String(buf, 0, n, "utf-8"));
				}
				object = new JSONObject(sBuffer.toString());
				in.close();
			}
			conn.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return object;
	}
	
	

	public static StringBuffer map2StringBuffer(Map<String, Object> params) {
		StringBuffer buf = new StringBuffer("");
		if (params != null && params.size() > 0) {
			Iterator<Map.Entry<String, Object>> it = params.entrySet().iterator();
			while (it.hasNext()) {
				Map.Entry<String, Object> entry = it.next();
				buf.append(entry.getKey()).append("=").append(entry.getValue()).toString();
				buf.append("&");
			}
		}
		if (buf.length() > 1)
			buf.deleteCharAt(buf.length() - 1);
		return buf;
	}
}

抱歉!评论已关闭.