和上次一样,我打算通过url向mysql存储,对之前的代码进行修改,可以使用
public class web_login {
public String executeHttpGet2(){
HttpURLConnection connection = null;
InputStream in = null;
try{
String Url = "http://192.168.0.105:8080/temdate/register";
try {
URL url = new URL(Url);
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();
int resultCode = connection.getResponseCode();
if(HttpURLConnection.HTTP_OK == resultCode) {
in = connection.getInputStream();
return parseInfo(in);
}
return null;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}catch (Exception e){
e.printStackTrace();
}finally {
//意外退出时,连接关闭保护
if(connection != null){
connection.disconnect();
}
if(in != null){
try{
in.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
return null;
}
public String executeHttpGet(String number,String name,String phone,String classname){
HttpURLConnection connection = null;
InputStream in = null;
try{
String Url = "http://192.168.0.105:8080/temdate/servlet_login";
try {
URL url = new URL(Url);
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 = "number=" + URLEncoder.encode(number,"UTF-8") + "&name=" + URLEncoder.encode(name,"UTF-8")+ "&phone=" + URLEncoder.encode(phone,"UTF-8")
+ "&classname=" + URLEncoder.encode(classname,"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 (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}catch (Exception e){
e.printStackTrace();
}finally {
//意外退出时,连接关闭保护
if(connection != null){
connection.disconnect();
}
if(in != null){
try{
in.close();
}catch (Exception 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){
Log.d("RegisterActivity",line);
response.append(line);
}
Log.d("RegisterActivity","response.toString():"+response.toString());
return response.toString();
}catch (Exception e){
e.printStackTrace();
}finally {
if(reader != null){
try{
reader.close();
}catch (Exception e){
e.printStackTrace();
}
}
}
return null;
}
}