android网络编程之——客户端上传信息的代码
2015-11-20 15:38
471 查看
添加Java 发送http请求的代码。
调用方法如下:
public void submitPostServer(String s1,String s2, String url) throws IOException{
boolean result = false ;
result = ServiceSubmit.submitPost(s1,s2, url) ;
if(result){
Toast.makeText(this, "this is ok,", 1).show();
}else{
Toast.makeText(this, "this is no ok,", 1).show();
}
}
package com.my.hello; import java.io.IOException; import java.io.OutputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map; import android.content.IntentSender.SendIntentException; public class ServiceSubmit { public static boolean submitGet(String s1, String s2, String url) throws IOException{ Map<String,String> params = new HashMap<String,String>() ; params.put("name", s1) ; params.put("age", s2) ; return sendGetQuest(url,params, "UTF-8") ; } public static boolean submitPost(String s1, String s2, String url) throws IOException{ Map<String,String> params = new HashMap<String,String>() ; params.put("name", s1) ; params.put("age", s2) ; return sendPostQuest(url,params, "UTF-8") ; } public static boolean sendXMLQuest(String xml, String url) throws IOException{ byte[] data = xml.getBytes() ; URL path = new URL(url) ; HttpURLConnection conn = (HttpURLConnection) path.openConnection() ; conn.setRequestMethod("POST") ; conn.setDoOutput(true); conn.setConnectTimeout(5 * 1000); conn.setRequestProperty("Content-Type", "text/xml"); conn.setRequestProperty("Content-Length", String.valueOf(data.length)); return false; } private static boolean sendGetQuest(String url, Map<String, String> params, String encoding) throws IOException { StringBuilder sb = new StringBuilder(url) ; if(params != null && !params.isEmpty()){ sb.append("?") ; for(Map.Entry<String, String> entry:params.entrySet()){ sb.append(entry.getKey()).append("=") ; sb.append(URLEncoder.encode(entry.getValue(),encoding)) ; sb.append("&") ; } sb.deleteCharAt(sb.length()-1) ; } HttpURLConnection conn = (HttpURLConnection) new URL(sb.toString()).openConnection() ; conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if(conn.getResponseCode() == 200){ return true ; } return false; } private static boolean sendPostQuest(String url, Map<String, String> params, String encoding) throws IOException { StringBuilder sb = new StringBuilder() ; if(params != null && !params.isEmpty()){ for(Map.Entry<String, String> entry:params.entrySet()){ sb.append(entry.getKey()).append("=") ; sb.append(URLEncoder.encode(entry.getValue(),encoding)) ; sb.append("&") ; } sb.deleteCharAt(sb.length()-1) ; } byte[] data = sb.toString().getBytes() ; HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection() ; conn.setConnectTimeout(5000); conn.setRequestMethod("POST"); conn.setDoOutput(true); //允许对外传输数据 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); conn.setRequestProperty("Content-Length", data.length + ""); OutputStream os = conn.getOutputStream() ; os.write(data); os.flush(); if(conn.getResponseCode() == 200){ return true ; } return false; } }
调用方法如下:
public void submitPostServer(String s1,String s2, String url) throws IOException{
boolean result = false ;
result = ServiceSubmit.submitPost(s1,s2, url) ;
if(result){
Toast.makeText(this, "this is ok,", 1).show();
}else{
Toast.makeText(this, "this is no ok,", 1).show();
}
}
case R.id.bt_submit: String url_one = "http://172.27.251.31:8081/TestWeb/GetMethod" ; String s1 = et_submit_1.getText().toString() ; String s2 = et_submit_2.getText().toString() ; try { submitPostServer(s1, s2, url_one); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
相关文章推荐
- Nginx配置https访问
- 【转】Jmeter Http请求界面解释
- 虚拟机ping不通主机
- 关于 Xcode7网络请求的Http 协议改为 Https 协议的程序修改
- HTTP客户端服务端TCP连接内幕
- TCP连接探测中的Keepalive和心跳包
- ab和jmeter进行GET/POST压力测试的使用心得和比较
- [python&php 网络编程]设定并获取socket超时时间
- TCP的状态(SYN FIN ACK PSH RST URG)
- HTTP Method 之 Post VS. Get
- IIS7 IIS7.5 IIS8.5 HTTP 错误 500.19 – Internal Server Error解决方案小记
- http get with body data
- Windows Azure Virtual Machine (27) 使用psping工具,测试Azure VM网络连通性
- 网络爬虫雏形1号
- HttpClient4.3 使用经验(一) 简单使用
- DynamicLoadApk 源码解析 http://codekk.com/open-source-project-analysis
- HTTP请求报文与响应报文介绍
- http://blog.zhaojie.me/
- Java网络编程
- c++利用mongoose实现http服务