/** * 上传图片 * * @param * @return * @throws AppException */ public static boolean uploadPic( AppContext appContext, String picUrl, PicBean picBean, String uid, String phone) throws AppException { Map<String, Object> params = new HashMap<String, Object>(); params.put("lat", picBean.getLat()); params.put("lng", picBean.getLng()); params.put("u", uid); params.put("picName", picBean.getTitle()); // add params.put("ORGID", picBean.getUnitNumber()); params.put("mobile", phone); params.put("address", picBean.getAddress()); // add Map<String, File> files = new HashMap<String, File>(); if (picBean.getPath() != null) files.put("img", picBean.getImgeFile()); try { return http_post(appContext, picUrl, params, files); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } }
public static Boolean uploadBeeSource( AppContext appContext, ArrayList<BeeSource> beelist, String url, String phone, String userId) throws AppException { // TODO Auto-generated method stub JSONArray data = new JSONArray(); for (int i = 0; i < beelist.size(); i++) { String memo = "SYR%s;DD%s;BH%s;ZL%s;SL%s;GG%s;SLIAO%s;QMKS%s;QMJS%s;SGRQ%s;SGDW%s;ND%s;lat%s;lng%s;CMJG%s;YYQK%s;FZ%s;ORGID%s"; memo = String.format( memo, beelist.get(i).getA2(), beelist.get(i).getA4(), beelist.get(i).getA1(), beelist.get(i).getA5(), beelist.get(i).getA7(), beelist.get(i).getA8(), beelist.get(i).getA6(), beelist.get(i).getA12(), beelist.get(i).getA13(), beelist.get(i).getA14(), beelist.get(i).getA15(), beelist.get(i).getA9(), 0, 0, beelist.get(i).getA10(), beelist.get(i).getA11(), beelist.get(i).getA16(), beelist .get(i) .getUnitNumber() /*appContext.mOrgList.get(whichUnit).GetID()*/ /*Constants.unitMap.get(whichUnit)*/); JSONObject sms = new JSONObject(); try { sms.put("b", memo); sms.put("m", phone); sms.put("u", userId); } catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); } data.put(sms); } Map<String, Object> params = new HashMap<String, Object>(); params.put("data", data); try { return http_post(appContext, url, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } }
/** * 检查版本更新 * * @param url * @return */ public static Update checkVersion(AppContext appContext) throws AppException { try { return Update.parse( http_get(appContext, /*URLs.UPDATE_VERSION*/ "http://223.4.144.25:30018/version.xml")); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } }
public static boolean modifyPassword2( AppContext appContext, String rawUrl, String userId, String new_password) throws AppException { // TODO Auto-generated method stub Map<String, Object> params = new HashMap<String, Object>(); params.put("userId", userId); params.put("passwd", new_password); try { String url = rawUrl + "/modifyPWD.aspx"; return http_post(appContext, url, params, null); } catch (Exception e) { if (e instanceof AppException) throw (AppException) e; throw AppException.network(e); } }
/** * get请求URL * * @param url * @throws AppException */ private static InputStream http_get(AppContext appContext, String url) throws AppException { // System.out.println("get_url==> "+url); /* String cookie = getCookie(appContext); String userAgent = getUserAgent(appContext);*/ HttpClient httpClient = null; GetMethod httpGet = null; String responseBody = ""; int time = 0; do { try { httpClient = getHttpClient(); // httpGet = getHttpGet(url/*, cookie, userAgent*/); httpGet = getHttpGet(url, appContext.mCookies, ""); /*httpGet = getHttpGet(url, cookie, userAgent);*/ int statusCode = httpClient.executeMethod(httpGet); if (statusCode != HttpStatus.SC_OK) { throw AppException.http(statusCode); } responseBody = httpGet.getResponseBodyAsString(); // System.out.println("XMLDATA=====>"+responseBody); break; } catch (HttpException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // 发生致命的异常,可能是协议不对或者返回的内容有问题 e.printStackTrace(); throw AppException.http(e); } catch (IOException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // 发生网络异常 e.printStackTrace(); throw AppException.network(e); } finally { // 释放连接 httpGet.releaseConnection(); httpClient = null; } } while (time < RETRY_TIME); responseBody = responseBody.replaceAll("\\p{Cntrl}", ""); /*if(responseBody.contains("result") && responseBody.contains("errorCode") && appContext.containsProperty("user.uid")){ try { Result res = Result.parse(new ByteArrayInputStream(responseBody.getBytes())); if(res.getErrorCode() == 0){ appContext.Logout(); appContext.getUnLoginHandler().sendEmptyMessage(1); } } catch (Exception e) { e.printStackTrace(); } }*/ return new ByteArrayInputStream(responseBody.getBytes()); }
/** * 公用post方法 * * @param url * @param params * @param files * @throws AppException */ private static String _post( AppContext appContext, String url, Map<String, Object> params, Map<String, File> files) throws AppException { /*String cookie = getCookie(appContext); String userAgent = getUserAgent(appContext);*/ HttpClient httpClient = null; PostMethod httpPost = null; // post表单参数处理 int length = (params == null ? 0 : params.size()) + (files == null ? 0 : files.size()); Part[] parts = new Part[length]; int i = 0; if (params != null) for (String name : params.keySet()) { parts[i++] = new StringPart(name, String.valueOf(params.get(name)), UTF_8); // System.out.println("post_key==> "+name+" value==>"+String.valueOf(params.get(name))); } if (files != null) for (String file : files.keySet()) { try { parts[i++] = new FilePart(file, files.get(file)); } catch (FileNotFoundException e) { e.printStackTrace(); } // System.out.println("post_key_file==> "+file); } String responseBody = ""; int time = 0; do { try { httpClient = getHttpClient(); httpPost = getHttpPost(url /*, cookie, userAgent*/); httpPost.setRequestEntity(new MultipartRequestEntity(parts, httpPost.getParams())); int statusCode = httpClient.executeMethod(httpPost); if (statusCode != HttpStatus.SC_OK) { throw AppException.http(statusCode); } else if (statusCode == HttpStatus.SC_OK) { /*Cookie[] cookies = httpClient.getState().getCookies(); String tmpcookies = ""; for (Cookie ck : cookies) { tmpcookies += ck.toString()+";"; }*/ // 保存cookie /*if(appContext != null && tmpcookies != ""){ appContext.setProperty("cookie", tmpcookies); appCookie = tmpcookies; }*/ } responseBody = httpPost.getResponseBodyAsString(); // System.out.println("XMLDATA=====>"+responseBody); break; } catch (HttpException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // 发生致命的异常,可能是协议不对或者返回的内容有问题 e.printStackTrace(); throw AppException.http(e); } catch (IOException e) { time++; if (time < RETRY_TIME) { try { Thread.sleep(1000); } catch (InterruptedException e1) { } continue; } // 发生网络异常 e.printStackTrace(); throw AppException.network(e); } finally { // 释放连接 httpPost.releaseConnection(); httpClient = null; } } while (time < RETRY_TIME); /* responseBody = responseBody.replaceAll("\\p{Cntrl}", "");*/ /*if(responseBody.contains("result") && responseBody.contains("errorCode") && appContext.containsProperty("user.uid")){ try { Result res = Result.parse(new ByteArrayInputStream(responseBody.getBytes())); if(res.getErrorCode() == 0){ appContext.Logout(); appContext.getUnLoginHandler().sendEmptyMessage(1); } } catch (Exception e) { e.printStackTrace(); } } return new ByteArrayInputStream(responseBody.getBytes());*/ return responseBody; }