2012-03-01 41 views
0

返回響應有一條鏈路上接收遠程調用這種形式的web服務http://example.com/webservicename.fga如何調用使用JSON-RPC遠程方法,並得到Android的

我曾嘗試使用JSON進行登錄呼叫,但我沒有得到任何答案。 我要把那個應該返回一個sessionKey和ID的web服務的響應

在Web服務的服務器端方法startSession是

@Webservice(paramNames = {"email", "password", "stayLogged", "idClient"}, 
public Response startSession(String email, String password, Boolean stayLogged, String idClient) throws Exception { 
    boolean rC = stayLogged != null && stayLogged.booleanValue(); 
    UserService us = new UserService(); 
    User u = us.getUsersernamePassword(email, password); 
    if (u == null || u.getActive() != null && !u.getActive().booleanValue()) { 
     return ErrorResponse.getAccessDenied(id, logger); 
    } 
    InfoSession is = null; 
    String newKey = null; 
    while (newKey == null) { 
     newKey = UserService.md5(Math.random() + " " + new Date().getTime()); 
     if (SessionManager.get(newKey) != null) { 
      newKey = null; 
     } else { 
      is = new InfoSession(u, rC, newKey); 
      if (idClient != null && idClient.toUpperCase().equals("ANDROID")) { 
       is.setClient("ANDROID"); 
      } 
      SessionManager.add(newKey, is); 
     } 
    } 
    logger.log(Level.INFO, "New session started: " + newKey + " - User: " + u.getEmail()); 
    return new Response(new InfoSessionJson(newKey, is), null, id); 
} 

我曾嘗試使用此客戶端代碼來訪問它

static final String BASE_URI="http://example.com/webservicename.fga" 

HttpPost httppost = new HttpPost(BASE_URI); 

JSONObject json = new JSONObject(); 
String email="[email protected]"; 
String emailRic="email"+" "+"\""+email+"\""; 
String password="1234"; 
String passwordRic="password"+" "+"\""+password+"\""; 
String stayLogged="1"; 
String stayLoggedRic="stayLogged"+" "+"\""+stayLogged+"\""; 
String idClient="ANDROID"; 
String idClientRic="idClient"+" "+"\""+idClient+"\""; 

try { 
    List<String> accessParameters=new ArrayList<String>(); 
    accessParameters.add(emailRic); 
    accessParameters.add(passwordRic); 
    accessParameters.add(stayLoggedRic); 
    accessParameters.add(idClientRic); 

    String parameterS=accessParameters.toString(); 
    String RPCx=BASE_URI+"{\"method\":\""+"startSession"+"\", \"accessParameters\":"; 
    RPCx+=parameterS; 
    RPCx+=", \"id\":1}"; 
    String RPCString=RPCx.toString(); 


    JSONArray postjson=new JSONArray(); 
    postjson.put(RPCString); 
    httppost.setHeader("RPCString",RPCString.toString()); 
    httppost.getParams().setParameter("jsonpost",postjson); 
    System.out.print(RPCString); 
    HttpResponse response = mClient.execute(httppost); 

    if(response != null){ 
     InputStream is = response.getEntity().getContent(); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 
     System.out.println("the answer is:\n"+sb); 
     String line = null; 

    try { 
      while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
      } 

     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
    // text = sb.toString(); 
    } 
    // tv.setText(text); 

}catch (ClientProtocolException e) { 
    // TODO Auto-generated catch block 
} catch (IOException e) { 
    // TODO Auto-generated catch block 
} 

但沒有成功。

當然,我的客戶端代碼有問題(web服務工作正常)。

回答

0

的語法錯誤,

我不能發送整個字符串作爲JSON請求以這種方式

String RPCx=BASE_URI+"{\"method\":\""+"startSession"+"\", \"accessParameters\":"; 

我應該創建一個JSONObject與方法屬性和嵌套JSONArray爲參數,進行這我必須發送根JSONObject到服務器。