Retrofit2的get请求和post请求

准备工作

<uses-permission android:name="android.permission.INTERNET"/>
 compile 'com.squareup.retrofit2:retrofit:2.0.2'
 compile 'com.squareup.retrofit2:converter-gson:2.0.2'

定义

package com.github.c.retrofit2demo;

import retrofit2.Call;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.http.Field;
import retrofit2.http.FormUrlEncoded;
import retrofit2.http.GET;
import retrofit2.http.POST;
import retrofit2.http.Query;

/**
 * Created by a on 2016/5/19.
 */
public class AppStores {
//    请求地址 : http://ip.taobao.com/service/getIpInfo.php?ip=202.202.32.202

//    1.定义一个接口
    public interface TaobaoIPService {
//    1.1.get请求:通过@GET注解,指明访问的地址
        @GET("getIpInfo.php")//★这里最前面不能带“/”
//    1.2.定义一个请求网络,并且返回结果的方法(方法返回Call ),
//      通过@Query指定key,后面跟上value
        Call getIP(@Query("ip") String ip);
/*********************************************************************************/
//    1.3.post请求:通过@FormUrlEncoded、和@POST注解,指明访问的地址
//        (★注意:千万别忘了@FormUrlEncoded)
        @FormUrlEncoded
        @POST("getIpInfo.php")
//    1.4.通过@Field来指定key,后面跟上value
        Call postIP(@Field("ip") String ip);
    }

//    2.实例化retrofit,配置好请求地址和解析方式
    static Retrofit retrofit = new Retrofit.Builder()
            .baseUrl("http://ip.taobao.com/service/")//★这里最后面必须能带“/”
            .addConverterFactory(GsonConverterFactory.create())//设置将json解析为javabean所用的方式
            .build();
//    3.通过retrofit创建第一步定义的接口的实例,
//      供在外部直接通过该实例调用该接口的getIPad方法,完成网络请求
    static AppStores.TaobaoIPService taobaoIPService =
            retrofit.create(AppStores.TaobaoIPService.class);
}

使用

package com.github.c.retrofit2demo;

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

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity {

    private TextView tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv = (TextView) findViewById(R.id.tv);

//        1.调用getIP(或者postIP)方法得到Call
//        Call call = AppStores.taobaoIPService.getIP("202.202.32.202");
        Call call = AppStores.taobaoIPService.postIP("202.202.32.202");
//        2.call.enqueue开启异步网络请求
        call.enqueue(new Callback() {
            @Override
            public void onResponse(Call call, Response response) {
                IP body = response.body();
                String result = body.getData().toString();
//                3.可以直接更改UI,因为onResponse方法已经在UI线程中
                tv.setText(result);
//                4.取消请求
                call.cancel();
                Toast.makeText(MainActivity.this,call.isCanceled()+"",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onFailure(Call call, Throwable t) {
                tv.setText(t.toString());
                t.printStackTrace();
            }
        });



    }
}

补充上面的javabean—IP

/**
 * Created by a on 2016/5/18.
 */
public class IP {

    /**
     * code : 0
     * data : {"country":"中国","country_id":"CN","area":"西南","area_id":"500000","region":"重庆市","region_id":"500000","city":"重庆市","city_id":"500000","county":"","county_id":"-1","isp":"教育网","isp_id":"100027","ip":"202.202.33.33"}
     */

    private int code;
    /**
     * country : 中国
     * country_id : CN
     * area : 西南
     * area_id : 500000
     * region : 重庆市
     * region_id : 500000
     * city : 重庆市
     * city_id : 500000
     * county :
     * county_id : -1
     * isp : 教育网
     * isp_id : 100027
     * ip : 202.202.33.33
     */

    private DataBean data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public DataBean getData() {
        return data;
    }

    public void setData(DataBean data) {
        this.data = data;
    }

    public static class DataBean {
        private String country;
        private String country_id;
        private String area;
        private String area_id;
        private String region;
        private String region_id;
        private String city;
        private String city_id;
        private String county;
        private String county_id;
        private String isp;
        private String isp_id;
        private String ip;

        @Override
        public String toString() {
            return "DataBean{" +
                    "country='" + country + '\'' +
                    ", country_id='" + country_id + '\'' +
                    ", area='" + area + '\'' +
                    ", area_id='" + area_id + '\'' +
                    ", region='" + region + '\'' +
                    ", region_id='" + region_id + '\'' +
                    ", city='" + city + '\'' +
                    ", city_id='" + city_id + '\'' +
                    ", county='" + county + '\'' +
                    ", county_id='" + county_id + '\'' +
                    ", isp='" + isp + '\'' +
                    ", isp_id='" + isp_id + '\'' +
                    ", ip='" + ip + '\'' +
                    '}';
        }

        public String getCountry() {
            return country;
        }

        public void setCountry(String country) {
            this.country = country;
        }

        public String getCountry_id() {
            return country_id;
        }

        public void setCountry_id(String country_id) {
            this.country_id = country_id;
        }

        public String getArea() {
            return area;
        }

        public void setArea(String area) {
            this.area = area;
        }

        public String getArea_id() {
            return area_id;
        }

        public void setArea_id(String area_id) {
            this.area_id = area_id;
        }

        public String getRegion() {
            return region;
        }

        public void setRegion(String region) {
            this.region = region;
        }

        public String getRegion_id() {
            return region_id;
        }

        public void setRegion_id(String region_id) {
            this.region_id = region_id;
        }

        public String getCity() {
            return city;
        }

        public void setCity(String city) {
            this.city = city;
        }

        public String getCity_id() {
            return city_id;
        }

        public void setCity_id(String city_id) {
            this.city_id = city_id;
        }

        public String getCounty() {
            return county;
        }

        public void setCounty(String county) {
            this.county = county;
        }

        public String getCounty_id() {
            return county_id;
        }

        public void setCounty_id(String county_id) {
            this.county_id = county_id;
        }

        public String getIsp() {
            return isp;
        }

        public void setIsp(String isp) {
            this.isp = isp;
        }

        public String getIsp_id() {
            return isp_id;
        }

        public void setIsp_id(String isp_id) {
            this.isp_id = isp_id;
        }

        public String getIp() {
            return ip;
        }

        public void setIp(String ip) {
            this.ip = ip;
        }
    }
}

更多请查看你真的会用Retrofit2吗?Retrofit2完全教程

关于URL拼接规范:

Retrofit2的get请求和post请求_第1张图片

你可能感兴趣的:(网络请求)