Struts2+Android (3) 多种方式向服务器发送信息
来源:互联网 发布:网店运营优化整体方案 编辑:程序博客网 时间:2024/06/03 20:52
还是接上篇
修改了一些VideoManageAction
package com.su.action;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import java.io.OutputStream;import java.text.SimpleDateFormat;import java.util.Date;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.struts2.ServletActionContext;import com.opensymphony.xwork2.ActionSupport;import com.su.action.util.StreamTool;public class VideoManageAction extends ActionSupport {private String title;private String timelength;private File myFile; private String myFileFileName; public String getMethod() {return method;}public void setMethod(String method) {this.method = method;}private String method; public File getMyFile() {return myFile;}public void setMyFile(File myFile) {this.myFile = myFile;}public String getTitle() {return title;}public void setTitle(String title) {this.title = title;}public String getTimelength() {return timelength;}public void setTimelength(String timelength) {this.timelength = timelength;}public void setMyFileFileName(String myFileFileName) {this.myFileFileName = myFileFileName;}public String getMyFileFileName() {return myFileFileName;}public String execute() throws Exception {if (method.equals("getXml")) {InputStream inStream = ServletActionContext.getRequest().getInputStream();byte[] data = StreamTool.readInputStream(inStream);String xml = new String(data, "UTF-8");System.out.println("11111111111111111111");System.out.println(xml);}if (myFile!=null) {InputStream is = new FileInputStream(myFile);String uploadPath = ServletActionContext.getServletContext().getRealPath("/upload");File file = new File(uploadPath);if (!file.exists()) {file.mkdir();}File toFile = new File(uploadPath, this.getMyFileFileName());OutputStream os = new FileOutputStream(toFile);byte[] buffer = new byte[1024];int length = 0;while ((length = is.read(buffer)) > 0) {os.write(buffer, 0, length);}is.close();os.close();}System.out.println(this.getTimelength()+"\n"+this.getTitle()+"\n");return SUCCESS;}}
这次使用TestCase 用的时候加如activity吧
package cn.itcast.uploaddata;import java.io.InputStream;import java.util.HashMap;import java.util.Map;import cn.itcast.net.HttpRequest;import android.test.AndroidTestCase;import android.util.Log;public class HttpRequestTest extends AndroidTestCase {private static final String TAG = "HttpRequestTest";public void testSendXMLRequest() throws Throwable{String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><persons><person id=\"23\"><name>liming</name><age>30</age></person></persons>";HttpRequest.sendXML("http://10.1.27.35:8080/VideoWeb2/upload.action?method=getXml", xml);}public void testSendGetRequest() throws Throwable{//?method=save&title=xxxx&timelength=90Map<String, String> params = new HashMap<String, String>();//params.put("method", "save");params.put("title", "liming");params.put("timelength", "80");HttpRequest.sendGetRequest("http://10.1.27.35:8080/VideoWeb2/upload.action", params, "UTF-8");}public void testSendPostRequest() throws Throwable{Map<String, String> params = new HashMap<String, String>();//params.put("method", "save");params.put("title", "中国");params.put("timelength", "80");HttpRequest.sendPostRequest("http://10.1.27.35:8080/VideoWeb2/upload.action", params, "UTF-8");}public void testSendRequestFromHttpClient() throws Throwable{Map<String, String> params = new HashMap<String, String>();//params.put("method", "save");params.put("title", "传智播客");params.put("timelength", "80");boolean result = HttpRequest.sendRequestFromHttpClient("http://10.1.27.35:8080/VideoWeb2/upload.action", params, "UTF-8");Log.i("HttRequestTest", ""+ result);}}
package cn.itcast.net;import java.io.InputStream;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.URL;import java.net.URLEncoder;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import java.util.regex.Matcher;import java.util.regex.Pattern;import org.apache.http.HttpResponse;import org.apache.http.NameValuePair;import org.apache.http.client.entity.UrlEncodedFormEntity;import org.apache.http.client.methods.HttpPost;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.message.BasicNameValuePair;import org.xmlpull.v1.XmlPullParser;import android.util.Xml;import cn.itcast.utils.StreamTool;public class HttpRequest {public static boolean sendXML(String path, String xml)throws Exception{byte[] data = xml.getBytes();URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setRequestMethod("POST");conn.setConnectTimeout(5 * 1000);conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");conn.setRequestProperty("Content-Length", String.valueOf(data.length));OutputStream outStream = conn.getOutputStream();outStream.write(data);outStream.flush();outStream.close();if(conn.getResponseCode()==200){return true;}return false;}public static boolean sendGetRequest(String path, Map<String, String> params, String enc) throws Exception{StringBuilder sb = new StringBuilder(path);sb.append('?');// ?method=save&title=435435435&timelength=89&for(Map.Entry<String, String> entry : params.entrySet()){sb.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), enc)).append('&');}sb.deleteCharAt(sb.length()-1);URL url = new URL(sb.toString());HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(5 * 1000);if(conn.getResponseCode()==200){return true;}return false;}public static boolean sendPostRequest(String path, Map<String, String> params, String enc) throws Exception{// title=dsfdsf&timelength=23&method=saveStringBuilder sb = new StringBuilder();if(params!=null && !params.isEmpty()){for(Map.Entry<String, String> entry : params.entrySet()){sb.append(entry.getKey()).append('=').append(URLEncoder.encode(entry.getValue(), enc)).append('&');}sb.deleteCharAt(sb.length()-1);}byte[] entitydata = sb.toString().getBytes();//得到实体的二进制数据URL url = new URL(path);HttpURLConnection conn = (HttpURLConnection)url.openConnection();conn.setRequestMethod("POST");conn.setConnectTimeout(5 * 1000);conn.setDoOutput(true);//如果通过post提交数据,必须设置允许对外输出数据//Content-Type: application/x-www-form-urlencoded//Content-Length: 38conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");conn.setRequestProperty("Content-Length", String.valueOf(entitydata.length));OutputStream outStream = conn.getOutputStream();outStream.write(entitydata);outStream.flush();outStream.close();if(conn.getResponseCode()==200){return true;}return false;}//SSL HTTPS Cookiepublic static boolean sendRequestFromHttpClient(String path, Map<String, String> params, String enc) throws Exception{List<NameValuePair> paramPairs = new ArrayList<NameValuePair>();if(params!=null && !params.isEmpty()){for(Map.Entry<String, String> entry : params.entrySet()){paramPairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));}}UrlEncodedFormEntity entitydata = new UrlEncodedFormEntity(paramPairs, enc);//得到经过编码过后的实体数据HttpPost post = new HttpPost(path); //formpost.setEntity(entitydata);DefaultHttpClient client = new DefaultHttpClient(); //浏览器HttpResponse response = client.execute(post);//执行请求if(response.getStatusLine().getStatusCode()==200){return true;}return false;}}
- Struts2+Android (3) 多种方式向服务器发送信息
- Struts2+Android 多种方式向服务器发送信息
- Android-向服务器发送图片
- 服务器主动向客户端发送信息机制
- Android开发使用GET方式向服务器请求和发送数据
- Android开发使用POST方式向服务器请求和发送数据
- android通过httpPost向服务器发送请求
- android通过httpPost向服务器发送请求 .
- android通过httpPost向服务器发送请求
- Android向服务器发送心跳服务
- 使用struts2框架中3种客户端向服务端发送请求参数的方式
- android 向服务器Get和Post请求的两种方式,android向服务器发送文件,自己组装协议和借助第三方开源
- iOS网络开发基础--向服务器发送数据的方式
- 向服务器发送请求的三种方式
- Android调用WEBSERVER发送信息到服务器
- 向服务器发送XML
- 向服务器发送数据
- 向服务器发送请求
- union 和union all的区别
- 康托尔展开
- IOC与AOP
- 从头开始用 Ubuntu 12.04
- cookis and session
- Struts2+Android (3) 多种方式向服务器发送信息
- 3.UML通俗理解
- java小问题求解
- 黑马程序员-数据库2
- ACCEPT语句
- JSTL(JSP标准标签库/JavaServer Pages Standard Tag Library)
- TCP/IP网络协议学习笔记四--ICMP
- 黑马程序员---String类
- 单调队列+IO优化 POJ——Sliding Window