iteye_17313 2012-07-03 10:47
浏览 415
已采纳

struts端如何设置action的方法为post提交的方式提交

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>

该如何设置,求帮助

  • 写回答

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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 chaquopy python 安卓
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题