gpt4 book ai didi

android - 关于 Volley 响应的问题

转载 作者:行者123 更新时间:2023-11-29 15:37:37 24 4
gpt4 key购买 nike

我想在从服务器获取数据的地方实现一些代码。
我使用了 Volley 响应方法来获取数据。
我对此有疑问,我需要如果响应结果为空,则将显示 TextView,否则将显示 ListView。

下面是我的代码。

如果服务器上没有数据,则日志结果为{"result":[]}

如果我使用 if(response.trim().length()==13) 条件。然后就可以正常工作了

   private void sendRequest(){
StringRequest stringRequest = new StringRequest(Request.Method.POST,JSON_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("***********","************");
Log.e("***********",response.trim());
Log.e("***********","************");
if(response.trim().equalsIgnoreCase("")){
tvMSG.setText("There is no history");
tvMSG.setVisibility(View.VISIBLE);
listView.setVisibility(View.GONE);
}
else{
showJSON(response);
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MembershipHistory.this,error.getMessage(), Toast.LENGTH_LONG).show();
}
});

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}

protected void showJSON(String json){
ParseJSON pj = new ParseJSON(json);
pj.parseJSON();
Profile_Match_custom_Lists cl = new Profile_Match_custom_Lists(MembershipHistory.this, ParseJSON.ids,ParseJSON.ages, ParseJSON.heights, ParseJSON.communities,
ParseJSON.castes,ParseJSON.educations,ParseJSON.occupations,ParseJSON.incomes,ParseJSON.pics,ParseJSON.locations,ParseJSON.shortlist,ParseJSON.expressinterest);
listView.setAdapter(cl);
}

下面是 ParseJSON 类

public class ParseJSON {

public static String[] ids;
public static String[] ages;
public static String[] heights;
public static String[] communities;
public static String[] castes;
public static String[] educations;
public static String[] occupations;
public static String[] incomes;
public static String[] pics;
public static String[] shortlist;
public static String[] expressinterest;
public static String[] locations;

public static final String JSON_ARRAY = "result";
public static final String KEY_ID = "id";
public static final String KEY_AGE="age";
public static final String KEY_HEIGHT = "height";
public static final String KEY_COMMUNITY = "community";
public static final String KEY_CASTE = "caste";
public static final String KEY_EDUCATION = "education";
public static final String KEY_OCCUPATION = "occupation";
public static final String KEY_INCOME= "income";
public static final String KEY_PIC = "pic";
public static final String KEY_LOCATION="location";
public static final String KEY_EXPRESSINTEREST="expressinterest";
public static final String KEY_SHORTLIST="shortlist";


private JSONArray users = null;
private String json;
public ParseJSON(String json){
this.json = json;
}

protected void parseJSON(){
JSONObject jsonObject=null;
try {
jsonObject = new JSONObject(json);

users = jsonObject.getJSONArray(JSON_ARRAY);
ids = new String[users.length()];
ages = new String[users.length()];
heights = new String[users.length()];
communities = new String[users.length()];
castes = new String[users.length()];
educations = new String[users.length()];
occupations = new String[users.length()];
incomes = new String[users.length()];
pics = new String[users.length()];
locations = new String[users.length()];
shortlist = new String[users.length()];
expressinterest = new String[users.length()];

for(int i=0;i<users.length();i++){

JSONObject jo = users.getJSONObject(i);
ids[i] = jo.getString(KEY_ID);
ages[i] = jo.getString(KEY_AGE);
heights[i] = jo.getString(KEY_HEIGHT);
communities[i] = jo.getString(KEY_COMMUNITY);
castes[i] = jo.getString(KEY_CASTE);
educations[i] = jo.getString(KEY_EDUCATION);
occupations[i] = jo.getString(KEY_OCCUPATION);
incomes[i] = jo.getString(KEY_INCOME);
pics[i] = jo.getString(KEY_PIC);
locations[i] = jo.getString(KEY_LOCATION);
shortlist[i] = jo.getString(KEY_SHORTLIST);
expressinterest[i] = jo.getString(KEY_EXPRESSINTEREST);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}

最佳答案

显然您已从服务器收到 {"result":[]},但与空字符串进行比较。

您应该解析响应并检查空数组。

try {
JSONObject responseObject = new JSONObject(response);
JSONArray array = responseObject.getJSONArray("result");
if(array.length() == 0){
//no data
} else{
//use the data.
}
} catch (JSONException e) {
e.printStackTrace();
}

您还可以使用 if (response.trim().equalsIgnoreCase("{\"result\":[]}")) 代替 if(response.trim() .equalsIgnoreCase("")) 但不推荐这种方法。

关于android - 关于 Volley 响应的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46543080/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com