網絡請求框架

retrofit_okhttp

public class Model {
    //使用前提:必須添加4個依賴庫
     Retrofit retrofit;
    //服務器地址
    public static final String BASE_URL = "http://....com";
    public static final String ID = "44297";
    public static final String SIGN = "cc8738fb83254674b4b1e169548ce8b9";
    //回調接口,通知”外面“,裏面接口調用抽象方法。外面實現該接口
    public interface BeanCallback<T> {
        void onSuccess(T bean);
        void onError(String message);
    }
    /**單例模式**/
    private Model() {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)                .addConverterFactory(GsonConverterFactory.create())
                //請求bean對象
                //請求String則是ScalarsConverterFactory
                .build();
    }
    private static Model instance= new Model();
    public static Model getInstance() {
        return instance;
    }

    //請求接口
    public void getType(final BeanCallback<第一次先寫String,第二次寫Bean> callback) {
        //使用retorfit請求接口
        Call<分兩次> call = retrofit.create(接口註解.class).getType(ID, SIGN);
        call.enqueue(new Callback<分兩次>() {
            @Override
            public void onResponse(Call<分兩次> call, Response<分兩次> response) {
    //第一次
    //Log.e("TAG", "--------" + response.body().toString());
    //第二次(如果是JsonObject系統自動轉)
                TypeBean bean = response.body();
                /**如果是JsonArray,自己手動轉
                Gson gson = new Gson();
                String result = response.body();
                List<bean> list = gson.fromJson(result, new TypeToken<List<bean>>() {}.getType());**/
                if (bean.getShowapi_res_code() == 0)
                    callback.onSuccess(bean);
                else
                callback.onError(bean.getShowapi_res_error());
            }
            @Override
            public void onFailure(Call<TypeBean> call, Throwable t) {
                callback.onError("網絡請求失敗!");
            }
        });
    }

兩種模式:
MVC:單例.getType
MVP:new Presenter————–model通知presenter———-presenter通知ui
okhttp請求到數據,修改ui———必須runOnUiThread
retrofit請求到數據,直接修改Ui

/**接口註解**/
public interface GirlService {

    //類型接口
    String TYPE_URL = "/1208-1";
    //根據ID 獲取圖片集
    String PICLIST_BY_ID = "/1208-2";
    //根據圖片集 列舉所有圖片
    String ALLPIC_BY_LIST = "/1208-3";


    //http:
    // 請求行    請求方法(GET POST)  url  http1.1
    // 請求頭    正文的格式 json,xml,鍵值對,流
    // 請求正文


    //GET請求 獲取 類型接口
    @GET(TYPE_URL)
    //Get的參數 使用Query來標記
    Call<TypeBean> getType(@Query("showapi_appid") String showApi_id, @Query("showapi_sign") String showApi_sign);


    //使用Post來請求 根據類型id獲取圖片集
    @POST(PICLIST_BY_ID) //  @Multipart帶文件 使用Part
    //  @FormUrlEncoded  不帶文件使用Field
    //標明 內容類型
    @FormUrlEncoded
    //使用鍵值對
    Call<ListBean> getListByTypeId(@Field("showapi_appid") String showApi_id, @Field("showapi_sign") String showApi_sign,
                                   @Field("type") int type, @Field("page") int page, @Field("rows") int rows);


    @POST(ALLPIC_BY_LIST)
    @FormUrlEncoded
    Call<PicBean> getAllList(@Field("showapi_appid") String showApi_id, @Field("showapi_sign") String showApi_sign, @Field("id") String id);


//    @POST("dizhi")
//    @Multipart
//    Call<String> test(@Part MultipartBody.Part file, @Part("username") String username, @Part("password") String password);
}

xutils


        1. xutils 初始化
        2. 設置請求參數       RequestParams
        3. 開始請求         x.http().post
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章