Android Retrofit框架的简单应用

原文:https://blog.csdn.net/uyy203/article/details/52455702

Retrofit:Square提供的开源产品,为Android平台的应用提供一个类型安全的REST客户端

  1. https://github.com/square/retrofit

RxAndroid:响应式编程框架RxJava针对Android平台的扩展。

  1. https://github.com/ReactiveX/RxAndroid
  2. https://github.com/ReactiveX/RxJava

Dagger2:依赖注入(或叫ioc,用过Spring的都知道这东东)框架,用于解耦的。

  1. https://github.com/google/dagger/

通过

  1. http://ip.taobao.com/service/getIpInfo.php?ip=218.19.99.83

获得json数据

在app的gradle中加入依赖包

compile 'com.squareup.retrofit2:retrofit:2.0.0-beta4'//Retrofit2所需要的包
compile 'com.squareup.retrofit2:converter-gson:2.0.0-beta4'//ConverterFactory的Gson依赖包
compile 'com.squareup.retrofit2:converter-scalars:2.0.0-beta4'//ConverterFactory的String依赖包

在AndroidManifest.xml加入网络访问权限


编写API服务

package com.example.xyz.retrofit.Response;

import retrofit2.Call;
import retrofit2.http.GET;
import retrofit2.http.Query;

/**
 * Created by xyz on 2016/9/5.
 */
public interface ApiService {

    @GET("service/getIpInfo.php")//请求目录
    Call//回调、传入
    getIpInfo(@Query("ip") String ip);//url传值key和value

}

定义接受数据的类GetIpInfoResponse

package com.example.xyz.retrofit.Response;
import com.example.xyz.retrofit.model.IpInfo;

/**
 * Created by xyz on 2016/9/5.
 */
public class GetIpInfoResponse {
    public int code;
    public IpInfo data;
}

IpInfo

package com.example.xyz.retrofit.model;

/**
 * Created by xyz on 2016/9/6.
 */
public class IpInfo {
    public String country;
    public String Country_id;
    public String area;
    public String area_id;
    public String ip;
    public String city;
    public String isp;

}

activity_main.xml





    

MainActivity.java

package com.example.xyz.retrofit;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.example.xyz.retrofit.Response.ApiService;
import com.example.xyz.retrofit.Response.GetIpInfoResponse;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    private Button b1;
    private TextView tx1;
    private String enpoint="http://ip.taobao.com";
    private String ip="218.19.99.83";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        b1=(Button) findViewById(R.id.b1);
        tx1=(TextView) findViewById(R.id.tx1);

        b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Retrofit retrofit=new Retrofit.Builder()//实例化retrofit
                        .baseUrl(enpoint)
                        .addConverterFactory(GsonConverterFactory.create())//读取GSON
                        .build();

                ApiService apiService=retrofit.create(ApiService.class);//指定retrofit的api服务

                Call call=apiService.getIpInfo(ip);
                call.enqueue(new Callback() {
                    @Override
                    public void onResponse(Call call, Response response) {
                        GetIpInfoResponse getIpInfoResponse=response.body();//将GSON数据写入GetIpInfoResponse
                        tx1.setText(getIpInfoResponse.data.country);
                        tx1.append(",");
                        tx1.append(getIpInfoResponse.data.area);
                        tx1.append(",");
                        tx1.append(getIpInfoResponse.data.city);
                        tx1.append(",");
                        tx1.append(getIpInfoResponse.data.isp);
                        tx1.append(",code="+getIpInfoResponse.code);

                    }

                    @Override
                    public void onFailure(Call call, Throwable t) {
                        Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        
    }
}

你可能感兴趣的:(Android Retrofit框架的简单应用)