• 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 }
  • 相关阅读:
    webpackdevserver 找不到目录
    Sublime text 实用插件 包推荐
    github for windows 安装
    很有创意的广告
    介绍一个软件SnippetCompiler
    Nodepad++ ftp github for windows组合开发php
    c#读取写入文本文件
    什么是临时,什么是长久,什么是永久?
    c#操作xml(读,写)
    php连接mssql数据库的几种方式
  • 原文地址:https://www.cnblogs.com/renzimu/p/4540812.html
Copyright © 2020-2023  润新知