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.http.HttpUtils; import com.jqmobile.core.utils.json.JSONConvertor2; import com.jqmobile.core.utils.json.JSONObject; import com.jqmobile.core.utils.plain.StringUtils; /** * 建议使用新版ClientContext2。新clientcontext实现久其移动客户端通讯协议 * @author MODI * */ @Deprecated public class ClientContext 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://127.0.0.1/patrol"; private final String serviceURI = WS_URI; private final HttpUtils hu = HttpUtils.getHttpUtils(serverURL, serviceURI); private final Map<String, String> sessionStr = new HashMap<String, String>(); private final Map<String, Object> session = new HashMap<String, Object>(); //=============================File upload download=================================== public boolean upload(String fileName, InputStream is){ fileName = StringUtils.valueOfBytes(fileName);// String url = serverURL+"/"+fileName+UPLOAD_URI; try { HttpURLConnection conn = hu.connect(url); OutputStream os = conn.getOutputStream(); // byte[] bs = new byte[1024]; // while(is.read(bs)!=-1){ // os.write(bs); // } int available = is.available(); if(available>2048){ int count = available/1024; byte[] bs = new byte[1024]; for(int i=0; i<count; i++){ if(-1!=is.read(bs)){ os.write(bs); } } bs = new byte[available-((count-1)*1024)]; if(-1 != is.read(bs)){ os.write(bs); } }else{ byte[] bs = new byte[available]; if(-1 != is.read(bs)){ os.write(bs); } } int b; while((b=is.read())!=-1){ os.write(b); } os.close(); conn.setConnectTimeout(HttpUtils.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 = hu.connect(url); conn.setConnectTimeout(HttpUtils.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 ClientContext put(String key, String value){ sessionStr.put(key, value); return this; } public <T> ClientContext 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; HttpUtils.setTIMEOUT(timeout); } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { Object result = hu.invoke(intf.getName(), method.getName(), args); return result; } } }