#报错信息:No Network Security Config specified, using platform default.Failed to connect to /192.168.xxxx:8080.
<?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>
network_security_config.xml
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 {
@Autowired
private 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;
}
}