Get和Post提交方式的自定义工具类:
Get方式提交的方法工具类:
**
* http访问网络的get和post方式的封装类
* Created by Administrator on 2017/3/13.
*/
public class HttpUtil {
/**
* 静态get方法工具类,直接用类名调用
* 调用自定义读取连接返回结果的封装类的方法
*/
public static String httpGet(String url, Map<String,Object> map){
//url的拼接操作url+"?key=value&"
try {
//调用一个自定义的解析参数map获取username和password的方法
String data=parsePamars(map);
String strurl=url+"?"+data;
URL newUrl=new URL(strurl);
HttpURLConnection conn= (HttpURLConnection) newUrl.openConnection();//打开连接
conn.setRequestMethod("GET");//设置请求方式,大写的GET
//调用自定义获取请求结果的封装方法
String result=readRusult(conn);
return result;
}catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
Post方式提交的方法工具类:
public static String httpPost(String url,Map
解析Map结合获取data数据的方法:
public static String parsePamars(Map<String ,Object> map){
String data="";
if(map!=null){
Set<String> set= map.keySet();//遍历map中的健,找到对应的值
for (String key:set) {
String value= map.get(key).toString();//取得值
data=data+key+"="+value+"&";//最后会多出一个&符号
}
if(!TextUtils.isEmpty(data)){
data= data.substring(0,data.length()-1);//将最后一个&符号去掉
}
}
return data;
读取CONN结果的方法:
public static String readRusult(HttpURLConnection conn){
try {
int code= conn.getResponseCode();
if(code==200){//请求成功,读取结果
InputStream is=conn.getInputStream();
InputStreamReader inreader=new InputStreamReader(is);
BufferedReader reader=new BufferedReader(inreader);
String line="";
StringBuffer buffer=new StringBuffer();
while ((line=reader.readLine())!=null){
buffer.append(line);
}
//关闭流,断开连接
reader.close();
inreader.close();
is.close();
conn.disconnect();
return buffer.toString();
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
MainAcvitity中调用工具类:
public class MainActivity extends AppCompatActivity {
private EditText et_user,et_password;
private Button btn_login;
private MyThread thread=null;
private MyHandle handle=null;
private String url=”http://192.168.15.114:8080/HttpTest/Login”;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView(){
handle=new MyHandle();//实例化控件时候也实例化handle
et_user= (EditText) findViewById(R.id.et_user);
et_password= (EditText) findViewById(R.id.et_password);
btn_login= (Button) findViewById(R.id.btn_login);
btn_login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//①实例化控件,给按钮添加点击事件,点击按钮启动子线程
if (thread==null){
thread=new MyThread();
thread.start();
}
}
});
}
/**
* ②自定义子线程
*/
class MyThread extends Thread{
//③重写run方法
//访问网络,进行登录,把登录的结果利用handle发送到主线程,写一个访问网络的方法
@Override
public void run() {
super.run();
//⑤调用访问网络的方法,获取结果值
String result= loginGet();
String result=loginPost();
Map<String ,Object> map=new HashMap<>();
map.put("username",et_user.getText().toString());
map.put("password",et_password.getText().toString());
String result=HttpUtil.httpGet(url,map);
String result=HttpUtil.httpPost(url,map);
//⑥子线程向主线程发送message,需要自定义handle
Message message= handle.obtainMessage(1,result);
handle.sendMessage(message);
//发送过消息,线程置空,方便下次使用
thread=null;
}
}