private void initData() { customerServices = new ArrayList<UserInfoEO>(); if (FinanceStocksApp.isLogin()) { List<ChatEO> lstChats = ChatEO.getMsgList(0, AppConstants.PAGESIZE); if (lstChats != null && lstChats.size() > 0) { UserInfoEO serviceInfo = FinanceStocksApp.mAuthInfo.getBindService(); if (serviceInfo != null) { L.d(TAG, "[Bind ServiceInfo]: " + new Gson().toJson(serviceInfo)); customerServices.add(serviceInfo); } } List<NotificationEO> lstNotifications = NotificationEO.queryNotifyList(0, AppConstants.PAGESIZE); if (lstNotifications != null && lstNotifications.size() > 0) { NotificationEO mLastNotify = NotificationEO.queryLastNotify(); if (mLastNotify != null) { UserInfoEO mSystem = new UserInfoEO(); mSystem = new UserInfoEO(); mSystem.setNickName("系统消息"); mSystem.setAvatarUrl("drawable://" + R.drawable.head_kf); mSystem.setLastMsg(mLastNotify.getMsg()); mSystem.setLastMsgTime(mLastNotify.getCreateTime()); customerServices.add(mSystem); } } L.d(TAG, "Customer Service List: " + new Gson().toJson(customerServices)); if (listCustomerServices.getAdapter() == null) { adapter = new CustomerServiceAdapter(); adapter.setDatas(customerServices); listCustomerServices.setAdapter(adapter); } else { adapter.setDatas(customerServices); } } FinanceStocksApp.params.put(AppConstants.ISSERVICEMSGOPEN_KEY, false); FinanceStocksApp.params.put(AppConstants.ISSYSTEMMSGOPEN_KEY, false); }
@Override protected Object doInBackground(Object... params) { lineCount = lstInvestments.size(); List<FinancingProjectVo> lstInvestOrders = new ArrayList<FinancingProjectVo>(); res = queryInvestments(lineCount, null); L.d(TAG, "[My Investments]: " + new Gson().toJson(res)); if (res != null && res.getProjects() != null && res.getProjects().size() > 0) { lstInvestOrders.addAll(res.getProjects()); if (lstInvestOrders != null) { for (FinancingProjectVo investOrder : lstInvestOrders) { // 遍历排除已存在的项目 if (lstInvestments.contains(investOrder)) { lstInvestments.remove(investOrder); } } } lstInvestments.addAll(lstInvestOrders); Collections.sort(lstInvestments); } return super.doInBackground(params); }
private Res handlePOSTRequest(Req request, Class<Res> resClass) { L.d(TAG, "Request URL: " + request.getURL()); HttpPost httpPost = new HttpPost(request.getURL()); // 添加请求头 Map<String, String> headersMap = request.getHeaders(); if (headersMap != null) { Iterator<Map.Entry<String, String>> iterator = headersMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); httpPost.addHeader(entry.getKey(), entry.getValue()); } } L.d(TAG, "Request Header: " + new Gson().toJson(headersMap)); // 判断是否存在上传文件,以不同方式添加请求参数 if (request.existUploadFiles()) { Map<String, String> uploadFilesMap = request.getUploadFiles(); Map<String, Object> paramsMap = request.getParams(); /*MultipartEntityBuilder builder = MultipartEntityBuilder.create(); builder.setMode( HttpMultipartMode.BROWSER_COMPATIBLE ); // builder.setCharset( Charset.forName( "UTF-8" ) ); String data = new Gson().toJson( paramsMap ); // 设置中文编码 ContentType contentType = ContentType.create( "application/json" , HTTP.UTF_8 ); StringBody strBody = new StringBody( data , contentType ); builder.addPart( "data" , strBody ); // builder.addTextBody( "data" , data ); // builder.addTextBody( "data" , data , ContentType.APPLICATION_JSON.withCharset( "UTF-8" ) ); Log.d( "[File Data]" , data ); Iterator<Entry<String , String>> iterator = uploadFilesMap.entrySet().iterator(); while( iterator.hasNext() ) { Map.Entry<String , String> entry = iterator.next(); String key = entry.getKey(); String filePath = entry.getValue(); if( !TextUtils.isEmpty( filePath ) ) { byte[] fileByte = FileUtils.getBytesFromFile( new File( filePath ) ); builder.addBinaryBody( key , fileByte ); } } HttpEntity httpEntity = builder.build();*/ MultipartEntity httpEntity = new MultipartEntity(); StringBody sb = new StringBody( new Gson().toJson(paramsMap), ContentType.APPLICATION_JSON.withCharset("UTF-8")); httpEntity.addPart("data", sb); Iterator<Entry<String, String>> iterator = uploadFilesMap.entrySet().iterator(); while (iterator.hasNext()) { Map.Entry<String, String> entry = iterator.next(); String key = entry.getKey(); String filePath = entry.getValue(); if (!TextUtils.isEmpty(filePath)) { /*byte[] fileByte = FileUtils.getBytesFromFile( new File( filePath ) ); builder.addBinaryBody( key , fileByte );*/ ContentBody cbFile = new FileBody(new File(filePath)); httpEntity.addPart(key, cbFile); } } httpPost.setEntity(httpEntity); /*if( paramsMap != null ) { MultipartEntity httpEntity = new MultipartEntity(); Iterator<Map.Entry<String , Object>> iterator = paramsMap.entrySet().iterator(); while( iterator.hasNext() ) { Map.Entry<String , Object> entry = iterator.next(); L.d( TAG , "---- Params Key: " + entry.getKey() + " ----\n---- Params Value: " + entry.getValue() ); StringBody sb = new StringBody( new Gson().toJson( entry.getValue() ) , ContentType.APPLICATION_JSON.withCharset( "UTF-8" ) ); httpEntity.addPart( entry.getKey() , sb ); } httpPost.setEntity( httpEntity ); }*/ } else { Map<String, Object> paramsMap = request.getParams(); L.e(TAG, "[Request Params]: " + new Gson().toJson(paramsMap)); if (paramsMap != null) { String bodyJsonString = new Gson().toJson(paramsMap); StringEntity entity = null; try { entity = new StringEntity(bodyJsonString, HTTP.UTF_8); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } httpPost.setEntity(entity); } } Res response = null; try { HttpResponse httpResponse = httpClient.execute(httpPost); try { response = (Res) resClass.newInstance().parseResult(httpResponse); if (response != null && response.isTokenError()) { EventBus.getDefault().post(new ErrorTokenEvent()); } } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return response; }