Retrofit使用一
来源:互联网 发布:spc数据输出 编辑:程序博客网 时间:2024/05/17 17:42
Gradle设置
compile 'com.squareup.retrofit2:retrofit:2.2.0'compile 'com.squareup.retrofit2:converter-gson:2.0.2'1,定义bean2,定义接口类public class RepositoryBean { String full_name; String html_url; int contributions; @Override public String toString() { return full_name + " (" + contributions + ")"; }}
public interface GitHubService { @GET("orgs/{orgName}/repos") Call<List<RepositoryBean>> queryOrgRepos( @Path("orgName") String orgName); Retrofit retrofit = new Retrofit.Builder() .baseUrl("https://api.github.com/") .addConverterFactory(GsonConverterFactory.create()) .build();}
3,调用
package com.screenreocder.lostjason.networktest.retrofit;/** * Created by LostboyJason on 2017/5/2. */import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.TextView;import com.screenreocder.lostjason.networktest.R;import org.reactivestreams.Subscriber;import org.reactivestreams.Subscription;import java.util.Arrays;import java.util.List;import io.reactivex.Observable;import io.reactivex.schedulers.Schedulers;import retrofit2.Call;import retrofit2.Callback;import retrofit2.Response;import retrofit2.Retrofit;import static com.screenreocder.lostjason.networktest.retrofit.GitHubService.retrofit;public class MainActivity extends AppCompatActivity { public List<RepositoryBean> getContributorList() throws Exception{ GitHubService gitHubService = GitHubService.retrofit.create(GitHubService.class); Call<List<RepositoryBean>> call = gitHubService.queryOrgRepos("guchuanhangOrganization"); List<RepositoryBean> result = call.execute().body(); return result; } public void getContributorListA() throws Exception { GitHubService gitHubService = GitHubService.retrofit.create(GitHubService.class); Call<List<RepositoryBean>> call = gitHubService.queryOrgRepos("hub_url"); call.enqueue(new Callback<List<RepositoryBean>>() { @Override public void onResponse(Call<List<RepositoryBean>> call, Response<List<RepositoryBean>> response) { List<RepositoryBean> conList=response.body();// Type conListType=new TypeToken<List<Contributor>>(){}.getType();// Gson gson=new Gson();// String resultString= gson.toJson(conList,conListType); RepositoryBean[] myArray = conList.toArray(new RepositoryBean[0]); tv.setText(Arrays.toString(myArray)); } @Override public void onFailure(Call<List<RepositoryBean>> call, Throwable t) { tv.setText(t.getLocalizedMessage()); } }); }TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv= (TextView) findViewById(R.id.tvvalue); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { try{ getContributorListA(); }catch (Exception e){ e.printStackTrace(); } } }); }}
0 0
- Retrofit使用教程(一)
- Retrofit 使用 一
- Retrofit使用教程(一)
- Retrofit使用教程(一)
- Retrofit的使用【一】
- Retrofit使用一
- Retrofit的使用教程(一)
- Android Retrofit使用(一)
- Retrofit使用教程(一)- Retrofit入门详解
- Retrofit使用教程(一)- Retrofit入门详解
- Retrofit使用一:超简单先用上
- Android Retrofit使用教程(一)
- Retrofit的使用详解(一)
- RxJava+Retrofit+OkHttp 懒人方式使用一
- RxJava+Retrofit+OkHttp 懒人方式使用一
- Retrofit的使用教程(一) 1.9
- Retrofit的简单使用(一)
- retrofit<一>
- 用static关键字修饰类
- 我整理的java开源项目
- Scene
- 移动端判断横屏竖屏
- Python 基础 —— 装饰器(1)
- Retrofit使用一
- Android 电话状态的监听
- static作用(修饰函数、局部变量、全局变量)
- wamp 默认别人机器不能访问自己项目 3.0版本没有直接切换在线状态
- Android各控件Demo下载地址各种ui效果图
- Scheduled注解 定时任务 cron表达式详解
- C++ 获取文件版本
- 特征值分解 & 奇异值分解(SVD)
- linux命令之grep