如果来自服务器的响应在android中是否为null?

时间:2017-05-17 07:28:54

标签: android

我发送服务器请求来处理响应和错误我已经实现了接口。我只是想知道如果“mResultCallBack == null”如果没有编辑条件if(mResultCallBack!= null)如何处理。代码如下: -

func (i item) GetName() string{
    return i.itemName
}

func (i item) GetAmount() int{
    return i.amount
}

}

和接口的代码

public Context mContext;
private IResult mResultCallBack;

public CServerRequest(IResult mResultCallBack, Context context) {
    this.mContext = context;
    this.mResultCallBack = mResultCallBack;
}

public void postWalletRequest(final String requestType, String url, JSONObject jsonObject) {
    try {
        RequestQueue queue = Volley.newRequestQueue(mContext);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                mResultCallBack.notifySuccess(requestType, response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                mResultCallBack.notifyError(requestType, error);

            }
        });
        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(jsonObjectRequest);
    } catch (Exception e) {
        e.printStackTrace();
    }

}

1 个答案:

答案 0 :(得分:0)

public Context mContext;
private IResult mResultCallBack;

public CServerRequest(IResult mResultCallBack, Context context) {
    this.mContext = context;
    this.mResultCallBack = mResultCallBack;
}

public void postWalletRequest(final String requestType, String url, JSONObject jsonObject) {
    try {
         if(jsonObject.getJSONArray.length() == 0) {

    System.out.println("JSONArray is null");      
 }
 else{
      System.out.println("JSONArray is not null");
      //parse your string here         


        RequestQueue queue = Volley.newRequestQueue(mContext);
        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonObject, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {

                mResultCallBack.notifySuccess(requestType, response);

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                mResultCallBack.notifyError(requestType, error);

            }
        });
        jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(10000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
        queue.add(jsonObjectRequest);
    }
} catch (Exception e) {
        e.printStackTrace();
    }

}