android网络编程之——客户端上传信息的代码


添加Java 发送http请求的代码。

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();
				}







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值