Java JSON解析时,代码中的问题是什么?
程序从web服务器获取数据,并将数据放入Java JSON解析时,代码中的问题是什么?,java,android,json,android-volley,android-broadcast,Java,Android,Json,Android Volley,Android Broadcast,程序从web服务器获取数据,并将数据放入arrayList。如果arrayList的大小大于1,则它将成功工作。但是如果它更小,那必须显示我设置的错误屏幕 我的问题是当它有两个不同的tabItem并得到两个不同的数组时,如果其中一个大小不大于1。它在同一页中显示错误消息。并在页面中显示数据。在没有数据的情况下,如何显示此屏幕? 这是从web服务器获取数据的类。 Voyage.java JSONObject object = new JSONObject(response); if (objec
arrayList
。如果arrayList
的大小大于1,则它将成功工作。但是如果它更小,那必须显示我设置的错误屏幕
我的问题是当它有两个不同的tabItem并得到两个不同的数组时,如果其中一个大小不大于1。它在同一页中显示错误消息。并在页面中显示数据。在没有数据的情况下,如何显示此屏幕?
这是从web服务器获取数据的类。Voyage.java
JSONObject object = new JSONObject(response);
if (object.getString(MyConstants.SERVICE_STATUS).equals(MyConstants.SERVICE_RESPONSE_STATUS_NOTAVAILABLE)) {
sendVoyagesErrorBroadcast(owner, MyConstants.ERROR_NOTAVAILABLE);
} else if (object.getString(MyConstants.SERVICE_STATUS).equals(MyConstants.SERVICE_RESPONSE_STATUS_SUCCESS)) {
JSONArray result = object.getJSONArray(MyConstants.SERVICE_RESULT);
if (result.length() > 0) {
JSONArray resultGoing = result.getJSONObject(0).getJSONArray("going");
sendVoyagesArrayBroadcast(owner + MyConstants.DIRECTION_GOING, resultGoing);
} else {
sendVoyagesErrorBroadcast(owner, MyConstants.ERROR_NO_VOYAGE);
}
if (has_return) {
if (result.length() >1) {
JSONArray resultReturn = result.getJSONObject(1).getJSONArray("round");
sendVoyagesArrayBroadcast(owner + MyConstants.DIRECTION_RETURN, resultReturn);
} else {
sendVoyagesErrorBroadcast(owner, MyConstants.ERROR_NO_VOYAGE);
}
}
}
这是解析JSON数组并检查数组大小的类。
MyBaseVoyagementFragment.java
public ParseIntentDataAndLoadVoyagesAdapter(Intent intent) {
this.intent = intent;
}
@Override
protected String doInBackground(String... params) {
if (isCancelled()) return "";
try {
JSONArray jsonArray = new JSONArray(intent.getStringExtra("data"));
voyageList = new ArrayList<>();
for (int i = 0; i < jsonArray.length(); i++) {
voyageList.add(Voyage.setJsonToClass(jsonArray.getJSONObject(i), direction, owner));
}
if (voyageList.size() < 1) {
setErrorView(MyConstants.ERROR_NO_VOYAGE);
}
编辑:添加了setErorView函数
void setErrorView(int errorType) {
if (errorType == MyConstants.ERROR_NOTAVAILABLE && owner == "ship") {
if (SeawayReturnFragment.tabLayout != null)
SeawayReturnFragment.tabLayout.setVisibility(View.INVISIBLE);
SeawayDepartureFragment.tabLayout.setVisibility(View.INVISIBLE);
}
errorView.setVisibility(View.VISIBLE);
mRecyclerView.setVisibility(View.GONE);
TextView tvDesc = errorView.findViewById(R.id.tv_Desc);
tvDesc.setText(getErrorDesc(errorType));
}
布局如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp">
<TextView
android:id="@+id/textView25"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_below="@+id/img_Type"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Bir şeyler ters gitti"
android:textColor="@color/md_grey_500"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/img_Type"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:contentDescription="@string/cd_Error"
app:srcCompat="@drawable/ic_sadf" />
<TextView
android:id="@+id/tv_Desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView25"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
android:textColor="@color/md_grey_400"
android:textSize="20sp" />
我解决了这个问题。根据我的代码,这个问题不会发生。这种情况的发生是因为web服务器的错误。该程序从web服务器获取“going”和“round”数组。服务器不发送“圆形”数组,所以在我的程序中会出现这个问题。现在他们修复了,它将空数组作为“圆形”发送。所以,我的问题解决了。您可以共享您的xml布局文件吗?您在哪里设置错误屏幕?并在setErrorView
方法中共享代码?对不起,我回复晚了。我现在对它们进行了编辑,我想当数据长度大于one@MuhammadMuzammilSharif我认为这与形象无关。我在分析Voyage.java中的数据时使用的broadcast和if子句。是否要将在广播中发送的错误显示到各自的片段中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp">
<TextView
android:id="@+id/textView25"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_below="@+id/img_Type"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Bir şeyler ters gitti"
android:textColor="@color/md_grey_500"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/img_Type"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:contentDescription="@string/cd_Error"
app:srcCompat="@drawable/ic_sadf" />
<TextView
android:id="@+id/tv_Desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView25"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:gravity="center"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Medium"
android:textColor="@color/md_grey_400"
android:textSize="20sp" />