package com.jqmobile.core.android; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.net.HttpURLConnection; import java.util.HashMap; import java.util.Map; import android.app.Application; import com.jqmobile.core.http2.HttpSerializableObject; import com.jqmobile.core.http2.HttpUtils; import com.jqmobile.core.utils.json.JSONArray; import com.jqmobile.core.utils.json.JSONConvertor2; import com.jqmobile.core.utils.json.JSONObject; import com.jqmobile.core.utils.plain.StringUtils; /** * 新版通讯协议 * @author MODI * */ public class ClientContext2 extends Application{ /** * [MMC]webservice的服务相对地址 */ public static String WS_URI = "/ws/android.client"; public static String UPLOAD_URI = "upload.client"; public static String DOWNLOAD_URI = "download.client"; /** * 连接超时 */ // private final static int TIMEOUT = 10*1000; //10秒 static final // private final String serverURL = "http://115.29.47.215:9797"; // private final String serverURL = "http://10.29.0.9:9797"; private final String serverURL = "http://192.168.1.116:9797"; private final String serviceURI = WS_URI; private final HttpUtils hu = new HttpUtils(serverURL, serviceURI);//.getHttpUtils(serverURL, serviceURI); private final Map<String, String> sessionStr = new HashMap<String, String>(); private final Map<String, Object> session = new HashMap<String, Object>(); private String sessionId; //=============================File upload download=================================== public boolean upload(String fileName, InputStream is){ fileName = StringUtils.valueOfBytes(fileName);// String url = serverURL+"/"+fileName+UPLOAD_URI; try { HttpURLConnection conn = HttpUtils.connect(url); OutputStream os = conn.getOutputStream(); // byte[] bs = new byte[1024]; // while(is.read(bs)!=-1){ // os.write(bs); // } byte[] bs = new byte[is.available()]; if(-1 != is.read(bs)){ os.write(bs); } int b; while((b=is.read())!=-1){ os.write(b); } os.close(); conn.setConnectTimeout(hu.getTimeOut()); int responseCode = conn.getResponseCode(); // StringBuilder sResult = new StringBuilder(); if (responseCode == HttpURLConnection.HTTP_OK) { // Log.i("load", fileName+"上传成功"); InputStream httpInput = null; BufferedReader httpReader = null; // 读取数据 httpInput = conn.getInputStream(); httpReader = new BufferedReader(new InputStreamReader( httpInput, HttpUtils.CHARSET)); String line = null; while ((line = httpReader.readLine()) != null) { if (sResult.length() > 0) { sResult.append("\n"); } sResult.append(line); } if(sResult.toString().isEmpty()){ return false; } JSONObject result = new JSONObject(sResult.toString()); Object obj = JSONConvertor2.unSerializable(result); // Object obj = result.get(HttpUtils.RESPONSE_DATA); if(obj instanceof Boolean){ return (Boolean) obj; } } } catch (Exception e) { e.printStackTrace(); return false; } return false; } public InputStream download(String fileName){ fileName = StringUtils.valueOfBytes(fileName);// String url = serverURL+"/"+fileName+DOWNLOAD_URI; try { HttpURLConnection conn = HttpUtils.connect(url); conn.setConnectTimeout(hu.getTimeOut()); int responseCode = conn.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { // Log.i("load", fileName+"上传成功"); return conn.getInputStream(); } // OutputStream os = conn.getOutputStream(); // byte[] bs = new byte[1024]; // while(is.read(bs)!=-1){ // os.write(bs); // } // os.close(); } catch (IOException e) { e.printStackTrace(); } return null; } //================================================================ @SuppressWarnings("unchecked") public <T> T getService(Class<T> serviceClass){ return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class<?>[]{serviceClass}, new ClientInvocationHandler(serviceClass)); } public ClientContext2 put(String key, String value){ sessionStr.put(key, value); return this; } public <T> ClientContext2 put(Class<T> c, T value){ session.put(c.getName(), value); return this; } public String get(String key){ return sessionStr.get(key); } @SuppressWarnings("unchecked") public <T> T get(Class<T> c){ return (T) session.get(c.getName()); } class ClientInvocationHandler implements InvocationHandler{ private final Class<?> intf; /** * 构造函数 * @param serverURL 服务地址 * @param serviceURI 业务服务对相应的相对地址 * @param sessionID */ public ClientInvocationHandler(Class<?> intf) { this(intf, 10*1000); } public ClientInvocationHandler( Class<?> intf, int timeout) { this.intf = intf; hu.setTimeOut(timeout);; // HttpUtils.setTIMEOUT(timeout); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { HttpSerializableObject hso = new HttpSerializableObject(); hso.setIntf(intf.getName()); hso.setMethod(method.getName()); hso.setSession(sessionId); hso.setData(JSONConvertor2.serializableArray(args).toString()); // HttpSerializableObject result = hu.invoke(hso); if(null != result.getSession() && !result.getSession().equals("")){ sessionId = result.getSession(); } if(null != result.getException()){ throw new Exception(); } JSONArray array = new JSONArray(result.getData()); return JSONConvertor2.unAutoSerializable(array.getJSONObject(0), method.getReturnType()); } } //用于客户端重新登录的时候 public void ClearSession(){ this.sessionId = null; } }