安卓Retrofit post请求 带参数

时间:2024-03-13 19:26:33


写个这玩意可费事 ,我是安卓开发的小白我叫菜鸟,欢迎大家能点开我的博客。在这里我们共同成长,最近没事写了这玩意,网上一大堆,我也看不懂。自己试着写了个,来起来就直接复制粘贴的型的代码,希望给像我这样懒得人带来一股清流,废话不多说,开始搞起来:


1.先加个依赖:

   compile 'com.squareup.retrofit2:retrofit:2.1.0'
   compile 'com.squareup.retrofit2:converter-gson:2.1.0'
   compile 'com.squareup.retrofit2:converter-scalars:2.1.0'
   compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
   compile 'io.reactivex.rxjava2:rxjava:2.1.0'
   compile 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
	
       位置请看图:

安卓Retrofit post请求 带参数

2.创建实例:


         Retrofit retrofit = new Retrofit.Builder()
        .baseUrl("你的Url")
        //增加返回值为String的支持
        .addConverterFactory(ScalarsConverterFactory.create())
        //增加返回值为Gson的支持(以实体类返回)
        .addConverterFactory(GsonConverterFactory.create())
        .build();

   3.创建接口

        BlueService requestSerives = retrofit.create(BlueService.class);//这里采用的是Java的动态代理模式

    4.你不得给人家创建出来:

	

    public interface BlueService {

    @FormUrlEncoded
    @POST("a/b")//拼接的url 
    Call<String> getString(@Field("参数1") String loginname,
                           @Field("参数2") String nloginpwd);
}

   5.给上面的参数值:

   Call<String> call = requestSerives.getString("pixiu", "android");//传入我们请求的键值对的值
6.请求回调:

  Call<String> call = requestSerives.getString("pixiu", "android");//传入我们请求的键值对的值

    call.enqueue(new Callback<String>() {
        @Override
        public void onResponse(Call<String> call, Response<String> response) {
            Log.e("===", "return:" + response.body().toString());
            Toast.makeText(MainActivity.this, "" + response, Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onFailure(Call<String> call, Throwable t) {
            Log.e("===", "失败");
        }
    });
}

    没了  因为懒


  项目已经上传不懂自己看:

http://download.csdn.net/download/qq_36078915/10155617



谢谢支持 希望你开发如风一样! 爱你的 GZM 小菜鸟