Android Studio 中的模拟器向此电脑的Tomcat服务器发送请求,Tomcat服务器的SpringMVC处理http请求,

#报错信息:No Network Security Config specified, using platform default.Failed to connect to /192.168.xxxx:8080.

解决方案:AndroidMainfest.xml文件配置如下

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"    package="com.example.myapplication">    <!--- 添加网络权限 -->    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>    <uses-permission android:name="android.permission.INTERNET"/>    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>    <application        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"        <!--- 网络安全配置 -->        android:usesCleartextTraffic="true"        android:networkSecurityConfig="@xml/network_security_config">        <uses-library            android:name="org.apache.http.legacy"            android:required="false" />        <activity android:name=".ui.login.MainActivity"></activity>        <activity android:name=".ui.login.LogActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity></manifest>

在res目录下新建资源文件:

network_security_config.xml
Android连接SpringMVC配置信息_第1张图片
Android连接SpringMVC配置信息_第2张图片network_security_config.xml 文件配置信息:

<?xml version="1.0" encoding="utf-8"?><network-security-config>    <base-config cleartextTrafficPermitted="true" /></network-security-config>

LogActivity.java文件(仅仅用于测试是否能连接到Tomcat):

package com.example.myapplication.ui.login;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;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.myapplication.R;import com.example.myapplication.data.model.HttpUtils;import com.example.myapplication.data.model.NetUtils;import com.example.myapplication.data.model.User;import com.google.gson.Gson;import net.sf.json.JSONObject;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import okhttp3.Call;import okhttp3.Response;public class LogActivity extends AppCompatActivity {    private EditText ed_username;    private EditText ed_password;    private Button btn_login;    String name;    String password;    String urlPath;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_log);        btn_login = (Button)findViewById(R.id.btn_login);        btn_login.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View view) {                ed_username = findViewById(R.id.ed_username); //获取用户控件                name = ed_username.getText().toString(); //获取控件里面的值                ed_password = findViewById(R.id.ed_password);                password = ed_password.getText().toString();                /*if (name=="" || password=="") {                    Toast.makeText(getApplicationContext(),"用户名或密码为空",Toast.LENGTH_SHORT).show();                    return;                }*/                new Thread() {                    @Override                    public void run() {                        int num= login("langxing","123456");                        if(num>0){   //判断id是否大于1,>1就是查询到有数据 ,可以进入主界面                            Intent it=new Intent(LogActivity.this,MainActivity.class);                            it.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);                            startActivity(it);                        }else {                            runOnUiThread(new Runnable() {                                @Override                                public void run() {                                    Toast.makeText(getApplicationContext(),"用户名或密码错误",Toast.LENGTH_SHORT).show();                                }                            });                        }                    }                }.start();            }        });    }    //json格式与服务端交互    private int login(String name,String password){        //使用okhtttp连接服务器        //urlPath = "http://10.0.2.2:8080/emp_ssm/user/addUser.action?name="+name+"&"+"password="+password;        urlPath = "http://192.168.1.117:8080/emp_ssm/user/addUser.action";        try {            //1,找水源--创建URL            URL url = new URL(urlPath);//放网站            //2,开水闸--openConnection            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();            //3,建管道--InputStream            InputStream inputStream = httpURLConnection.getInputStream();            //4,建蓄水池蓄水-InputStreamReader            InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");            //5,水桶盛水--BufferedReader            BufferedReader bufferedReader = new BufferedReader(reader);            StringBuffer buffer = new StringBuffer();            String temp = null;            while ((temp = bufferedReader.readLine()) != null) {                //取水--如果不为空就一直取                buffer.append(temp);            }            bufferedReader.close();//记得关闭            reader.close();            inputStream.close();            Log.e("MAIN", buffer.toString());//打印结果        } catch (MalformedURLException e) {            e.printStackTrace();        } catch (IOException e) {            e.printStackTrace();        }        return  1;    }}

服务器端SpringMVC中userController.java 配置信息(仅仅用于测试):

package com.zhiyou100.controller;import javax.enterprise.inject.Model;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.omg.CORBA.Request;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestMethod;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.ResponseBody;import com.zhiyou100.pojo.User;import com.zhiyou100.service.UserService;@Controller@RequestMapping("/user")public class UserController {@Autowiredprivate UserService userService;    @RequestMapping(value="/addUser", method={RequestMethod.POST,RequestMethod.GET})    @ResponseBody    public User addUserFromClient(HttpServletRequest request){        String userName=request.getParameter("name");        String password=request.getParameter("password");        User user = new User();        user.setName(userName);        user.setPassword(password);        System.out.println(user.getName()+" hahahhhhh "+user.getPassword());        return user;    }}

更多相关文章

  1. Phone双模修改涉及文件列表
  2. Android 读取文件File的大小
  3. Android屏幕截图并保存截取屏幕的图片到指定文件
  4. android输出log信息
  5. Android 编译App报错 找不到android.support.annotation.Keep的
  6. Android 编译提示R文件找不到
  7. 总结系列-Android的文件系统

随机推荐

  1. mysql中like % %模糊查询的实现
  2. 详解MySQL like如何查询包含&#39;%&#39;
  3. MySQL模糊查询用法大全(正则、通配符、内
  4. MySQL中ESCAPE关键字的用法详解
  5. Mysql Sql 语句练习题(50道)
  6. 最全50个Mysql数据库查询练习题
  7. 在sql中实现取一行最大值或者最小值
  8. mysql 5.7 的 /etc/my.cnf 参数介绍
  9. MySQL kill指令使用指南
  10. MySQL 索引的一些细节分享