khaintt / android-json-rpc

Automatically exported from code.google.com/p/android-json-rpc
0 stars 0 forks source link

Throws JSON Exception if "error" parameter is not set! #1

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In the JSONRPCHttpClient class in the doJSONRequest function an error
occurs, if the JSONRPC Server returns a result (not a error).

In the JSONRPC 2.0 Specification the "error" parameter of the response
object is omitted at succes.

Because 'jsonObject.get("error")' returns "JSONObject["error"] not found."
an exception gets thrown.

I replaced this code:

    // Check for remote errors
    Object jsonError = jsonResponse.get("error");
    if (!jsonError.equals(null)) {
        throw new JSONRPCException(jsonError);
    } else {
        return jsonResponse;
    }

with the following:

    // Check for remote errors
    if (jsonResponse.has("error")) {
        throw new JSONRPCException(jsonResponse.get("error"));
    } else {
        return jsonResponse;
    }

Original issue reported on code.google.com by felixro...@gmail.com on 4 Feb 2010 at 12:41

GoogleCodeExporter commented 9 years ago
Felix, thanks for reporting this.
I think both tests should be performed to preserve compatibility with JSONRPC 
1.0 (in 
which the error field is always present but may have a null value)

So the modification will be along the lines :
     // Check for remote errors
     if (jsonResponse.has("error")) {
        Object jsonError = jsonResponse.get("error");
        if (!jsonError.equals(null)) throw new 
JSONRPCException(jsonResponse.get("error"));
        return jsonResponse; //JSON-RPC 1.0
    } else {
        return jsonResponse; //JSON-RPC 2.0
    }

I'll run some tests and upload svn and package after that.
I have mainly focused on 1.0 spec for the library, so if you encounter other 
inconsistent behaviours with 2.0 you are welcome to report them here.

Alexandre

Original comment by alexd6...@gmail.com on 4 Feb 2010 at 7:21

GoogleCodeExporter commented 9 years ago

Original comment by alexd6...@gmail.com on 4 Feb 2010 at 7:22

GoogleCodeExporter commented 9 years ago
Code updated in svn and in the new 0.2.1 release.

Original comment by alexd6...@gmail.com on 5 Feb 2010 at 5:01