关于排球响应的问题

时间:2017-10-03 11:03:49

标签: android android-volley

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

以下是我的代码。

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

如果我使用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();
     }
    }
  }

3 个答案:

答案 0 :(得分:2)

显然,您已从服务器收到{"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("")),但不建议使用此方法。

答案 1 :(得分:1)

{"result":[]}有13个字符,当然。

但是,这与空字符串不同,如果result键发生更改,或服务器响应添加任何空格字符,则表示条件不佳。

此外,当结果列出为空时,您的ParseJson类看起来很正常。所有数组的长度均为0。因此,listview为空。

如果您想在没有数据的情况下显示内容,您应该在Listview上使用setEmptyView

我还建议不要使用static String[]值,并且您已经定义了一个User类,您可以从中解析JSON(理想情况下使用像Gson这样的JSON库)

答案 2 :(得分:1)

以下更改应该有效 在ParseJSON.java

private JSONArray users = null;
private String json;
private int arraySize=-1
public ParseJSON(String json){
    this.json = json;
}
public int getArraySize(){
   return arraySize;
}
protected void parseJSON(){
    JSONObject jsonObject=null;
    try {
        jsonObject = new JSONObject(json);
        users = jsonObject.getJSONArray(JSON_ARRAY);
        arraySize=1; .......// rest of code will be same here

onResponse(String response)的实施中

            @Override
            public void onResponse(String response) {
                Log.e("***********","************");
                Log.e("***********",response.trim());
                Log.e("***********","************");
                ParseJSON pj = new ParseJSON(json);
                pj.parseJSON();
                if(pj.getArraySize()>0){  // or ==1  whatever you like.
                    tvMSG.setText("There is no history");
                    tvMSG.setVisibility(View.VISIBLE);
                    listView.setVisibility(View.GONE);
                }
                else{
                    showJSON(pj);
                }
            }

请不要忘记更改showJSON(pj),如下所示

protected void showJSON(ParseJSON pj){

    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);
}

快乐编码..