本文主要根据代码实例来分析安卓客户端上传文本信息到网络的网页。
1、业务类实现,包括3个参数
public class ServiceSubmit { public static boolean submit(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 sendGetQuset(url,params, "UTF-8") ; } private static boolean sendGetQuset(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; } }
String name = new String(request.getParameter("name").getBytes("ISO8859-1"),"UTF-8") ; String age = request.getParameter("age") ; System.out.println("get method name is: " + name) ; System.out.println("get method pwd is: " + age) ; System.out.println("this is test. " ) ;
public void submitServer(String s1,String s2, String url) throws IOException{ boolean result = false ; result = ServiceSubmit.submit(s1,s2, url) ; if(result){ Toast.makeText(this, "this is ok,", 1).show(); }else{ Toast.makeText(this, "this is no ok,", 1).show(); } }
String url_one = "http://172.27.1.11:8081/TestWeb/GetMethod" ; String s1 = et_submit_1.getText().toString() ; String s2 = et_submit_2.getText().toString() ; try { submitServer(s1, s2, url_one); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }