Android中網絡框架Retrofit2.0簡單使用

在Andrroid開發中,網絡請求十分常用
而在Android網絡請求庫中,Retrofit是當下最熱的一個網絡請求庫

依賴

compile 'com.squareup.retrofit2:retrofit:2.0.2' // Retrofit庫

compile 'com.squareup.okhttp3:okhttp:3.1.2' // Okhttp庫

compile 'com.squareup.retrofit2:converter-gson:2.0.2'

創建一個接口

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Path;

public interface APIInterface {
    //接口:https://api.github.com/users/Guolei1130
    //注意導包不要導錯 看上面包的類型
    //第一種方式 在有解析類Bean的情況下 泛型裏填解析類Bean
    //接口定義:註解方式添加請求方式:get請求內部放拼接的連接和需要傳遞的參數
    //如果不需要傳遞參數 註解裏也要打上"/"或"."
    @GET("users/{username}")//username相當於一個名字 會把網址參數傳進這個名字裏    
    Call<LoginBean> getLogin(@Path("username")String user);//Path此註解裏的參數要和Get註解裏的名字參數相同 才能匹配 

    //第二種方式 沒有解析類Bean的情況下 泛型裏填ResponseBody
    @GET("users/{username}")
    Call<ResponseBody> getLoginInfo(@Path("username")String user);

MainActivity


import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofit = new     Retrofit.Builder().baseUrl("https://api.github.com")//baseURL
                .addConverterFactory(GsonConverterFactory.create()).build();
        APIInterface apiInterface = retrofit.create(APIInterface.class);//獲取請求接口實例

        //第一種有解析類Bean的情況下
        /*Call<LoginBean> call = apiInterface.getLogin("Guolei1130");
        call.enqueue(new Callback<LoginBean>() {
            @Override//主線程
            public void onResponse(Call<LoginBean> call, Response<LoginBean> response) {
                String login = response.body().login;
                Toast.makeText(MainActivity.this, login, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call<LoginBean> call, Throwable t) {

            }*/


        //第二種沒有解析類 ResponseBody自動解析
        Call<ResponseBody> call = apiInterface.getLoginInfo("Guolei1130");
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                try {
                    Toast.makeText(MainActivity.this, response.body().string(), Toast.LENGTH_SHORT).show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }
        });
        //同步請求,必須在子線程
   //  Response<LoginBean> =  call.execute();
    }

這裏寫圖片描述
詳細請看
http://blog.csdn.net/carson_ho/article/details/73732076

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章