Android笔记

Android Retrofit框架的简单应用

2019-07-23  本文已影响0人  Cedric_h

原文: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加入网络访问权限

<uses-permission android:name="android.permission.INTERNET"/>

编写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<GetIpInfoResponse>//回调、传入
    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

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.xyz.retrofit.MainActivity">


    <Button
        android:id="@+id/b1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tx1"
        android:layout_below="@id/b1"
        android:text="" />

</RelativeLayout>

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<GetIpInfoResponse> call=apiService.getIpInfo(ip);
                call.enqueue(new Callback<GetIpInfoResponse>() {
                    @Override
                    public void onResponse(Call<GetIpInfoResponse> call, Response<GetIpInfoResponse> 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<GetIpInfoResponse> call, Throwable t) {
                        Toast.makeText(getApplicationContext(),t.getMessage(),Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });

        
    }
}
上一篇下一篇

猜你喜欢

热点阅读