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,定义bean
public class RepositoryBean {    String full_name;    String html_url;    int contributions;    @Override    public String toString() {        return full_name + " (" + contributions + ")";    }}

2,定义接口类

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