/////////////////////////////////////////////////////////////////////////
//	project description: download heart rate csv files from motoactv.com
//	ps: revise the store path first or crate the folder
//	author: Jun
//	date: 10/23/2013
//	version: 2.0
/////////////////////////////////////////////////////////////////////////


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;


public class Main {
	private final static int BUFFER=10240;
	public static void main(String args[]) throws HttpException, IOException
	{
		 HttpClient client = new HttpClient();
	        //client.getHostConfiguration().setHost("www.imobile.com.cn", 80, "http");
	        HttpMethod method = getGetMethod("https://motoactv.com/session/login.json?password=scarletknights&remember_me=0&screen_name=sehealthmonitoring%40gmail.com");//使用POST方式提交数据
	     client.executeMethod(method);
	       //打印服务器返回的状态
	     System.out.println(method.getStatusLine());
	       //打印结果页面
	    String response =   new String(method.getResponseBodyAsString().getBytes("8859_1"));
	       //打印返回的信息
	     System.out.println(response);
	        method.releaseConnection();
	        
	        method = getGetMethod("https://motoactv.com/export/exportData.json?startDate=&endDate=&exportAll=true&formats%5B%5D=CSV");//使用POST方式提交数据
		     client.executeMethod(method);
		       //打印服务器返回的状态
		     System.out.println(method.getStatusLine());
		       //打印结果页面
		    response =   new String(method.getResponseBodyAsString().getBytes("8859_1"));
		       //打印返回的信息
		     System.out.println(response);
		        method.releaseConnection();
		        
		    String exportID=response.split(",")[1].split(":")[1].split("\"")[1];
		        
		    method=getGetMethod("https://motoactv.com/export/downloadExport?exportID="+exportID);
		    client.executeMethod(method);
		    InputStream in=method.getResponseBodyAsStream();
		    FileOutputStream out = new FileOutputStream(new File("E:\\test_jar\\test.zip"));   
		    byte[] b = new byte[BUFFER];   
            int len = 0;   
            while((len=in.read(b))!= -1){   
                out.write(b,0,len);  
                System.out.println(len);
            }   
            in.close();   
            out.close();   
               
        System.out.println("download, success!!");   
	    }
	    /** *//**
	     * 使用GET方式提交数据
	   * @return
	     */
	    private static HttpMethod getGetMethod(String str){
	    	System.out.println("Add:"+str);
	        return new GetMethod(str);
	    }
	    /** *//**
	     * 使用POST方式提交数据
	   * @return
	     */
	    private static HttpMethod getPostMethod(){
	        PostMethod post = new PostMethod("https://motoactv.com/session/login.json");
	        NameValuePair pw = new NameValuePair("password","scarletknights");
	        NameValuePair rm = new NameValuePair("remember_me","0");
	        NameValuePair sn = new NameValuePair("screen_name","sehealthmonitoring%40gmail.com");
	        post.setRequestBody(new NameValuePair[] {pw,rm,sn});
	        return post;
	    }

}

