版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shuimofengyang/article/details/84137338
背景:取另外分公司的数据!以前是有那边的同事每天发数据包过来。现在是提供接口,获取众多类型的下载地址,自己去下。我来接手做这个事。语言java ,参数json格式 ,返回也是json格式。解析取到url生成txt文件。
1.依赖:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.5</version> </dependency> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.6</version> </dependency>
2.
package getUrl; import com.google.gson.JsonElement; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import org.apache.http.HttpEntity; import org.apache.http.client.methods.CloseableHttpResponse; import org.apache.http.client.methods.HttpPost; import org.apache.http.entity.StringEntity; import org.apache.http.impl.client.CloseableHttpClient; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.json.JSONObject; import java.io.*; import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class RestUtil { /** * * @param url 请求地址 * @param body 请求参数 * @return 生成下载链接文件 * description:根据请求返回结果解析下载链接,生成链接文件。 * @throws Exception */ public static String sendHttpPost(String url, String body) throws Exception { SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date d=new Date(); String today = sdf.format(new Date(d.getTime() - (long)24 * 60 * 60 * 1000 * 3)); CloseableHttpClient httpClient = HttpClients.createDefault(); HttpPost httpPost = new HttpPost(url); httpPost.addHeader("Content-Type", "application/json"); httpPost.setEntity(new StringEntity(body)); CloseableHttpResponse response = httpClient.execute(httpPost); System.out.println(response.getStatusLine().getStatusCode() + "\n"); HttpEntity entity = response.getEntity(); String responseContent = EntityUtils.toString(entity, "UTF-8"); JsonParser parse = new JsonParser(); JsonObject json = (JsonObject) parse.parse(responseContent); Iterator<JsonElement> iterator = json.get("data").getAsJsonObject().get("result").getAsJsonArray().iterator(); JsonObject parm = (JsonObject)parse.parse(body); String typeId=parm.get("params").getAsJsonObject().get("typeId").toString().substring(1,6); FileOutputStream stream = new FileOutputStream(typeId +"_" + today + ".txt"); while (iterator.hasNext()) { String item = iterator.next().toString(); JsonObject subjson = (JsonObject) parse.parse(item); String urls_one = subjson.get("downloadUrl").getAsString() + "\r\n"; byte[] bytes = urls_one.getBytes(); stream.write(bytes); stream.flush(); } stream.close(); response.close(); httpClient.close(); return responseContent; } public String sendParams(String type){ JSONObject jsonObj = new JSONObject(); SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd"); Date d=new Date(); String today = sdf.format(new Date(d.getTime() - (long)24 * 60 * 60 * 1000 * 3)); Map<String,String> params= new HashMap(); params.put("date","20181112"); //params.put("date",today); params.put("typeId",type); jsonObj.put("params",params); jsonObj.put("key","/3MUlS7058C0ow7avJTb"); jsonObj.put("id","5be566660e5b084842ca732c"); jsonObj.put("pageNum",0); jsonObj.put("pageSize",25); return jsonObj.toString(); } public static void main(String []args) { try { RestUtil restUtil = new RestUtil(); String url ="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; String[] arr={"layout","video","click","ffalcon_appstore"}; for (int i = 0; i <arr.length ; i++) { String parm=restUtil.sendParams(arr[i]); String resultString = restUtil.sendHttpPost(url, parm); System.out.println("参数:"+parm); System.out.println("返回结果:"+resultString); } } catch (Exception e) { // TODO: handle exception System.out.print(e.getMessage()); } } }
结果: