JSONObject(response.body()。toString())为空对象[重复]

问题描述 投票:0回答:1
我不知道为什么response.body()为null,这会使我的应用崩溃,请检查代码

这是普通班

public class Common { public static User userterkini; public static tujuan tujuanterkini; public static final String UPDATE = "Update"; public static final String DELETE = "Delete"; public static final int PICK_IMAGE_DI_PESAN=71; public static final String baseUrl = "https://maps.googleapis.com"; public static kordint getgeokodeservis(){ return retrofitclient.getClient(baseUrl).create(kordint.class); } public static Bitmap scalebitmap(Bitmap bitmap,int lebar,int tinggi) { Bitmap scalebitmaps = Bitmap.createBitmap(lebar,tinggi,Bitmap.Config.ARGB_8888); float scaleX = lebar/(float)bitmap.getWidth(); float scaleY = tinggi/(float)bitmap.getHeight(); float pivotX=0,pivotY=0; Matrix scaleMatrix = new Matrix(); scaleMatrix.setScale(scaleX,scaleY,pivotX,pivotY); Canvas canvas=new Canvas(scalebitmaps); canvas.setMatrix(scaleMatrix); canvas.drawBitmap(bitmap,0,0,new Paint(Paint.FILTER_BITMAP_FLAG)); return scalebitmaps; } }

这是kordnt接口

package com.example.wsulangserver01.Remote; public interface kordint { @GET("maps/api/geocode/json") Call<String> getgeokode(@Query("address")String address); @GET("maps/api/directions/json") Call<String>gettujuan(@Query("origin")String origin,@Query("destination")String destination); }

这是retrofitclient类

public class retrofitclient { private static Retrofit retrofit = null; public static Retrofit getClient(String baseUrl) { if (retrofit==null) { retrofit=new Retrofit.Builder() .baseUrl(baseUrl) .addConverterFactory(ScalarsConverterFactory.create()) .build(); } return retrofit; } }

这是地图活动

private void rute(final LatLng lokasiuser, String alamat) { kordint.getgeokode(alamat).enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { try{ JSONObject jsonObject=new JSONObject(response.body().toString()); String lat = ((JSONArray)jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometri") .getJSONObject("location") .get("lng").toString(); String lng = ((JSONArray)jsonObject.get("results")).getJSONObject(0) .getJSONObject("geometri") .getJSONObject("location") .get("lng").toString(); LatLng lokasipesanan = new LatLng(Double.parseDouble(lat),Double.parseDouble(lng)); Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.wahidin); bitmap = Common.scalebitmap(bitmap,70,70); MarkerOptions marker = new MarkerOptions().icon(BitmapDescriptorFactory.fromBitmap(bitmap)) .title("Dikirim ke "+Common.tujuanterkini.getNotelp()) .position(lokasipesanan); mMap.addMarker(marker); //jalur rute kordint.gettujuan(lokasiuser.latitude+","+lokasiuser.longitude,lokasipesanan +","+lokasipesanan.latitude+","+lokasipesanan.longitude).enqueue(new Callback<String>() { @Override public void onResponse(Call<String> call, Response<String> response) { new parser().execute(response.body().toString()); } @Override public void onFailure(Call<String> call, Throwable t) { } }); } catch (JSONException e) { e.printStackTrace(); } } @Override public void onFailure(Call<String> call, Throwable t) { } }); }

贝洛是错误

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toString()' on a null object reference at com.example.wsulangserver01.Mapps$1.onResponse(Mapps.java:126) at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7397) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:920)

我不知道为什么我的response.body()总是为空,请帮助我解决这个问题:)
java android android-studio maps retrofit2
1个回答
0
投票
Retrofit onResponsedocumentation)在收到HTTP响应时调用,换句话说,还考虑200或404。请使用Response.isSuccessful()检查请求是否成功,否则,只需打印错误代码和/或错误正文即可进行调试。

-1
投票
您可以根据您的JSON响应创建模型,然后使用Gson和GsonConverter !!当您使用GsonConverter时,不必解析JSON,因为Gson会对您的代码执行此操作!
© www.soinside.com 2019 - 2024. All rights reserved.