i have the following code in my client:
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();
And my web service has the following code:
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);
}
}
}
The client communicates with the server, but the server reads the parameters username and password only when i put the parameters in the URL like this:
HttpPost httppost = new HttpPost("http://10.0.2.2:8888"+"?username=" + userna开发者_Go百科me1 + "&password=" + password1);
How can the server read the parameters from the entity body? I am trying to send the parameters from the client using this line:
UrlEncodedFormEntity p_entity = new UrlEncodedFormEntity(nvps, HTTP.UTF_8);
Isn't it better to pass the parameters in the entity body?
You could try the following and see if it works (I've used it before)
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);
Hope it works for you as well.
And if you are working on very simple Server side (as your question suggested) you might want to look at other framework that I mentioned below. Primarily look at this code example for 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");
}
精彩评论