【发布时间】:2018-01-23 12:45:53
【问题描述】:
我从 api 结构中获取数据时出现此错误。我检查了许多网站以供参考,但我没有得到正确的解决方案
public class ApiClient {
public static final String BASE_URL = "http://stock.adverscribe.in/api/controllers/";
private static Retrofit retrofit = null;
public
static Retrofit getClient() {
Gson gson = new GsonBuilder()
.setLenient()
.create();
if (retrofit == null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
}
return retrofit;
}
}
ApiInterface.java
public interface ApiInterface {
@GET("getMenu")
Call<MenuResponse> getMenuResult();
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
Call<MenuResponse> call = apiService.getMenuResult();
call.enqueue(new Callback<MenuResponse>() {
@Override
public void onResponse(Call<MenuResponse> call, Response<MenuResponse> response) {
int code = response.body().getCode();
Toast.makeText(MainActivity.this, "" + code, Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<MenuResponse> call, Throwable t) {
Toast.makeText(MainActivity.this, "" + t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
这是json数据
{
"code": 200,
"message": "SUCCESS",
"data": {
"menu": [
{
"division": {
"stock_division_id": "1",
"stock_division_name": "Men",
"stock_division_status": "1",
"created_date": "2018-01-12 11:59:00",
"updated_date": "2018-01-12 11:59:22"
},
"cat": [
{
"stock_category_id": "1",
"stock_division_id": "1",
"stock_category_name": "T-Shirts",
"stock_category_status": "1",
"created_date": "2018-01-12 12:00:14",
"updated_date": "2018-01-12 12:00:14"
}
]
},
{
"division": {
"stock_division_id": "2",
"stock_division_name": "Women",
"stock_division_status": "1",
"created_date": "2018-01-12 11:59:44",
"updated_date": "2018-01-12 11:59:44"
},
"cat": [
{
"stock_category_id": "2",
"stock_division_id": "2",
"stock_category_name": "T-Shirts",
"stock_category_status": "1",
"created_date": "2018-01-12 12:02:24",
"updated_date": "2018-01-12 12:02:24"
}
]
}
]
}
}
错误堆栈
01-23 18:34:14.219 13628-13628/? I/art: Late-enabling -Xcheck:jni
01-23 18:34:14.544 13628-13628/com.adverscribe.myapplication W/System: ClassLoader referenced unknown path: /data/app/com.adverscribe.myapplication-2/lib/arm
01-23 18:34:14.567 13628-13628/com.adverscribe.myapplication I/InstantRun: starting instant run server: is main process
01-23 18:34:14.687 13628-13628/com.adverscribe.myapplication W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-23 18:34:14.998 13628-13628/com.adverscribe.myapplication D/NetworkSecurityConfig: No Network Security Config specified, using platform default
01-23 18:34:15.123 13628-13650/com.adverscribe.myapplication I/Adreno: QUALCOMM build : ead2395, I23cc4685e6
Build Date : 04/11/17
OpenGL ES Shader Compiler Version: XE031.09.00.04
Local Branch :
Remote Branch : quic/gfx-adreno.lnx.1.0.r5-rel
Remote Branch : NONE
Reconstruct Branch : NOTHING
01-23 18:34:15.137 13628-13650/com.adverscribe.myapplication I/OpenGLRenderer: Initialized EGL, version 1.4
01-23 18:34:15.137 13628-13650/com.adverscribe.myapplication D/OpenGLRenderer: Swap behavior 1
01-23 18:34:15.170 13628-13628/com.adverscribe.myapplication W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
01-23 18:34:15.205 13628-13628/com.adverscribe.myapplication I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@ca6d04c time:134394442
01-23 18:34:16.628 13628-13628/com.adverscribe.myapplication I/ViewRootImpl: ViewRoot's KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=134395859, downTime=134395859, deviceId=-1, source=0x101 } to DecorView@67df74[MainActivity]
01-23 18:34:16.630 13628-13628/com.adverscribe.myapplication I/AudioManagerEx: AudioManagerEx created
01-23 18:34:16.709 13628-13628/com.adverscribe.myapplication I/ViewRootImpl: ViewRoot's KeyEvent { action=ACTION_UP, keyCode=KEYCODE_BACK, scanCode=0, metaState=0, flags=0x48, repeatCount=0, eventTime=134395942, downTime=134395859, deviceId=-1, source=0x101 } to DecorView@67df74[MainActivity]
【问题讨论】:
-
显示完整的错误堆栈跟踪。并显示您尝试解析的 JSON。
-
json 数据堆栈跟踪已添加到问题中,请查看
-
您提供的日志中没有异常堆栈跟踪。请给出异常堆栈跟踪。
-
正如@dave 提到的,除了 JSON 之外,您的 API 还会返回一些垃圾,这会使响应成为无效的 JSON。在继续之前让您的 API 返回 JSON。