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







你可能感兴趣的:(android网络编程之——客户端上传信息的代码)