retrofit的简单使用

来源:互联网 发布:中文calypso软件安装 编辑:程序博客网 时间:2024/06/05 20:59

public interface APIService {
    @GET("weather")
    Call<WeatherBean> loadWeather(@Query("cityname") String cityname,@Query("key") String apiKey);


    @GET("query")
    Call<KuaidiBean> loadKuaidi(@Query("type") String type,@Query("postid") String postid);

    @GET("weather")
    Observable<WeatherBean> getWeatherData(@Query("cityname") String cityname, @Query("key") String apiKey);
}

 

 

 

package com.example.testmodel;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;

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

public class MainActivity extends AppCompatActivity {

 //   private String baseUrl = "http://apis.haoservice.com/";
    private String key = "8d1c763f9d214afa90e9e3cc23d287dd";

    private String baseUrl2="http://www.kuaidi100.com/";
    private String postId="3342435893144";

    private TextView tvInfo;

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

        tvInfo = (TextView) findViewById(R.id.tvInfo);
        //getWeather();
        getKuaidi();

    }
    public void getKuaidi() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(baseUrl2)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        APIService service=retrofit.create(APIService.class);
        service.loadKuaidi("shentong",postId).enqueue(new Callback<KuaidiBean>() {
            @Override
            public void onResponse(Call<KuaidiBean> call, Response<KuaidiBean> response) {
                String res="";
                for (int i = 0; i < response.body().getData().size(); i++) {
                    res+=response.body().getData().get(i).getFtime()+"---"+response.body().getData().get(i).getContext()+"\n";
                }
                tvInfo.setText(""+res);
            }

            @Override
            public void onFailure(Call<KuaidiBean> call, Throwable t) {

            }
        });
    }

    public void getWeather() {
        Retrofit retrofit=new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        APIService service=retrofit.create(APIService.class);
        service.loadWeather("北京",key).enqueue(new Callback<WeatherBean>() {
            @Override
            public void onResponse(Call<WeatherBean> call, Response<WeatherBean> response) {
                WeatherBean wb=response.body();
                Log.i("+++++", "onResponse: "+wb.getReason());
                tvInfo.setText(wb.getReason());
            }

            @Override
            public void onFailure(Call<WeatherBean> call, Throwable t) {

            }
        });
    }


}

 

 

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    compile 'com.squareup.okhttp3:okhttp:3.9.0'
    compile 'com.squareup.retrofit2:retrofit:2.3.0'
    compile 'com.squareup.retrofit2:converter-gson:2.3.0'
    compile 'io.reactivex.rxjava2:rxjava:2.1.6'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.3.0'
}