失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 服务器下传Android端 本地Tomcat服务器接收android端上传的数据

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

时间:2024-05-23 11:34:56

相关推荐

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

8种机械键盘轴体对比

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

大体描述

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

Tomcat服务器端

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

超级简单:

ServletDemo1.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服务器控制台会输出账号和密码。界面

项目结构

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

可以看到建了两个包,一个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 .HttpURLConnection;

import .MalformedURLException;

import .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="/apk/res-auto"

xmlns:tools="/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"/>

效果

eclipse端输出:

如果觉得《服务器下传Android端 本地Tomcat服务器接收android端上传的数据》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。