android客户端在访问struts框架搭建的服务端方法时,在struts端如何设置action的方法为post提交的方式提交(在客户端访问时需要传递一些json参数,get方式提交的无法在android端提交);
在struts中配置的信息:
<package name="json" extends="json-default">
<action name="selectById" class="UserAction" method="selectById">
<result type="json"></result> <!--返回值类型设置为json,不设置返回页面-->
</action>
</package>
该如何设置,求帮助
phone2Server方法里面有你要的
[code="java"]
package cn.itcast.web.java.client;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
public class Phone {
public static void main(String[] args) throws Exception {
URL url = new URL("http://127.0.0.1:8080/xxx");
URLConnection urlConnection = url.openConnection();
HttpURLConnection httpURLConnection = (HttpURLConnection) urlConnection;
phone2Server(httpURLConnection);
serverTophone(httpURLConnection);
}
private static void phone2Server(HttpURLConnection httpURLConnection)
throws IOException, UnsupportedEncodingException {
httpURLConnection.setDoOutput(true);
OutputStream os = httpURLConnection.getOutputStream();
PrintWriter pw = new PrintWriter(os);
String name = "杰克";
name = URLEncoder.encode(name,"UTF-8");
pw.write("username="+name+"&password=123");
pw.flush();
pw.close();
//设置提交方法
httpURLConnection.setRequestMethod("POST");
//取得服务端响应的状态码
int code = httpURLConnection.getResponseCode();
System.out.println(code);
}
private static void serverTophone(HttpURLConnection httpURLConnection) throws Exception {
//接收服务端的响应
InputStream is = httpURLConnection.getInputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len=is.read(buf))>0){
String msg = new String(buf,0,len);
System.out.println(msg);
}
is.close();
}
}
[/code]
提交方式是由客户端决定的,你服务器端怎么设置貌似意义不大!
页面中使用
method="post"
你貌似搞反了吧,设置提交方式,是在客户端中设置,而非服务器端。而且Android也提供了如何配置参数为post方式提交呀,你可以google下。
服务器端设置提交方式完全没有意义,是在Android客户端进行的,你可以参考下这篇文章http://liaokang-java.iteye.com/blog/1166083,也可以调用Android集成的HttpClient工具进行Post提交