• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Nsikaktopdown/AndelaChallengeProject: GitHub Api using Retrofit in Android

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

Nsikaktopdown/AndelaChallengeProject

开源软件地址:

https://github.com/Nsikaktopdown/AndelaChallengeProject

开源编程语言:

Java 100.0%

开源软件介绍:

#AndelaChallengeProject

Android app to retrieve a list of Java Developers in Lagos using the Github API

Getting Started

You can glone or download this project to your local machine.

Prerequisites

Intermediate Android Experience Little or no experience with [Retrofit] (http://square.github.io/retrofit/) Library [GitHub API ] (https://api.github.com/search/users?q=language:java%20location:lagos)

Dependencies

Obviously to use Retrofit you need this library in your dependencies. You also need a converter used for JSON format : Gson. Parceler is used for serialization and deserialization of objects.

 compile 'com.squareup.retrofit2:converter-gson:2.1.0'
    compile 'com.squareup.retrofit2:retrofit:2.1.0'

GitHub Api Response in JSON

{
  "total_count": 142,
  "incomplete_results": false,
  "items": [
    {
      "login": "moyheen",
      "id": 8110201,
      "avatar_url": "https://avatars1.githubusercontent.com/u/8110201?v=3",
      "gravatar_id": "",
      "url": "https://api.github.com/users/moyheen",
      "html_url": "https://github.com/moyheen",
      .........
    },

What do you need ?

There is a minimum amount of files to write to request a Rest Api, you need at least one interface to write your query, a model to retrieve the api response and a restclient to make your calls.

POJO

you need models to retrieve data from JSON returned by the server. You have to create POJOs, the fields name have to be the same as those in the JSON but you can have different names if you use @SerializedName annotation to specify the name of the field in the JSON. There is Serializable and Parcelable for serialization and deserialization of object.

public class User implements Parcelable {

    @SerializedName("login")
    @Expose
    private String login;
    .......
    }

Service

You have to create an interface called Service here to manage your url calls. In this interface you have to specify the type of the request like POST, GET, PUT, etc. For an asynchronized request you have to add a Callback to your methods and return void.

To make the HTTp request we need to send some paramenters. In this case it is a query so we can also add q query parameters with @Query.

public interface RestApiService {

    @GET("/search/users")
    Call<UserList> getUserList(@Query("q") String filter);

}

you will have to create your restclient object with your services.

```

public RestApiBuilder() { retrofit = new Retrofit.Builder() .baseUrl(BASE_URL) .addConverterFactory(GsonConverterFactory.create()) .build(); }

public RestApiService getService() {
    return retrofit.create(RestApiService.class);
}

```

Screenshot

Authors




鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap