2011-10-28 68 views
0

我想从Android上的JSON-RPC服务获取响应,目前我正在3.0 Honeycomb上开发。android-json-rpc,接收无效响应

这是我使用的库: 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; 
    } 
} 

回答

2

我已经测试使用的库的最后版本的系统。它工作得很好。你需要我们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:使用这个新版本,您可以使用参数send作为数组,或使用JSONObject发送命名参数。这只有在使用JSON-RPC协议的2.0版时才可能。

0

这是唯一JSON-RPC客户我已经能够获得在Android上Zend_Json_Server工作(我已经尝试了一些)。

确保该版本设置为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();