2015-09-24 50 views
3

由於我對使用Android的第三方HTTP庫(如Retrofit)非常陌生,因此有幾個問題想問問誰能幫助我。在Android中使用Retrofit 2.0

最近似乎有一個很大的改變,從翻新1.9到2.0的過渡,例如API的變化。所以我找不到使用該庫的適當文件,即使在它自己的網頁上。

在實現向服務器註冊用戶的HTTP請求時,根據網頁,首先要創建一個定義角色的接口(例如POST,GET等)。例如:

public interface GitHubService { 
    @GET("https://stackoverflow.com/users/{user}/repos") 
    Call<List<Repo>> listRepos(@Path("user") String user); 
} 

而且似乎我應該創建一個改造實例連接到服務器,並使用

Retrofit retrofit = new Retrofit.Builder() 
    .baseUrl("https://api.github.com") 
    .build(); 

GitHubService service = retrofit.create(GitHubService.class); 

的事情,我真的想找出是,

  1. 上述仿製藥中的Repo是什麼意思?這是我仍然無法弄清楚的第一件事。

  2. 什麼應該寫在括號後面跟@POST或@GET這樣的每個註釋?它是否意味着服務器URL下的子目錄?根據上面的示例代碼,@GET註解是否聲明listRepos方法從「用戶」路徑獲取用戶值?這是如此令人困惑......

請,我真的是新來的這個世界,所以我絕望的幫助你。提前致謝。

+0

觀看[此談話第一(https://www.youtube.com/watch?v=aEuNBk1b5OE#t=2480)(約改造的1.x),然後[這個談話]( https://www.youtube.com/watch?v=KIAoQbAu3eA&feature=youtu.be)(關於Retrofit 2.x)是改造創作者之一的傑克沃頓。 – nhaarman

+0

請參閱http://stackoverflow.com/a/41015925/3470479 – PrakhaRaM

回答

3
  1. 本示例中的'回購'表示Github上的存儲庫。 Retrofit會自動反序列化對Java POJO的JSON響應。該示例從Github API獲取有關Github存儲庫的信息,獲取的存儲庫信息由Repo類/一個Repo對象列表表示。你將不得不提供你自己的類表示你從服務器/ API獲得的數據。
  2. 它是指服務器URL下的子目錄嗎?

基本上,是的。這是您嘗試在服務器上訪問的資源的路徑/ URI,用baseUrl指定。我們有baseUrlhttps://api.github.com。我們追加路徑/users/{user}/repos。該方法

@GET("https://stackoverflow.com/users/{user}/repos") 
     Call<List<Repo>> listRepos(@Path("user") String user); 

需要用戶ID作爲參數,並用該參數替換{user}

所以,如果你調用該方法與參數JakeWharton完整URI是

https://api.github.com/users/JakeWharton/repos 

您可以從瀏覽器中調用它來查看響應。 您必須更改這些Url/Uri字符串以匹配您想要訪問的API。

0

及其工作

enter image description here 包com.keshav.gmailretrofitexampleworking.network;

import retrofit2.Retrofit; 
import retrofit2.converter.gson.GsonConverterFactory; 

public class ApiClient { 
    public static final String BASE_URL = "http://api.androidhive.info/json/"; 
    private static Retrofit retrofit = null; 

    public static Retrofit getClient() { 
     if (retrofit == null) { 
      retrofit = new Retrofit.Builder() 
        .baseUrl(BASE_URL) 
        .addConverterFactory(GsonConverterFactory.create()) 
        .build(); 
     } 
     return retrofit; 
    } 
} 
============================================== 
package com.keshav.gmailretrofitexampleworking.network; 

import com.keshav.gmailretrofitexampleworking.models.Message; 

import java.util.List; 

import retrofit2.Call; 
import retrofit2.http.GET; 

public interface ApiInterface { 
    @GET("inbox.json") 
    Call<List<Message>> getInbox(); 
} 

=========================================== ==

呼叫APi的

private void getInbox() { 
    swipeRefreshLayout.setRefreshing(true); 

    ApiInterface apiService = 
      ApiClient.getClient().create(ApiInterface.class); 

    Call<List<Message>> call = apiService.getInbox(); 
    call.enqueue(new Callback<List<Message>>() { 
     @Override 
     public void onResponse(Call<List<Message>> call, Response<List<Message>> response) { 
      // clear the inbox 
      messages.clear(); 

      // add all the messages 
      // messages.addAll(response.body()); 

      // TODO - avoid looping 
      // the loop was performed to add colors to each message 

      Log.e("keshav","response" +response.body()); 

      for (Message message : response.body()) { 
       // generate a random color 

       // TODO keshav Generate Random Color Here 
       message.setColor(getRandomMaterialColor("400")); 
       messages.add(message); 
      } 

      mAdapter.notifyDataSetChanged(); 
      swipeRefreshLayout.setRefreshing(false); 
     } 

     @Override 
     public void onFailure(Call<List<Message>> call, Throwable t) { 
      Toast.makeText(getApplicationContext(), "Unable to fetch json: " + t.getMessage(), Toast.LENGTH_LONG).show(); 
      swipeRefreshLayout.setRefreshing(false); 
     } 
    }); 
} 

編譯 'com.google.code.gson:GSON:2.6.2'

compile 'com.squareup.retrofit2:retrofit:2.0.2' 

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

源代碼

https://drive.google.com/open?id=0BzBKpZ4nzNzUVFRnVVkzc0JabUU

https://drive.google.com/open?id=0BzBKpZ4nzNzUc2FBdW00WkRfWW8

相關問題