1. 程式人生 > >xUtils的網路請求簡單封裝

xUtils的網路請求簡單封裝

其他的前提條件就不多說了,只要你會使用xutils就完全可以使用。不多說了,直接上程式碼。

HttpxUtils.java類:

/**
 * @Title 封裝xUtils網路請求
 * @Authour zhoujp
 * @Time 2016年7月27日 下午3:25:21
 * 
 */
public class HttpxUtils {
/**
* 傳送get請求

* @param <T>
*/
public static <T> Cancelable Get(String url, Map<String, String> map, CommonCallback<T> callback) {
RequestParams params = new RequestParams(url);
if (null != map) {
for (Map.Entry<String, String> entry : map.entrySet()) {
params.addQueryStringParameter(entry.getKey(), entry.getValue());
}
}
Cancelable cancelable = x.http().get(params, callback);
return cancelable;
}

/**
* 傳送post請求

* @param <T>
*/
public static <T> Cancelable Post(String url, Map<String, Object> map, CommonCallback<T> callback) {
RequestParams params = new RequestParams(url);
if (null != map) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
params.addParameter(entry.getKey(), entry.getValue());
}
}
Cancelable cancelable = x.http().post(params, callback);
return cancelable;
}

/**
* 上傳檔案

* @param <T>
*/
public static <T> Cancelable UpLoadFile(String url, Map<String, Object> map, CommonCallback<T> callback) {
RequestParams params = new RequestParams(url);
if (null != map) {
for (Map.Entry<String, Object> entry : map.entrySet()) {
params.addParameter(entry.getKey(), entry.getValue());
}
}
params.setMultipart(true);
Cancelable cancelable = x.http().get(params, callback);
return cancelable;
}

/**
* 下載檔案

* @param <T>
*/
public static <T> Cancelable DownLoadFile(String url, String filepath, CommonCallback<T> callback) {
RequestParams params = new RequestParams(url);
// 設定斷點續傳
params.setAutoResume(true);
params.setSaveFilePath(filepath);
Cancelable cancelable = x.http().get(params, callback);
return cancelable;
}

public class JsonResponseParser implements ResponseParser {
// 檢查伺服器返回的響應頭資訊
@Override
public void checkResponse(UriRequest request) throws Throwable {
}


/**
* 轉換result為resultType型別的物件

* @param resultType
*            返回值型別(可能帶有泛型資訊)
* @param resultClass
*            返回值型別
* @param result
*            字串資料
* @return
* @throws Throwable
*/
@Override
public Object parse(Type resultType, Class<?> resultClass, String result) throws Throwable {
return new Gson().fromJson(result, resultClass);
}
}
}

簡單測試用例:

@ContentView(R.layout.activity_main)
public class MainActivity extends Activity {

@ViewInject(R.id.btn_get)
private Button btn_get;// get請求

@ViewInject(R.id.btn_post)
private Button btn_post;// post請求

@ViewInject(R.id.btn_load)
private Button btn_load;// 上傳檔案

@ViewInject(R.id.btn_down)
private Button btn_down;// 下載檔案

@ViewInject(R.id.btn_get_xml)
private Button btn_get_xml;// 傳送get請求(伺服器以xml格式返回)

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
x.view().inject(this);// 初始化
}
@Event(value = { R.id.btn_get, R.id.btn_post, R.id.btn_load, R.id.btn_down, R.id.btn_get_xml })
private void getButtonEvent(View v) {
switch (v.getId()) {
case R.id.btn_get:
String urlget = "http://api.k780.com:88/?app=idcard.get";
Map<String, String> mapGet = new HashMap<>();
mapGet.put("appkey", "10003");
mapGet.put("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4");
mapGet.put("format", "json");
mapGet.put("idcard", "110101199001011114");
HttpxUtils.Get(urlget, mapGet, new CommonCallback<String>() {

@Override
public void onCancelled(CancelledException arg0) {
}


@Override
public void onError(Throwable arg0, boolean arg1) {

}

@Override
public void onFinished() {

}

@Override
public void onSuccess(String arg0) {
Toast.makeText(getApplicationContext(), arg0, Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.btn_post:
String urlpost = "http://api.k780.com:88/?app=idcard.get";
Map<String, Object> mapPost = new HashMap<>();
mapPost.put("appkey", "10003");
mapPost.put("sign", "b59bc3ef6191eb9f747dd4e83c99f2a4");
mapPost.put("format", "json");
mapPost.put("idcard", "110101199001011114");
HttpxUtils.Post(urlpost, mapPost, new CommonCallback<String>() {
@Override
public void onCancelled(CancelledException arg0) {

}

@Override
public void onError(Throwable arg0, boolean arg1) {

}

@Override
public void onFinished() {

}

@Override
public void onSuccess(String arg0) {
Toast.makeText(getApplicationContext(), arg0, Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.btn_load:
// 圖片上傳地址
String urlLoad = "";
Map<String, Object> mapLoad = new HashMap<>();
// 傳入自己的相應引數
// map.put(key, value);
// map.put(key, value);
HttpxUtils.UpLoadFile(urlLoad, mapLoad, new CommonCallback<String>() {

@Override
public void onCancelled(CancelledException arg0) {

}


@Override
public void onError(Throwable arg0, boolean arg1) {

}

@Override
public void onFinished() {

}

@Override
public void onSuccess(String arg0) {
Toast.makeText(getApplicationContext(), arg0, Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.btn_down:
// 檔案下載地址
String urlDown = "";
// 檔案儲存在本地的路徑
String filepath = "";
HttpxUtils.DownLoadFile(urlDown, filepath, new CommonCallback<String>() {

@Override
public void onCancelled(CancelledException arg0) {

}


@Override
public void onError(Throwable arg0, boolean arg1) {

}

@Override
public void onFinished() {

}

@Override
public void onSuccess(String arg0) {
Toast.makeText(getApplicationContext(), arg0, Toast.LENGTH_SHORT).show();
}
});
break;
case R.id.btn_get_xml:
String url = "http://flash.weather.com.cn/wmaps/xml/china.xml";
HttpxUtils.Get(url, null, new CommonCallback<String>() {

@Override
public void onCancelled(CancelledException arg0) {

}

@Override
public void onError(Throwable arg0, boolean arg1) {

}

@Override
public void onFinished() {

}

@Override
public void onSuccess(String arg0) {
try {
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser xmlPullParser = factory.newPullParser();
xmlPullParser.setInput(new StringReader(arg0));
int eventType = xmlPullParser.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT) {
switch (eventType) {
case XmlPullParser.START_TAG:
String nodeName = xmlPullParser.getName();
if ("city".equals(nodeName)) {
String pName = xmlPullParser.getAttributeValue(0);
Toast.makeText(getApplicationContext(), pName, Toast.LENGTH_SHORT).show();
}
break;
}
eventType = xmlPullParser.next();
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
break;
}
}
}