Android 通过Get/Post提交请求数据到服务器


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

package com.lifengfeng.submitdata.utils;

 

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java.net.URLConnection;

import java.net.URLEncoder;

 

import android.util.Log;

 

public class NetUtils {

 

    private static final String TAG = "NetUtils";

     

    /**

     * 使用post的方式登录

     * @param userName

     * @param password

     * @return

     */

    public static String loginOfPost(String userName, String password) {

        HttpURLConnection conn = null;

        try {

            URL url = new URL("http://10.0.2.2:8080/Server/servlet/LoginServlet");

             

            conn = (HttpURLConnection) url.openConnection();

             

            conn.setRequestMethod("POST");

            conn.setConnectTimeout(10000); // 连接的超时时间

            conn.setReadTimeout(5000); // 读数据的超时时间

            conn.setDoOutput(true); // 必须设置此方法, 允许输出

//          conn.setRequestProperty("Content-Length", 234);     // 设置请求头消息, 可以设置多个

             

            // post请求的参数

            String data = "username=" + userName + "&password=" + password;

             

            // 获得一个输出流, 用于向服务器写数据, 默认情况下, 系统不允许向服务器输出内容

            OutputStream out = conn.getOutputStream(); 

            out.write(data.getBytes());

            out.flush();

            out.close();

             

            int responseCode = conn.getResponseCode();

            if(responseCode == 200) {

                InputStream is = conn.getInputStream();

                String state = getStringFromInputStream(is);

                return state;

            } else {

                Log.i(TAG, "访问失败: " + responseCode);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            if(conn != null) {

                conn.disconnect();

            }

        }

        return null;

    }

 

    /**

     * 使用get的方式登录

     * @param userName

     * @param password

     * @return 登录的状态

     */

    public static String loginOfGet(String userName, String password) {

        HttpURLConnection conn = null;

        try {

            String data = "username=" + URLEncoder.encode(userName) + "&password=" + URLEncoder.encode(password);

            URL url = new URL("http://10.0.2.2:8080/Server/servlet/LoginServlet?" + data);

            conn = (HttpURLConnection) url.openConnection();

             

            conn.setRequestMethod("GET");       // get或者post必须得全大写

            conn.setConnectTimeout(10000); // 连接的超时时间

            conn.setReadTimeout(5000); // 读数据的超时时间

             

            int responseCode = conn.getResponseCode();

            if(responseCode == 200) {

                InputStream is = conn.getInputStream();

                String state = getStringFromInputStream(is);

                return state;

            } else {

                Log.i(TAG, "访问失败: " + responseCode);

            }

        } catch (Exception e) {

            e.printStackTrace();

        } finally {

            if(conn != null) {

                conn.disconnect();      // 关闭连接

            }

        }

        return null;

    }

     

    /**

     * 根据流返回一个字符串信息

     * @param is

     * @return

     * @throws IOException

     */

    private static String getStringFromInputStream(InputStream is) throws IOException {

        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        byte[] buffer = new byte[1024];

        int len = -1;

         

        while((len = is.read(buffer)) != -1) {

            baos.write(buffer, 0, len);

        }

        is.close();

         

        String html = baos.toString();  // 把流中的数据转换成字符串, 采用的编码是: utf-8

         

//      String html = new String(baos.toByteArray(), "GBK");

         

        baos.close();

        return html;

    }

}

时间: 2024-09-02 03:10:06

Android 通过Get/Post提交请求数据到服务器的相关文章

android post方式提交中文数据到服务器乱码问题

问题描述 android post方式提交中文数据到服务器乱码问题 Android端代码 AsyncHttpClient client = new AsyncHttpClient(); //URLEncoder.encode(categoryutf-8""); RequestParams params = new RequestParams(); if(path1!=null){ file1=new File(path1); } if(path2!=null){ file2=new F

android 如何用 http GET请求方式向服务器发送数据,并接收返回的数据

问题描述 android 如何用 http GET请求方式向服务器发送数据,并接收返回的数据 就发送自定义的一个字符串s,求一个简单的例子,GET请求方式向服务器发送数据,并接收返回的数据 解决方案 public class HttpUtil { private static final int CONNECT_SUCESS = 200; public static void sendHttpRequest(final String address, final HttpCallBackList

【黑马Android】(05)短信/查询和添加/内容观察者使用/子线程网络图片查看器和Handler消息处理器/html查看器/使用HttpURLConnection采用Post方式请求数据/开源项目

备份短信和添加短信 操作系统短信的uri: content://sms/ <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.itheima28.backupsms" android:versionCode="1

长城电脑数据中心服务器电源技术获奖

本文讲的是长城电脑数据中心服务器电源技术获奖,[IT168 资讯]"2014年度中国电子学会科学技术奖"于日前正式揭晓,中国长城计算机深圳股份有限公司(简称长城电脑)提交的"数据中心服务器电源技术研发与产业化项目"以其领先的创新技术成功荣获"科技进步类二等奖".该项目研制难度大,创新性强,属国内首创,不仅是长城电脑在电源及服务器领域长期以来深厚技术积累的结晶,也填补了国内空白,打破了我国在高端服务电源领域长期依赖进口的尴尬局面,代表着我国在高端服

详解Android提交数据到服务器的两种方式四种方法_Android

Android应用开发中,会经常要提交数据到服务器和从服务器得到数据,本文主要是给出了利用http协议采用HttpClient方式向服务器提交数据的方法. 代码比较简单,这里不去过多的阐述,直接看代码. /** * @author Dylan * 本类封装了Android中向web服务器提交数据的两种方式四种方法 */ public class SubmitDataByHttpClientAndOrdinaryWay { /** * 使用get请求以普通方式提交数据 * @param map 传

详解Android提交数据到服务器的两种方式四种方法

Android应用开发中,会经常要提交数据到服务器和从服务器得到数据,本文主要是给出了利用http协议采用HttpClient方式向服务器提交数据的方法. 代码比较简单,这里不去过多的阐述,直接看代码. /** * @author Dylan * 本类封装了Android中向web服务器提交数据的两种方式四种方法 */ public class SubmitDataByHttpClientAndOrdinaryWay { /** * 使用get请求以普通方式提交数据 * @param map 传

Android 经典笔记之八:网络请求数据基础介绍

关于网络请求数据总结 目录介绍 1.Http请求与响应 1.1 Http请求包的结构 1.2 HTTP响应包结构 2.Http请求方式 3.Get和Post的比较 3.1 get请求 3.2 post请求 3.3 其他区别 3.4 网络心声 4.Http响应方式 5.同步和异步 6.Http缓存机制讲解 6.1 request请求字段含义 6.2 response响应字段含义 6.3 缓存机制逻辑图 0.本人写的综合案例 案例 说明及截图 模块:新闻,音乐,视频,图片,唐诗宋词,快递,天气,记事

服务器-急...通过中文请求数据,为什么在android 5.0上不需要编码,而在4.04上需要?

问题描述 急...通过中文请求数据,为什么在android 5.0上不需要编码,而在4.04上需要? 通过传入一个中文str请求服务器数据,服务端有URLDecoder.decode(str)处理,在 安卓终端,为什么在android 5.0上不需要对对中文str进行处理,而在4.04上需要对str 进行URLEncoder.encode(str)处理,如果不处理,返回结果为空,难道是android 5.0 上默认做了URLEncoder.encode(str)吗??? 解决方案 加了默认编码

超时-如何实现Android在规定时间内若http请求的数据还没返回就直接关闭请求

问题描述 如何实现Android在规定时间内若http请求的数据还没返回就直接关闭请求 在Android上进行http网络访问时,如果在规定时间内网络还没返回数据,就跳转到另一个界面,请问这个该怎么实现? 解决方案 设置超时时间 con.setConnectTimeout (6000); 关闭请求可以抛出异常中写 解决方案二: Android 浏览器直接请求链接有返回数据,但在程序中请求无返回. 解决方案三: HTTP协议使用TCP协议传输的,因此在传输数据之前采用三次握手建立TCP连接.第4个