Android Retrofit网络请求①

public interface API {

    @GET("Login")
    Call<ResponseBody> getNews(@Query("username") String userName, @Query("password") String password);

    @FormUrlEncoded
    @POST("Login")
    Call<ResponseBody> postCheck(@Field("username") String userName, @Field("password") String password);
}
 public void getRetrofit() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)//url:服务器地址
                .build() ;
        API api = retrofit.create(API.class) ;
        Call<ResponseBody> newsCall = api.getNews("xxx" , "xxx" );//参数
        newsCall.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                try {
                    Log.e("TAG" , response.body().string() + "") ;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Log.e("TAG" , t.toString() + "") ;
            }
        });
    }
 public void postRetrofit(){
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .build();
        API api = retrofit.create(API.class) ;
        Call<ResponseBody> call = api.postCheck("xxx" , "xxx" ) ;//参数
        call.enqueue(new Callback<ResponseBody>() {
            @Override
            public void onResponse(Call<ResponseBody> call, retrofit2.Response<ResponseBody> response) {
                try {
                    Log.e("TAG" , response.body().string()+"");
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Call<ResponseBody> call, Throwable t) {
                Toast.makeText(MainActivity.this, ""+t.toString(), Toast.LENGTH_SHORT).show();
            }
        });
    }

注:post请求时遇到的问题

① :

 @POST("Login")
 Call<ResponseBody> postCheck(@Query("username") String userName, @Query("password") String password);

当我用与GET相同的写法写POST请求时,出现以下错误:
这里写图片描述这里写图片描述

先是在打印log日志的地方出现空指针,随后断点调试发现请求格式无效。原因未找到,如知晓,麻烦留言告诉我,谢谢!

②:

随后谷歌了一波,我更改了接口申明:

@FormUrlEncoded
@POST("Login")
Call<ResponseBody> postCheck(@Query("username") String userName, @Query("password") String password);

我忘记在那篇博客看到的,说这个 @FormUrlEncoded 注解必不可少(POST中)。结果如下:
这里写图片描述这里写图片描述

调用API接口中的方法时直接崩溃,断点调试结果:找不到本地变量。原因未明,如知晓,麻烦留言告诉我,谢谢!

③:

根据log日志提示,最终更改为:

@FormUrlEncoded
@POST("Login")
Call<ResponseBody> postCheck(@Field("username") String userName, @Field("password") String password);

编译通过。

这里写图片描述

这是从https://www.jianshu.com/p/0fda3132cf98博主那儿抄下来的,希望对你们有用!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值