2011-08-13 63 views
0

我在我的客戶端下面的代碼:服務器 - 從請求讀取參數(POST)

Log.v(TAG, "Trying to Login"); 
    HttpClient client = new DefaultHttpClient(); 
    EditText etxt_user = (EditText) findViewById(R.id.username); 
    EditText etxt_pass = (EditText) findViewById(R.id.password); 
    String username1 = etxt_user.getText().toString(); 
    String password1 = etxt_pass.getText().toString(); 
    HttpPost httppost = new HttpPost("http://10.0.2.2:8888"); 
    Log.v(TAG, "message1");   
    //add your Data 
    List<BasicNameValuePair> nvps = new ArrayList<BasicNameValuePair>(); 
    nvps.add(new BasicNameValuePair("username", username1)); 
    nvps.add(new BasicNameValuePair("password", password1)); 

    try { 
      UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8); 
      httppost.setEntity(p_entity); 
      //Execute HTTP Post Request 
      HttpResponse response = client.execute(httppost); 

      Log.v(TAG,"message2"); 
      Log.v(TAG, response.getStatusLine().toString()); 
      HttpEntity responseEntity = response.getEntity(); 

而且我的web服務具有下面的代碼:

public Source invoke(Source request){ 
     String replyElement = new String("hello world"); 
     StreamSource reply = new StreamSource(new StringReader(replyElement)); 
     String replyElement2 = new String("hello world 2"); 
     StreamSource reply2 = new StreamSource(new StringReader(replyElement2)); 
     String amount = null; 
     if (ws_ctx == null)throw new RuntimeException("DI failed on ws_ctx."); 
     if (request == null) { 
      System.out.println("Getting input from query string"); 
      // Grab the message context and extract the request verb. 
      MessageContext msg_ctx = ws_ctx.getMessageContext(); 
      String x = msg_ctx.toString(); 
      System.out.println("The value" + x + "was received from the client"); 
      String http_verb = (String)msg_ctx.get(MessageContext.HTTP_REQUEST_METHOD); 
      System.out.println(http_verb); 
      String query = (String)msg_ctx.get(MessageContext.QUERY_STRING); 
      System.out.println("Query String = " + query); 
      if(query == null) 
      { 
       System.out.println("The query variable has zero value!!!!!"); 
      } 
      else 
      { 
       System.out.println("The value of the query variable is:" + query); 
      } 

      http_verb = http_verb.trim().toUpperCase() 

     } else { 
      System.out.println("Getting input from input message"); 
      Node n = null; 
      if (request instanceof DOMSource) { 
       n = ((DOMSource) request).getNode(); 
      } else if (request instanceof StreamSource) { 
       StreamSource streamSource = (StreamSource) request; 
       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
       DocumentBuilder db = dbf.newDocumentBuilder(); 
       InputSource inputSource = null; 
       if (streamSource.getInputStream() != null) { 
        inputSource = new InputSource(streamSource.getInputStream()); 
       } else if (streamSource.getReader() != null) { 
        inputSource = new InputSource(streamSource.getReader()); 
       } 
       n = db.parse(inputSource); 
      } else { 
       throw new RuntimeException("Unsupported source: " + request); 
      } 

     } 

    return reply2; 
    } 
    catch(Exception e){ 
     e.printStackTrace(); 
     throw new HTTPException(500); 
    } 

} 

}

客戶端與服務器通信,但服務器只讀取參數的用戶名和密碼,當我把這樣的參數在URL中:

HttpPost httppost = new HttpPost("http://10.0.2.2:8888"+"?username=" + username1 + "&password=" + password1); 

服務器如何從實體主體讀取參數?我試圖從客戶端使用此行發送參數:

UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8); 

傳遞參數到實體主體不是更好嗎?

回答

0

你可以嘗試以下,看看它是否工作(我以前用過它)


    StringEntity params = new StringEntity("username=" + username1 + "&password=" + password1);   
    HttpPost request = new HttpPost(path); 
    HttpClient httpClient = new DefaultHttpClient(); 
    request.addHeader("content-type", "application/x-www-form-urlencoded"); 
    request.setEntity(params); 

希望它爲你工作爲好。

如果你在非常簡單的服務器端工作(如你的問題建議),你可能想看看我在下面提到的其他框架。主要看JAX-RS的代碼示例:



    @POST 
     @Produces(MediaType.TEXT_HTML) 
     @Consumes(MediaType.APPLICATION_FORM_URLENCODED) 
     public void newTodo(
       @FormParam("id") String id, 
       @FormParam("summary") String summary, 
       @FormParam("description") String description, 
       @Context HttpServletResponse servletResponse 
     ) throws IOException { 
      Todo todo = new Todo(id,summary); 
      if (description!=null){ 
       todo.setDescription(description); 
      } 
      TodoDao.instance.getModel().put(id, todo); 

      URI uri = uriInfo.getAbsolutePathBuilder().path(id).build(); 
      Response.created(uri).build(); 

      servletResponse.sendRedirect("../create_todo.html"); 
     } 

+0

不幸的是沒有! :(我不明白我在做什麼錯誤!也許服務器的代碼是錯誤的。謝謝你的回答! – anna

+0

嗨你想在服務器上做什麼?我猜你正在使用JAX-WS來構建你的REST,JAX-WS主要用於處理POST中的XML(因此也包含請求中的Source),如果你正在構建簡單的REST服務(就像你的問題所建議的那樣),你可能需要檢查http:// www。 vogella.de/articles/REST/article.html或另一種流行的Java REST框架http://www.restlet.org/ – momo

+0

我已經提供了來自http://www.vogella.de/articles/REST/的示例article.html您可能正在尋找 – momo