xutils網絡框架的最簡單使用

小菜鳥一枚,公司有些需求是也在學習中,希望大神提出寶貴意見,謝謝。

首先是導入依賴包:

compile 'org.xutils:xutils:3.5.0'
接下來是對於網絡接口地址的初始化:
public class XHttpUrL {

  public static final String FATEHER_URL = "-----------接口地址--------";

}

對於網絡接口的調用

public class XHttp {
    //utf-8格式轉換(進行特殊格式轉換調用的方法)
    public static String toUtf8(String str) {
        String result = null;
        try {
            result = URLEncoder.encode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return result;
    }
public static void getFather(String node, Callback.CommonCallback<String> listener) {
////根據後臺確定是否需要轉換  
   //不用轉換的方法   
 x.http().post(new RequestParams(XHttpUrL.FATEHER_URL + node), listener);
//用轉換的方法   
x.http().post(new RequestParams(XHttpUrL.FATEHER_URL + toutf8(node)), listener);
}}
//在方法中調用
首先是在oncreate方法中進行初始化,不然容易空指針
x.Ext.init(getApplication());
然後直接開始調用需要的方法即可
列出幾個例子:::::
普通get方法

HttpUtils http = new HttpUtils();
http.send(HttpRequest.HttpMethod.GET,
    "http://www.lidroid.com",
    new RequestCallBack<String>(){
        @Override
        public void onLoading(long total, long current, boolean isUploading) {
            testTextView.setText(current + "/" + total);
        }

        @Override
        public void onSuccess(ResponseInfo<String> responseInfo) {
            textView.setText(responseInfo.result);
        }

        @Override
        public void onStart() {
        }

        @Override
        public void onFailure(HttpException error, String msg) {
        }
});






使用HttpUtils上傳文件 或者 提交數據 到服務器(post方法)

RequestParams params = new RequestParams();
params.addHeader("name", "value");
params.addQueryStringParameter("name", "value");

// 只包含字符串參數時默認使用BodyParamsEntity,
// 類似於UrlEncodedFormEntity("application/x-www-form-urlencoded")。
params.addBodyParameter("name", "value");

// 加入文件參數後默認使用MultipartEntity("multipart/form-data"),
// 如需"multipart/related",xUtils中提供的MultipartEntity支持設置subType爲"related"。
// 使用params.setBodyEntity(httpEntity)可設置更多類型的HttpEntity(如:
// MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity)。
// 例如發送json參數:params.setBodyEntity(new StringEntity(jsonStr,charset));
params.addBodyParameter("file", new File("path"));
...

HttpUtils http = new HttpUtils();
http.send(HttpRequest.HttpMethod.POST,
    "uploadUrl....",
    params,
    new RequestCallBack<String>() {

        @Override
        public void onStart() {
            testTextView.setText("conn...");
        }

        @Override
        public void onLoading(long total, long current, boolean isUploading) {
            if (isUploading) {
                testTextView.setText("upload: " + current + "/" + total);
            } else {
                testTextView.setText("reply: " + current + "/" + total);
            }
        }

        @Override
        public void onSuccess(ResponseInfo<String> responseInfo) {
            testTextView.setText("reply: " + responseInfo.result);
        }

        @Override
        public void onFailure(HttpException error, String msg) {
            testTextView.setText(error.getExceptionCode() + ":" + msg);
        }
});






發佈了38 篇原創文章 · 獲贊 5 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章