2011-10-28 70 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();