zoukankan      html  css  js  c++  java
  • Android 开发工具类 28_sendGETRequest

    以 GET 方式上传数据,小于 2K,且安全性要求不高的情况下。

     1 package com.wangjialin.internet.userInformation.service;
     2 
     3 import java.net.HttpURLConnection;
     4 import java.net.URL;
     5 import java.net.URLEncoder;
     6 import java.util.HashMap;
     7 import java.util.Map;
     8 
     9 public class UserInformationService {
    10     
    11     public static boolean save(String title, String length) throws Exception{
    12         
    13         String path = "http://192.168.1.103:8080/ServerForGETMethod/ServletForGETMethod";
    14         Map<String, String> params = new HashMap<String, String>();
    15         params.put("name", title);
    16         params.put("age", length);
    17         
    18         return sendGETRequest(path, params, "UTF-8");
    19     }    
    20     
    21     /**
    22      * 发送GET请求
    23      * @param path 请求路径
    24      * @param params 请求参数
    25      * @return
    26      */
    27     private static boolean sendGETRequest(String path, Map<String, String> params, String encoding) throws Exception{
    28         // http://192.178.1.103:8080/ServerForGETMethod/ServletForGETMethod?title=xxxx&length=90
    29         StringBuilder sb = new StringBuilder(path);
    30         
    31         if(params != null && !params.isEmpty()){
    32             sb.append("?");
    33             for(Map.Entry<String, String> entry : params.entrySet()){
    34                 sb.append(entry.getKey()).append("=");
    35                 sb.append(URLEncoder.encode(entry.getValue(), encoding));
    36                 sb.append("&");
    37             }
    38             sb.deleteCharAt(sb.length() - 1);
    39         }
    40         HttpURLConnection conn = (HttpURLConnection) new URL(sb.toString()).openConnection();
    41         conn.setConnectTimeout(5000);
    42         conn.setRequestMethod("GET");
    43         
    44         if(conn.getResponseCode() == 200){
    45             return true;
    46         }
    47         return false;
    48     }
    49 }

    UserInformationActivity

     1 package com.wangjialin.internet.userInformation.get;
     2 
     3 import android.app.Activity;
     4 import android.os.Bundle;
     5 import android.view.View;
     6 import android.widget.EditText;
     7 import android.widget.Toast;
     8 
     9 import com.wangjialin.internet.userInformation.service.UserInformationService;
    10 
    11 public class UserInformationActivity extends Activity {
    12     
    13      private EditText titleText;
    14         private EditText lengthText;
    15         
    16         @Override
    17         public void onCreate(Bundle savedInstanceState) {
    18             super.onCreate(savedInstanceState);
    19             setContentView(R.layout.main);
    20             
    21             titleText = (EditText) this.findViewById(R.id.title);
    22             lengthText = (EditText) this.findViewById(R.id.length);
    23         }
    24         
    25         public void save(View v){
    26             String title = titleText.getText().toString();
    27             String length = lengthText.getText().toString();
    28             try {
    29                 boolean result = false;
    30                 
    31                 result = UserInformationService.save(title, length);
    32             
    33                 if(result){
    34                     Toast.makeText(this, R.string.success, 1).show();
    35                 }else{
    36                     Toast.makeText(this, R.string.fail, 1).show();
    37                 }
    38             } catch (Exception e) {
    39                 e.printStackTrace();
    40                 Toast.makeText(this, R.string.fail, 1).show();
    41             }
    42         }
    43 }
  • 相关阅读:
    linux 文件系统(inode和block)
    vue状态管理vuex从浅入深详细讲解
    小白都能看懂的vue中各种通信传值方式,附带详细代码
    CSS3移动端vw+rem不依赖JS实现响应式布局
    JavaScript原生封装ajax请求和Jquery中的ajax请求
    永久解决Sublime包管理package control 打开install package报错 There are no packages available for installation
    从GitLab上创建分支本地拉取项目和提交项目详解
    前端路由的两种实现方式,内附详细代码
    几个例子理解浅拷贝和深拷贝
    讲解JavaScript中对闭包的理解
  • 原文地址:https://www.cnblogs.com/renzimu/p/4540812.html
Copyright © 2011-2022 走看看