改造2:从响应正文获取JSON

我想使用翻新2从我的api中获取字符串json,当使用翻新1来获取此json时我没有问题,但是使用翻新2 为我返回null。


这就是我的json的样子


{"id":1,"Username":"admin","Level":"Administrator"}

这是我的API


@FormUrlEncoded

@POST("/api/level")

Call<ResponseBody> checkLevel(@Field("id") int id);

这就是我的代码的样子


Retrofit retrofit = new Retrofit.Builder()

                .baseUrl(Config.BASE_URL)

                .addConverterFactory(GsonConverterFactory.create())

                .build();

        Api api = retrofit.create(Api.class);

        Call<ResponseBody> call = api.checkLevel(1);

        call.enqueue(new Callback<ResponseBody>() {

            @Override

            public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                JsonObject post = new JsonObject().get(response.body().toString()).getAsJsonObject();

                    if (post.get("Level").getAsString().contains("Administrator")) {


                    }

            }


            @Override

            public void onFailure(Call<ResponseBody> call, Throwable t) {

            }

        });

我是不熟悉2并使用上面的代码的新手,它总是使我的应用程序崩溃,因为response.body().toString()返回null。


请指导我如何获取该json字符串,以便可以将其转换为JsonObject。


互换的青春
浏览 489回答 3
3回答

慕村9548890

如果要以JSON格式获取整个响应,请尝试以下操作:我尝试了一种新方法,可以从服务器获取JSON格式的整个响应,而无需创建任何模型类。我不使用任何模型类从服务器获取数据,因为我不知道我将得到什么响应,否则它可能会根据要求而变化。这是JSON响应:{"contacts": [&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": "c200",&nbsp; &nbsp; &nbsp; &nbsp; "name": "sunil",&nbsp; &nbsp; &nbsp; &nbsp; "email": "email@gmail.com",&nbsp; &nbsp; &nbsp; &nbsp; "address": "xx-xx-xxxx,x - street, x - country",&nbsp; &nbsp; &nbsp; &nbsp; "gender" : "male",&nbsp; &nbsp; &nbsp; &nbsp; "phone": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "mobile": "+91 0000000000",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "home": "00 000000",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "office": "00 000000"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; "id": "c201",&nbsp; &nbsp; &nbsp; &nbsp; "name": "Johnny Depp",&nbsp; &nbsp; &nbsp; &nbsp; "email": "johnny_depp@gmail.com",&nbsp; &nbsp; &nbsp; &nbsp; "address": "xx-xx-xxxx,x - street, x - country",&nbsp; &nbsp; &nbsp; &nbsp; "gender" : "male",&nbsp; &nbsp; &nbsp; &nbsp; "phone": {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "mobile": "+91 0000000000",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "home": "00 000000",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "office": "00 000000"&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; },&nbsp; &nbsp; .&nbsp; &nbsp; .&nbsp; &nbsp; .]}在您的API界面中更改参数public interface ApiInterface {@POST("/index.php/User/login")//your api link&nbsp;@FormUrlEncodedCall<Object> getmovies(@Field("user_email_address") String title,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Field("user_password") String body);}在您的主要活动中ApiInterface apiService =&nbsp; &nbsp; &nbsp; &nbsp; ApiClient.getClient().create(ApiInterface.class);Call call = apiService.getmovies("a@gmail.com","123456");call.enqueue(new Callback() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onResponse(Call call, Response response) {&nbsp; &nbsp; &nbsp; &nbsp; Log.e("TAG", "response 33: "+new Gson().toJson(response.body()) );&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onFailure(Call call, Throwable t) {&nbsp; &nbsp; &nbsp; &nbsp; Log.e("TAG", "onFailure: "+t.toString() );&nbsp; &nbsp; &nbsp; &nbsp; // Log error here since request failed&nbsp; &nbsp; }});之后,您通常可以使用JSON对象和JSON数组获取参数
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android