如何从我的ServiceHandler(Android JSON)返回状态代码

时间:2014-09-09 17:29:23

标签: android json httprequest

我想从每个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?

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

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;


    }

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

2 个答案:

答案 0 :(得分:0)

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

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;

}

}

最后在你的asynctask:

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;


}

答案 1 :(得分:0)

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;

}