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

这是我在Android中阅读Web服务的方式:

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中创建异常时使用的字符串的事实。相反,我得到这个错误:



在WebApi中,我在catch块中所做的只是引发异常:

    try
    {

    }
    catch (Exception ex)
    {
        throw ex;
    }


使用提琴手意识到我正在收到此消息:

{"Message":"Error."}


好吧,在Internet上寻找解决方案时,我发现我可以做这样的事情:

try
        {

        }
        catch (Exception ex)
        {

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


但不幸的是,这也不起作用。尽管小提琴手现在显示了我用来创建异常的消息。


当我读取getResponseMessage()的值时,将返回以下字符串:“未找到”。

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

提前致谢。

最佳答案

好吧,我认为您需要先创建一个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中我们传递了异常消息。

希望能帮助到你。

关于android - 如何使用于在ASP.NET Web API中创建自定义异常的字符串在Android中解释为HttpURLConnection.getResponseMessage(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26835211/

10-11 22:48
查看更多