【问题标题】:android-json-rpc, receiving invalid responseandroid-json-rpc,收到无效响应
【发布时间】:2011-10-28 12:35:56
【问题描述】:

我正在尝试从 Android 上的 JSON-RPC 服务获取响应,我目前正在开发 3.0 Honeycomb。

这是我正在使用的库: http://code.google.com/p/android-json-rpc/

我正在使用这个 JSON-RPC 服务页面进行测试: http://www.raboof.com/projects/jayrock/demo.ashx

连接似乎有效,但我不断收到此异常

org.alexd.jsonrpc.JSONRPCException: Invalid JSON response

我尝试了不同的方法和调查页面,但总是遇到相同的异常。我哪里错了?

相关代码如下。使用 AsyncTask 是因为自 3.0 以来,Android 不允许主流中的网络连接。提前致谢。

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    JSONHandler task = new JSONHandler();
    task.execute(new String[] {"http://www.raboof.com/projects/jayrock/demo.ashx"});    
}

private class JSONHandler extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... urls) {
        for (String url : urls) {
            JSONRPCClient client = JSONRPCClient.create(url);
            client.setConnectionTimeout(2000);
            client.setSoTimeout(2000);

            try {
                client.call("counter");
            } catch (JSONRPCException e) {
                e.printStackTrace(); //Invalid JSON Response caught here
            }
        }
        return null;
    }
}

【问题讨论】:

    标签: android json json-rpc


    【解决方案1】:

    我已经使用库的最新版本测试了您的系统。它工作得很好。你需要我们 callInt("counter") 就可以了。

    有我用的代码:

    public JSONRPCClient client = JSONRPCClient.create("http://www.raboof.com/projects/jayrock/demo.ashx", JSONRPCClient.Versions.VERSION_2);
    try{
        int resInt = client.callInt("counter");
    } catch (JSONException e) {
        Log.i("JSON-RPC Client", e.toString());
    }
    

    我希望这会有所帮助。

    PS:在这个新版本中,您可以将参数作为数组发送,或者使用 JSONObject 来发送命名参数。这只有在使用 JSON-RPC 协议 2.0 版本时才有可能。

    【讨论】:

      【解决方案2】:

      这是我能够在 Android 上使用 Zend_Json_Server 的唯一 JSON-RPC 客户端(我已经尝试了一些)。

      确保也将版本设置为 2.0,因为除非您的服务器明确使用 2.0 规范,否则此客户端无法工作:

          $server = new Zend_Json_Server();
          $server->setClass('My_Class');
          $server->getRequest()->setVersion("2.0");
          if ('GET' == $_SERVER['REQUEST_METHOD']) {
              // Indicate the URL endpoint, and the JSON-RPC version used:
              $server->setTarget('/ajax.php')
                     ->setEnvelope(Zend_Json_Server_Smd::ENV_JSONRPC_2);
      
              // Grab the SMD
              $smd = $server->getServiceMap();
      
              // Return the SMD to the client
              header('Content-Type: application/json');
              echo $smd;
              return;
          }
      
      $server->handle();
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-21
        • 1970-01-01
        • 2013-07-01
        相关资源
        最近更新 更多