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);
传递参数到实体主体不是更好吗?
不幸的是没有! :(我不明白我在做什么错误!也许服务器的代码是错误的。谢谢你的回答! – anna
嗨你想在服务器上做什么?我猜你正在使用JAX-WS来构建你的REST,JAX-WS主要用于处理POST中的XML(因此也包含请求中的Source),如果你正在构建简单的REST服务(就像你的问题所建议的那样),你可能需要检查http:// www。 vogella.de/articles/REST/article.html或另一种流行的Java REST框架http://www.restlet.org/ – momo
我已经提供了来自http://www.vogella.de/articles/REST/的示例article.html您可能正在寻找 – momo