java - retrofit2.0 post提交一直返回400,用httpurlconnection就可以
巴扎黑
巴扎黑 2017-04-18 09:30:15
[Java讨论组]

1.用retrofit2.0 post提交 json
@POST("AppLoginIn")

Call<JsonObject> getLoginUser(@Body String parameterJson);

//待提交的json数据
private String toJsonUser() {

    JSONStringer jsonStringer = null;
    try {
        jsonStringer = new JSONStringer().object().key("LoginCode").value("SUPERMAN").key("Password")
                .value("111111").endObject();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    Log.i("", "---json user:" + jsonStringer.toString());
    return jsonStringer.toString();
}
//retrofit 请求
 private void postYDL(String jsonString) {
    Retrofit retrofit = new Retrofit.Builder().baseUrl(url)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    IService iService = retrofit.create(IService.class);
    Call<JsonObject> call = iService.getLoginUser(jsonString);
    call.enqueue(new Callback<JsonObject>() {
        @Override
        public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
            String responseData = "---body:" + response.body()
    • "n---message:" + response.message()

    • "n---headers:" + response.headers()

    • "n---code:" + response.code()

    • "n---isSuccessful:" + response.isSuccessful();

                 Log.i("", "---base action responseData:" + responseData);
                 tv1.setText(responseData);
             }
      
             @Override
    1. void onFailure(Call<JsonObject> call, Throwable t) {

                 tv1.setText(t.getMessage());
             }
         });

      }

      换另一个请求地址结果:

    巴扎黑
    巴扎黑

    全部回复(0)
    热门教程
    更多>
    最新下载
    更多>
    网站特效
    网站源码
    网站素材
    前端模板
    关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
    php中文网:公益在线php培训,帮助PHP学习者快速成长!
    关注服务号 技术交流群
    PHP中文网订阅号
    每天精选资源文章推送
    PHP中文网APP
    随时随地碎片化学习

    Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号