【问题标题】:How to go about returning the status code from my ServiceHandler (Android JSON)如何从我的 ServiceHandler (Android JSON) 返回状态码
【发布时间】:2014-09-09 17:29:23
【问题描述】:

我想从每个 httpResponse 中获取状态代码,并以与每个响应不同的方式处理代码。但是我在下面使用了一个单独的 serviceHandler 类:

public class ServiceHandler {
static String response = null;
public final static int GET = 1;
public final static int POST = 2;

public ServiceHandler() {

}

/**
 * Making service call
 * @url - url to make request
 * @method - http request method
 * */
public String makeServiceCall(String url, int method) {
    return this.makeServiceCall(url, method, null);
}

/**
 * Making service call
 * @url - url to make request
 * @method - http request method
 * @params - http request params
 * */
public String makeServiceCall(String url, int method,
                              List<NameValuePair> params) {
    try {
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;

        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            // adding post params
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }

            httpResponse = httpClient.execute(httpPost);

        } else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);

            httpResponse = httpClient.execute(httpGet);

        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);

        //get the Status code here
        int statusCode = httpResponse.getStatusLine().getStatusCode();





    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return response;

}

}

如何返回 statusCode?​​p>

这是我的代码,它调用它并处理解析响应:

protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String result = sh.makeServiceCall(url, ServiceHandler.GET);



        Log.d("Response: ", "> " + result);

        if (result != null) {
            try {

                NotValid = false;

            } catch (JSONException e) {
                e.printStackTrace();

            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
            NotValid = true;


        }


        return null;


    }

是否可以从上面在我的解析器中捕获的结果中解析状态代码?

【问题讨论】:

    标签: android json httprequest


    【解决方案1】:

    创建一个类并将状态和响应放入其中,然后返回该类

    class MyResult{
        public String response;
        public int statusCode;
    }
    

    然后你的代码必须是这样的:

    public class ServiceHandler {
    static String response = null;
    public final static int GET = 1;
    public final static int POST = 2;
    
    public ServiceHandler() {
    
    }
    
    /**
     * Making service call
     * @url - url to make request
     * @method - http request method
     * */
    public MyResult makeServiceCall(String url, int method) {
        return this.makeServiceCall(url, method, null);
    }
    
    /**
     * Making service call
     * @url - url to make request
     * @method - http request method
     * @params - http request params
     * */
    public MyResult makeServiceCall(String url, int method,
                                  List<NameValuePair> params) {
        MyResult result = new MyResult();
        try {
            // http client
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpEntity httpEntity = null;
            HttpResponse httpResponse = null;
    
            // Checking http request method type
            if (method == POST) {
                HttpPost httpPost = new HttpPost(url);
                // adding post params
                if (params != null) {
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                }
    
                httpResponse = httpClient.execute(httpPost);
    
            } else if (method == GET) {
                // appending params to url
                if (params != null) {
                    String paramString = URLEncodedUtils
                            .format(params, "utf-8");
                    url += "?" + paramString;
                }
                HttpGet httpGet = new HttpGet(url);
    
                httpResponse = httpClient.execute(httpGet);
    
            }
            httpEntity = httpResponse.getEntity();
            result.response = EntityUtils.toString(httpEntity);
    
            //get the Status code here
            result.statusCode = httpResponse.getStatusLine().getStatusCode();
    
    
    
    
    
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    
        return result;
    
    }
    
    }
    

    最后在你的异步任务中:

    protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();
    
            // Making a request to url and getting response
            MyResult result = sh.makeServiceCall(url, ServiceHandler.GET);
            Log.d("Response: ", "> " + result);
    
        if (result.response != null) {
            try {
    
                NotValid = false;
    
            } catch (JSONException e) {
                e.printStackTrace();
    
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
            NotValid = true;
    
    
        }
    
    
        return null;
    
    
    }
    

    【讨论】:

    • 它几乎可以工作,我在将响应和状态代码放入结果类时遇到问题,错误是无法解析符号“响应或状态代码”我将如何解决这个问题?
    • 你在哪里声明MyResult?您可以在单独的类文件中声明它,然后在您使用的每个类中导入该类,例如在 ServiceHandler 中导入该类
    • 我已经找到了一种更简单的方法,尽管你的方法有效,我正在处理服务处理程序类中的状态代码,并且根据代码,它会改变响应或保持不变。感谢您的回答!
    【解决方案2】:
    public String[] makeServiceCall(String url, int method,
                              List<NameValuePair> params) {
    try {
        String[] responseWithStatusCode=new String[2];
        // http client
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpEntity httpEntity = null;
        HttpResponse httpResponse = null;
    
        // Checking http request method type
        if (method == POST) {
            HttpPost httpPost = new HttpPost(url);
            // adding post params
            if (params != null) {
                httpPost.setEntity(new UrlEncodedFormEntity(params));
            }
    
            httpResponse = httpClient.execute(httpPost);
    
        } else if (method == GET) {
            // appending params to url
            if (params != null) {
                String paramString = URLEncodedUtils
                        .format(params, "utf-8");
                url += "?" + paramString;
            }
            HttpGet httpGet = new HttpGet(url);
    
            httpResponse = httpClient.execute(httpGet);
    
        }
        httpEntity = httpResponse.getEntity();
        response = EntityUtils.toString(httpEntity);
    
        //get the Status code here
        int statusCode = httpResponse.getStatusLine().getStatusCode();
    
        //extra line
        responseWithStatusCode[0]=response;
        responseWithStatusCode[1]=Integer.parseInt(statusCode);
    
    
    
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
    //return responseWithStatusCode instead of response....
    return responseWithStatusCode;
    

    }

    在你的 doBackground 方法中:

    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler sh = new ServiceHandler();
    
        // Making a request to url and getting response
        // changes in getting the return from makeServiceCall method
        String[] result = sh.makeServiceCall(url, ServiceHandler.GET);
        Log.d("Response: ", "> " + result);
    
    if (result[1] != null && result[0].equalsIgnoreCase("200")) { // also change this line
        try {
    
            NotValid = false;
    
        } catch (JSONException e) {
            e.printStackTrace();
    
        }
    } else {
        Log.e("ServiceHandler", "Couldn't get any data from the url");
        NotValid = true;
    
    
    }
    
    
    return null;
    

    }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-01
      • 1970-01-01
      • 2017-07-10
      • 2019-12-01
      • 2012-01-02
      • 2018-10-27
      • 2016-09-16
      相关资源
      最近更新 更多