android retrofit post

1.依赖
 compile 'com.squareup.retrofit2:retrofit:2.0.1'
    compile 'com.squareup.retrofit2:converter-gson:2.0.1'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
    compile 'io.reactivex:rxandroid:1.1.0'
    compile 'com.squareup.okhttp3:logging-interceptor:3.4.1'

2.Api

public class API {
    public static final String PAGE_URL = "https://api.tianapi.com/wxnew/";
}

3.Apiserver

public interface ApiService {
    @POST
    Observable<PageBean> postpage(@Url String url, @QueryMap Map<String,String> map);
}

4.main函数

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        postHome(API.PAGE_URL,"2","10");
    }
    public void postHome(String url,String page,String num){

        Map<String,String> map=new HashMap<>();
        map.put("key","8d6e3228d25298f13af4fc40ce6c9679");
        map.put("num",num);
        map.put("page",page);
        Retrofit retrofit = new Retrofit.Builder().baseUrl(url).addConverterFactory(GsonConverterFactory.create()).addCallAdapterFactory(RxJavaCallAdapterFactory.create()).build();

        ApiService apiService = retrofit.create(ApiService.class);

        Observable<PageBean> postpages = apiService.postpage(url,map);

        postpages.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<PageBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(PageBean pageBean) {
                Toast.makeText(MainActivity.this, ""+pageBean.getNewslist().get(1).getTitle().toString(), Toast.LENGTH_SHORT).show();

            }
        });
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值