Giter VIP home page Giter VIP logo

rx_retrofit_demo's Introduction

Rx_Retrofit_Demo

Retrofit的例子说明。

    package com.example.chengmengzhen.rx_retrofit_demo;

    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
  
    import com.example.chengmengzhen.rx_retrofit_demo.bean.Contributor;
    import com.example.chengmengzhen.rx_retrofit_demo.url.GitHubUrl;

    import java.util.List;
    
    import retrofit.Call;
    import retrofit.Callback;
    import retrofit.GsonConverterFactory;
    import retrofit.Response;
    import retrofit.Retrofit;


    public class MainActivity extends AppCompatActivity {
        public static final String API_URL = "https://api.github.com/";
        private Call<List<Contributor>> call;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            /******************************************************************************************************
             *      在Retrofit 1.9中,GsonConverter 包含在了package 中而且自动在RestAdapter创建的时候被初始化。
             *      这样来自服务器的son结果会自动解析成定义好了的Data Access Object(DAO)
             *
             *      但是在Retrofit 2.0中,Converter 不再包含在package 中了。
             *      你需要自己插入一个Converter 不然的话Retrofit 只能接收字符串结果。
             *      同样的,Retrofit 2.0也不再依赖于Gson 。
             *
             *      如果你想接收json 结果并解析成DAO,你必须把Gson Converter 作为一个独立的依赖添加进来。
             *
             *      compile 'com.squareup.retrofit:converter-gson:2.0.0-beta1'
             *****************************************************************************************************/
            // 如果你的公司用的服务器API_URL不会变,可以考虑封装起来
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl(API_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
    
            // 构建url框架
            GitHubUrl gitHubUrl = retrofit.create(GitHubUrl.class);
    
            // 给框架填入数据,成为一个完整的url
    
            call = gitHubUrl.contributors("square", "retrofit");
    
            /*****************************发送同步请求*****************************************
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    try {
                        Response<List<Contributor>> response = call.execute();
                        List<Contributor> contributors = response.body();
                        for (Contributor contributor : contributors) {
                            Log.e("MainActivity", contributor.login + " (" + contributor.contributions + ")");
                        }
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
    
                }
            }.start();
            *********************************************************************************/
    
            // 发送url请求后的回调  异步请求
            call.enqueue(new Callback<List<Contributor>>() {
                /****************************************************************************
                 *
                 * 在Retrofit 1.9中,如果获取的 response 不能背解析成定义好的对象,则会调用failure。
                 * 但是在Retrofit 2.0中,不管 response 是否能被解析。onResponse总是会被调用。
                 * 但是在结果不能被解析的情况下,response.body()会返回null。别忘了处理这种情况。
                 *
                 ****************************************************************************/
                @Override
                public void onResponse(Response<List<Contributor>> response, Retrofit retrofit) {
                    List<Contributor> contributors = response.body();
                    for (Contributor contributor : contributors) {
                        Log.e("MainActivity", contributor.login + " (" + contributor.contributions + ")");
                    }
                }
    
                @Override
                public void onFailure(Throwable t) {
                    Log.e("MainActivity", t.toString());
                }
            });
        }
    
        @Override
        protected void onDestroy() {
            super.onDestroy();
            call.cancel();
        }
    }

rx_retrofit_demo's People

Contributors

chengcnaplex avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.