- public static Map<String, Object> addCreative(CreativeAudit creativeAudit) {
- Map<String, Object> result = new LinkedHashMap();
- Map<String, Object> params = new LinkedHashMap<String, Object>();
- params.put("dspid", username);
- params.put("token", password);
- Map<String, Object> crpm = new LinkedHashMap<String, Object>();
- crpm.put("url", creativeAudit.getCdn_path());
- crpm.put("landingpage", creativeAudit.getTarget_url());
- crpm.put("advertiser", StringUtil.toUTF(creativeAudit.getAdname()));
- crpm.put("startdate", creativeAudit.getSdate());
- crpm.put("enddate", creativeAudit.getEdate());
- crpm.put("monitor", StringUtil.getUrlFromScript(creativeAudit.getThird_static_perform()));
- crpm.put("cm", StringUtil.getUrlFromScript(creativeAudit.getThird_static_click()));
- List<Map> list = new ArrayList<Map>();
- list.add(crpm);
- params.put("material", list);
- JSONObject jsonObject = JSONObject.fromObject(params);
- String postParams = jsonObject.toString();
- String addUrl = url + "/dsp/api/upload";
- InputStream instr = null;
- java.io.ByteArrayOutputStream out = null;
- try {
- URL url = new URL(addUrl);
- HttpURLConnection urlCon = (HttpURLConnection) url
- .openConnection();
- urlCon.setDoOutput(true);
- urlCon.setDoInput(true);
- urlCon.setRequestMethod("POST");
- urlCon.setUseCaches(false);
- urlCon.setInstanceFollowRedirects(true);
- urlCon.setRequestProperty("Content-Type", "application/json");
- // urlCon.setRequestProperty("Accept-Charset", "utf-8");
- // urlCon.setRequestProperty("contentType", "utf-8");
- DataOutputStream printout = new DataOutputStream(urlCon.getOutputStream());
- printout.writeBytes(postParams);
- printout.flush();
- printout.close();
- instr = urlCon.getInputStream();
- byte[] bis = IOUtils.toByteArray(instr);
- String responseString = new String(bis, "UTF-8");
- if ((responseString == null) || ("".equals(responseString.trim()))) {
- result.put("failed", "没有返回结果");
- return result;
- }
- JSONObject response = JSONObject.fromObject(responseString);
- int resultid = response.getInt("result");
- if (resultid == 0) {
- result.put("success", creativeAudit);
- } else {
- result.put("failed", String.valueOf(resultid) + " 信息:" + response.getString("message"));
- }
- } catch (Exception e) {
- e.printStackTrace();
- result.put("failed", "网络等原因造成错误");
- } finally {
- try {
- out.close();
- instr.close();
- } catch (Exception ex) {
- }
- }
- /*
- creativeAudit.setUuid("189-"+creativeAudit.getGeoid());
- result.put("success",creativeAudit);
- */
- return result;
- }
- //该片段来自于http://www.codesnippet.cn/detail/2304201512408.html
来源: http://www.codesnippet.cn/detail/2304201512408.html