package com.example.PersonalCenter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class WebServiceGet {
    public static String executeHttpGet(String phonenumber,String password,String address){
        HttpURLConnection connection = null;
        InputStream in = null;

        String Url = "http://106.54.210.208:8080/ChineseM"+address;
        String path = Url+"?phonenumber=" + phonenumber + "&password=" + password;
        URL url = null;
        try {
            url = new URL(path);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            connection = (HttpURLConnection)url.openConnection();
            connection.setRequestMethod("GET");
            connection.setConnectTimeout(100000);//建立连接超时
            connection.setReadTimeout(80000);//传输数据超时
            in = connection.getInputStream();
            return parseInfo(in);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(connection != null){
                connection.disconnect();
            }
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    //将字节流转换成String
    public static String parseInfo(InputStream inputStream){
        BufferedReader reader = null;
        String line = "";
        StringBuilder response = new StringBuilder();
            try {
                reader = new BufferedReader(new InputStreamReader(inputStream));
                while((line = reader.readLine()) != null) {
                        response.append(line);
                }
                return response.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(reader!=null){
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        return null;
    }
}
