百度推广搜索API异步数据报告的下载与保存,另附乱码问题解决方案

//-------------------------------ReportService------------------------------------------

      ReportService service4 = factory.getService(ReportService.class);

      GetProfessionalReportIdRequest reportIdParamter=new GetProfessionalReportIdRequest();

      ReportRequestType reportType=new ReportRequestType();

      reportType.setReportType(14);

      DateFormat  df=new SimpleDateFormat("yyyy-mm-dd");

      reportType.setStartDate(df.parse("2014-03-05"));

      reportType.setEndDate(df.parse("2014-03-06"));

      reportType.setLevelOfDetails(11);//账户粒度

      reportType.setStatRange(2);

      reportType.setUnitOfTime(4);

      List<String> data=new ArrayList<String>();

      data.add("impression");

      data.add("click");

      data.add("cost");

      data.add("ctr");

      data.add("cpc");

      data.add("cpm");

      data.add("position");

      data.add("conversion");

      reportType.setPerformanceData(data);

      reportIdParamter.setReportRequestType(reportType);

      GetProfessionalReportIdResponse res7=service4.getProfessionalReportId(reportIdParamter);

//       System.out.println(res7);

      GetReportStateRequest  reportState=new GetReportStateRequest();

      reportState.setReportId(res7.getReportId());

      GetReportStateResponse res8=service4.getReportState(reportState);

      GetReportFileUrlResponse res9;

      URL  destUrl;

      long startDate=System.currentTimeMillis();

      System.out.println("startDate:"+startDate);

    for(int i=0;i<1;i++){

  System.out.println(res8.getIsGenerated());

  if(res8.getIsGenerated()==1){

  System.out.println("正在处理中......");

  }

if (res8.getIsGenerated()==3) {

GetReportFileUrlRequest fileUrl=new GetReportFileUrlRequest();

fileUrl.setReportId(res7.getReportId());

res9=service4.getReportFileUrl(fileUrl);

System.out.println(res9.getReportFilePath());

destUrl=new URL(res9.getReportFilePath());

URLConnection httpUrl=destUrl.openConnection();

      httpUrl.connect();

      InputStream content = (InputStream) httpUrl.getContent();

      BufferedReader input=new BufferedReader(new InputStreamReader(content));

      String line;

      FileWriter fw = new FileWriter("D:/project/c.csv");

      PrintWriter pw = new PrintWriter(fw);

      while((line=input.readLine())!=null){

 pw.println(line);

//   System.out.print("账号 ID "+i+":"+input.read()+" ");

//   i++;

      }

      pw.close();

      fw.close();

break;

}

    }

      long endDate=System.currentTimeMillis();

      System.out.println("endDate:"+endDate);

      System.out.println("startDate-endDate:"+(endDate-startDate));


你可能感兴趣的:(java,百度API, ,推广搜索)