服务器下传Android端,本地Tomcat服务器接收android端上传的数据

66b52468c121889b900d4956032f1009.png

8种机械键盘轴体对比

本人程序员,要买一个写代码的键盘,请问红轴和茶轴怎么选?

大体描述

android端提交账号和密码,Tomcat服务器端会接收到,eclipse的控制台输出得到的账号和密码。

Tomcat服务器端

先建立了一个javaweb项目,我这项目名是ConnectTest,然后在建立了一个包,在这个包下建立了一个servlet文件,我这命名为ServletDemo1。建立完servlet文件后,千万不要忘记在web.xml中注册,下面我将贴一下具体代码实现和项目结构。项目结构

超级简单:

d729a0e53f375722e3a0512e3764ef2b.pngServletDemo1.java1

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

31package com.servlet.demo;

import java.io.IOException;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

public class extends HttpServlet{

private static final long serialVersionUID = 1L;

public (){

super();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

doPost(request, response);

}

protected void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

String username = request.getParameter("username");

String password = request.getParameter("password");

System.out.println("-----> doPost username:" + username + " password:" + password);

}

}web.xml

也很简单就是对上面的那个servlet进行注册:1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

ConnectTest

index.html

index.htm

index.jsp

default.html

default.htm

default.jsp

ServletDemo1

com.servlet.demo.ServletDemo1

ServletDemo1

/ServletDemo1

android端

因为只是一个简单的例子,所以页面很简单,两个输入框,一个输入账号,一个输入密码,点击发送按钮后,账号和密码会提交到Tomcat服务器端,所以eclipse这边的Tomcat服务器控制台会输出账号和密码。界面

003.png项目结构

这里只贴上java代码实现部分的结构,布局部分的默认就行:

96dbedb4df06ba732b3c599754b3c4a4.png

可以看到建了两个包,一个client是存放线程类的,另外一个主界面类。HttpThread.java1

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

48package com.example.client;

import android.util.Log;

import java.io.IOException;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

public class HttpThread extends Thread{

private String url;

private String username;

private String password;

public HttpThread(String url, String username, String password){

this.url=url;

this.username=username;

this.password=password;

}

public void send() throws IOException {

//将username和password传给Tomcat服务器

url=url+"?username="+username+"&password="+password;

try {

Log.i("测试", "start"); //Log.i我用来测试调试的。

URL httpUrl=new URL(url);

//获取网络连接

HttpURLConnection coon=(HttpURLConnection)httpUrl.openConnection();

//设置请求方法为Post

coon.setRequestMethod("POST");

//设置访问超时时间

coon.setReadTimeout(5000);

//调用getInputStream方法后,服务端才会收到请求,并阻塞式地接收服务端返回的数据

coon.getInputStream();

} catch (MalformedURLException e) {

e.printStackTrace();

}

}

public void run(){

super.run();

try {

send();

}catch (IOException e){

e.printStackTrace();

}

}

}MainActivity.java: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

38package com.example.connecttest;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

import com.example.client.HttpThread;

public class MainActivity extends AppCompatActivity{

private EditText username;

private EditText password;

private Button signup;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

username=(EditText)findViewById(R.id.account);

password=(EditText)findViewById(R.id.password);

signup=(Button)findViewById(R.id.btnSign);

signup.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

Log.i("1111", "1111111");

onLogin();

Toast.makeText(MainActivity.this,"success",Toast.LENGTH_SHORT).show();

}

});

}

private void onLogin() {

String url="http://192.168.2.133:8080/ConnectTest/ServletDemo1";

new HttpThread(url,username.getText().toString().trim(),password.getText().toString().trim()).start();

}

}AndroidManifest.xml1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22<?xml version="1.0" encoding="utf-8"?>

package="com.example.connecttest">

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:roundIcon="@mipmap/ic_launcher_round"

android:supportsRtl="true"

android:theme="@style/AppTheme">

布局 activity_main.xml1

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<?xml version="1.0" encoding="utf-8"?>

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

android:layout_marginTop="100dp"

android:id="@+id/account"

android:layout_width="match_parent"

android:layout_height="60dp"

android:text="请输入账号:"

android:background="#FCF2A4"/>

android:layout_marginTop="5dp"

android:id="@+id/password"

android:layout_width="match_parent"

android:layout_height="60dp"

android:text="请输入密码:"

android:background="#FCF2A4"/>

android:layout_marginTop="5dp"

android:id="@+id/btnSign"

android:layout_width="match_parent"

android:layout_height="50dp"

android:text="发送"

android:background="#67BAFD"/>

效果

003.png

eclipse端输出:

854b9c859384c2c5ec02a67fa5ee7ed1.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值