標籤:
//--------全域變數-----------
//註冊Url
private String urlPath="http://101.200.142.201:8080/VideoPlay/regist";
//----------------onCreate中---------------------------------
實現zhuce();方法
//--------註冊方法--------------------
//註冊方法
private void zhuce() {
new Thread(){
public void run() {
try {
//獲得輸入框輸入的內容
String phonenum=et_phone_num.getText().toString().trim();
String password=et_password.getText().toString().trim();
String name=et_name.getText().toString().trim();
String queren_password=et_queren_password.getText().toString().trim();
//編碼,將文字編碼
String ss=URLEncoder.encode(name, "utf-8");
//使用HttpURLConnection獲得網路資料
URL url=new URL(urlPath);
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
urlConnection.setConnectTimeout(5000);
urlConnection.setReadTimeout(5000);
urlConnection.setRequestMethod("POST");
urlConnection.connect();
String kk="userName="+ss+"&userPhone="+phonenum+"&userPassword="+password;
PrintWriter writer=new PrintWriter(urlConnection.getOutputStream());
writer.write(kk);
writer.flush();
writer.close();
int code=urlConnection.getResponseCode();
if (code==200) {
InputStream inputStream=urlConnection.getInputStream();
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
String liner;
StringBuffer buffer=new StringBuffer();
while ((liner=reader.readLine())!=null) {
buffer.append(liner);
}
String str=buffer.toString();
//建立Message給handler發送訊息
Message message=new Message();
message.what=1;
message.obj=str;
handler.sendMessage(message);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
};
}.start();
}
//-------------使用Handler接受 訊息並更新Ui-----------------
private Handler handler=new Handler(){
public void handleMessage(Message msg) {
switch (msg.what) {
case 1:
String text=(String) msg.obj;
tv_namess.setText(text);
break;
default:
break;
}
}
};
HttpURLConnection請求網路資料的Post請求