原文:https://blog.csdn.net/uyy203/article/details/52455702
Retrofit:Square提供的開源產(chǎn)品,為Android平臺的應(yīng)用提供一個(gè)類型安全的REST客戶端
RxAndroid:響應(yīng)式編程框架RxJava針對Android平臺的擴(kuò)展例证。
Dagger2:依賴注入(或叫ioc,用過Spring的都知道這東東)框架路呜,用于解耦的。
通過
獲得json數(shù)據(jù)
在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加入網(wǎng)絡(luò)訪問權(quán)限
<uses-permission android:name="android.permission.INTERNET"/>
編寫API服務(wù)
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>//回調(diào)织咧、傳入
getIpInfo(@Query("ip") String ip);//url傳值key和value
}
定義接受數(shù)據(jù)的類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()//實(shí)例化retrofit
.baseUrl(enpoint)
.addConverterFactory(GsonConverterFactory.create())//讀取GSON
.build();
ApiService apiService=retrofit.create(ApiService.class);//指定retrofit的api服務(wù)
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數(shù)據(jù)寫入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();
}
});
}
});
}
}