【问题标题】:How to fix it ? org.json.JSONException: Response cannot be parsed as JSON data如何解决? org.json.JSONException:响应无法解析为 JSON 数据
【发布时间】:2020-03-27 06:43:50
【问题描述】:

03-27 11:59:09.438 4577-4577/com.example.mealitup W/JsonHttpRH: onFailure(int, Header[], String, Throwable) 未被覆盖,但收到回调 org.json.JSONException:响应无法解析为 JSON 数据 在 com.loopj.android.http.JsonHttpResponseHandler$1$1.run(JsonHttpResponseHandler.java:158) 在 android.os.Handler.handleCallback(Handler.java:739) 在 android.os.Handler.dispatchMessage(Handler.java:95) 在 android.os.Looper.loop(Looper.java:135) 在 android.app.ActivityThread.main(ActivityThread.java:5305) 在 java.lang.reflect.Method.invoke(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:372) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

【问题讨论】:

  • 请提供一些您尝试过的代码和json(如果可能的话)。

标签: android json


【解决方案1】:

使用改造

1.创建界面

import java.util.List;
import retrofit2.Call;
import retrofit2.http.GET;
public interface RequestInterface {

    @GET(Constants.JSON)
    Call<List<MyModel>> getJSON();
}

2。调用此函数并传递 URL 以获取 JSON

Retrofit retrofit = new Retrofit.Builder()
            .baseUrl( "www.myJSONexample.com")
            .client(new OkHttpClient())
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    RequestInterface request = retrofit.create(RequestInterface.class);
    Call<List<MyModel>> call = request.getJSON();

    call.enqueue(new Callback<List<Recipe>>() {
        @Override
        public void onResponse(@NonNull Call<List<MyModel>> call, @NonNull 
   Response<List<MyModel>> response) {
            List<MyModel> jsonResponse = response.body();
            String str = jsonResponse.get( 0 );
            String str2 = jsonResponse.get( 1 );
   }
        @Override
        public void onFailure(@NonNull Call<List<Recipe>> call, @NonNull Throwable t) {
            Log.e(TAG, "FAIL: " + t.getMessage());
        }
    });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 2017-08-21
    • 1970-01-01
    • 2016-01-16
    相关资源
    最近更新 更多