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>
该如何设置,求帮助

struts端如何设置action的方法为post提交的方式提交
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
5条回答 默认 最新
- feizhuzi 2012-07-04 05:16关注
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]
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报