如何在ASP.NET Web API中将用于创建自定义异常的字符串解释为Android中的HttpURLConnection.getResponseMessage()

时间:2014-11-10 00:56:32

标签: android asp.net-web-api httpurlconnection

我正在尝试从ASP.NET Web API发送自定义异常,但是当我从Android使用这些WebService时,我总是收到不同的消息:

这是我如何阅读Android中的网络服务:

public Object doRequest(String url) {       
    String charset = Charset.defaultCharset().displayName();
    try {
        if (mFormBody != null) {
            // Form data to post
            mConnection
                    .setRequestProperty("Content-Type",
                            "application/json; charset="
                                    + charset);
            mConnection.setFixedLengthStreamingMode(mFormBody.length());
        }           
        mConnection.connect();

        if (mFormBody != null) {
            OutputStream out = mConnection.getOutputStream();
            writeFormData(charset, out);
        }

        // Get response data
        int status = mConnection.getResponseCode();
        if (status >= 300) {
            String message = mConnection.getResponseMessage();
            return new HttpResponseException(status, message);
        }

        InputStream in = mConnection.getInputStream();
        String enconding = mConnection.getContentEncoding();            
        if (enconding == null) {
            enconding = "UTF-8";
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                in, enconding));

        StringBuilder sb = new StringBuilder();         

        String line=null;           
        while ((line=reader.readLine()) != null) {
              sb.append(line);              
        }

        return sb.toString().trim();

    } catch (Exception e) {
        return e;
    }
    finally{
        if(mConnection!=null){
            mConnection.disconnect();
        }
    }
}

如您所见,我检查getResponseCode()返回的值,如果它等于或大于 300 ,则抛出异常。一切正常,除了getResponseMessage()没有返回我在WebApi中创建异常时使用的字符串这一事实。相反,我得到了这个错误:

enter image description here

在WebApi中,我在catch块中所做的就是抛出异常:

    try
    {

    }
    catch (Exception ex)
    {
        throw ex;
    } 

使用fiddler意识到我收到了这条消息:

{"Message":"Error."}

好吧,在互联网上寻找解决方案,我发现我可以这样做:

try
        {

        }
        catch (Exception ex)
        {   

            throw new HttpResponseException(Request.CreateErrorResponse(HttpStatusCode.NotFound, ex.Message));
            //throw (ex);
        }

但不幸的是,这也没有用。虽然fiddler现在显示我用来创建Exception的消息。

enter image description here 当我读到getResponseMessage()的值时,将返回此字符串:“未找到”。

您知道我需要做什么,以便从WebApi通过Exception发送的消息进入Android,特别是getResponseMessage()属性吗?

提前致谢。

1 个答案:

答案 0 :(得分:1)

好吧,我认为你需要先创建一个HttpResponseMessage对象,然后根据该对象创建你要投掷的HttpResponseException

设置HttpResponseMessage对象非常简单。大多数情况下,您只需要设置两个属性:ContentReasonPhrase

            try
            {

            }
            catch (Exception ex)
            {              

                HttpResponseMessage msg = new HttpResponseMessage(HttpStatusCode.NotFound)
                {
                    Content = new StringContent(string.Format("Excepción")),
                    ReasonPhrase = ex.Message
                };
                throw new HttpResponseException(msg);
            }

正如您所看到的那样,ReasonPhrase我们传递了异常消息。

希望它有所帮助。