android retrofit实例,Retrofit2简单实例

Retrofit 是什么呢?Rotrofit是square开发的一个jar,便于客户端与服务器的数据交互。下面通过一个简单的例子,给大家起到一个抛砖引玉的作用.

1 在Android Studio中引入Retrofit2

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

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

记得要在AndroidManifest中添加Intent 权限.

2 定义一个Bean,使用Gson完成请求数据与POJO的转换

package com.example.mvgos.retrofit2demo;

public class RepositoryBean {

String full_name;

String html_url;

int contributions;

@Override

public String toString() {

return full_name + " (" + contributions + ")";

}

}

3 定一个Interface,完成Retrofit初始化和封装网络请求涉及的接口

package com.example.mvgos.retrofit2demo;

import java.util.List;

import retrofit2.Call;

import retrofit2.Retrofit;

import retrofit2.converter.gson.GsonConverterFactory;

import retrofit2.http.GET;

import retrofit2.http.Path;

public interface GitHubService {

@GET("orgs/{orgName}/repos")

Call> queryOrgRepos(

@Path("orgName") String orgName);

Retrofit retrofit = new Retrofit.Builder()

.baseUrl("https://api.github.com/")

.addConverterFactory(GsonConverterFactory.create())

.build();

}

4调用接口完成网络请求

//同步调用,Android中不允许在Main Thread中进行网络请求,这里使用下面的一步请求

public List getContributorList() throws Exception{

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

Call> call = gitHubService.queryOrgRepos("guchuanhangOrganization");

List result = call.execute().body();

return result;

}

//异步请求

public void getContributorListA() throws Exception {

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

Call> call = gitHubService.queryOrgRepos("guchuanhangOrganization");

call.enqueue(new Callback>() {

@Override

public void onResponse(Call> call, Response> response) {

List conList=response.body();

}

@Override

public void onFailure(Call> call, Throwable t) {

textView.setText(t.getLocalizedMessage());

}

});

}

5 综合测试

//MainActivity.java

package com.example.mvgos.retrofit2demo;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import java.util.Arrays;

import java.util.List;

import retrofit2.Call;

import retrofit2.Callback;

import retrofit2.Response;

import static com.example.mvgos.retrofit2demo.GitHubService.retrofit;

public class MainActivity extends AppCompatActivity {

public List getContributorList() throws Exception{

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

Call> call = gitHubService.queryOrgRepos("guchuanhangOrganization");

List result = call.execute().body();

return result;

}

public void getContributorListA() throws Exception {

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

Call> call = gitHubService.queryOrgRepos("guchuanhangOrganization");

call.enqueue(new Callback>() {

@Override

public void onResponse(Call> call, Response> response) {

List conList=response.body();

// Type conListType=new TypeToken>(){}.getType();

// Gson gson=new Gson();

// String resultString= gson.toJson(conList,conListType);

RepositoryBean[] myArray = conList.toArray(new RepositoryBean[0]);

textView.setText(Arrays.toString(myArray));

}

@Override

public void onFailure(Call> call, Throwable t) {

textView.setText(t.getLocalizedMessage());

}

});

}

TextView textView;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

Button button = (Button) findViewById(R.id.button);

button.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

textView = (TextView) findViewById(R.id.textView);

try{

getContributorListA();

}catch (Exception e){

e.printStackTrace();

}

}

});

}

}

0818b9ca8b590ca3270a3433284dd417.png

苦于找不到服务器接口进行测试的同学请参考 github api.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值