package com.example.PersonalCenter;

import android.util.Log;

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

public class WebServicePost {
    public static String execuHttpPost(String username,String phonenumber,String password,String address){
        HttpURLConnection connection = null;
        InputStream in = null;
        String path = "http://106.54.210.208:8080/ChineseM/"+address;
        URL url = null;
        try {
            url = new URL(path);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }
        try {
            connection = (HttpURLConnection) url.openConnection();
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setReadTimeout(8000);
            connection.setUseCaches(false);
            connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
            connection.connect();
            DataOutputStream out = new DataOutputStream(connection.getOutputStream());
            String data = "username="+ URLEncoder.encode(username,"UTF-8")+"&phonenumber="+URLEncoder.encode(phonenumber,"UTF-8")+"&password="+URLEncoder.encode(password,"UTF-8");
            out.writeBytes(data);
            out.flush();
            out.close();
            int resultCode = connection.getResponseCode();
            if(HttpURLConnection.HTTP_OK == resultCode){
                in = connection.getInputStream();
                return parseInfo(in);
            }
            return null;
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(connection != null){
                connection.disconnect();
            }
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;

    }
    public static String parseInfo(InputStream inputStream){
        BufferedReader reader = null;
        String line = "";
        StringBuilder response = new StringBuilder();
        reader = new BufferedReader(new InputStreamReader(inputStream));

            try {
                while((line = reader.readLine())!=null){
                    Log.d("RegisterActivity",line);
                    response.append(line);
                }
                Log.d("RegisterActivity","response.toSring:"+response.toString());
                return response.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if(reader != null){
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                }
            }

        return null;
    }
}
