客户端和服务器端交流需要使用网络连接,考虑到减少重复代码,于是整理了一个网络连接工具类
最后两个参数分别是操作成功和失败的回调
package com.example.panda.easyexpress10;
import android.os.AsyncTask;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URL;
import java.net.URLConnection;
/**
* Created by Panda on 2015/9/8.
*/
public class DoNet
{
private int methodId;
private String urlStr;
private String paramsString;
private String resultStr;
public DoNet(final int methodId, final String urlStr, final String paramsString, final OkCallBack ok,final NotOkCallBack notok) {
System.out.println("------------------------------执行构造函数");
this.methodId=methodId;
this.urlStr=urlStr;
this.paramsString = paramsString;
this.resultStr = "";
new AsyncTask<Void,Void,String>(){
@Override
protected void onPostExecute(String s) {
if (s!=null){
if (ok!=null){
System.out.println("------------------------------提示:已成功取回数据!" +s);
ok.OnOk(s);
}
}else{
if (notok!=null){
System.out.println("------------------------------请求失败,请检查Url是否正确!" );
notok.OnNotOk();
}
}
super.onPostExecute(s);
}
@Override
protected String doInBackground(Void... params) {
System.out.println("------------------------------正在访问:"+urlStr);
StringBuilder sb=new StringBuilder();
try {
URLConnection urlc;
if (methodId==1){//1代 表get
System.out.println("------------------------------执行get方法,访问:"+urlStr+"?"+paramsString);
urlc=new URL(urlStr+"?"+paramsString).openConnection();
}else{
System.out.println("------------------------------执行post方法,访问:"+urlStr+"参数"+paramsString);
urlc= new URL(urlStr).openConnection();
urlc.setDoOutput(true);
urlc.setDoInput(true);
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(urlc.getOutputStream(),"utf-8"));
bw.write(paramsString);
bw.flush();//必须要!!!!!!
}
System.out.println("------------------------------即将读取数据.." );
//开始读取数据
BufferedReader br=new BufferedReader(new InputStreamReader(urlc.getInputStream(),"utf-8"));
String line;
int count=0;
while((line=br.readLine())!=null){
sb.append(line);
System.out.println("------------------------------正在读取第" + (count++) +"行");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
}.execute();
}
public interface OkCallBack{
Void OnOk(String result);
}
public interface NotOkCallBack{
Void OnNotOk();
}
}
在点击事件中调用
public void onClick(View v) {
new DoNet(
2, //2代表 post
Varbs.HostSting+"check/",
"uid="+Varbs.uid,//参数请求格式 "uid=5"
new DoNet.OkCallBack() {
public Void OnOk(String result) {
if (result.trim().equals("T")) {
Toast.makeText(getApplicationContext(), "签到成功", Toast.LENGTH_SHORT).show();
}else if(result.trim().equals("AlredyChecked")) {
Toast.makeText(getApplicationContext(), "明天再签到吧!", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getApplicationContext(), "签到失败", Toast.LENGTH_SHORT).show();
}
return null;
}
},
new DoNet.NotOkCallBack() {
public Void OnNotOk() {
System.out.println("请检查网络连接!");
Toast.makeText(getApplicationContext(), "请检查网络连接", Toast.LENGTH_SHORT).show();
return null;
}
});
}