我尝试使用Rest和post
从Android连接Tomcat服务器这是发布和回复的一部分
public void networkServiceModule(String ID, String Pwd){
Log.d("networkServiceModule","ID : "+ID);
Log.d("networkServiceModule","PW : "+Pwd);
Call<PostJson> thumbnailCall = ApplicationController.buildNetworkService().postLogin(new PostJson(ID,Pwd));
thumbnailCall.enqueue(new Callback<PostJson>() {
@Override
public void onResponse(Response<PostJson> response, Retrofit retrofit) {
if(response.isSuccess()) {
String resultCode = response.body().getResult_code().toString();
Log.d("networkServiceModule", "response Code : "+resultCode);
Toast.makeText(getBaseContext(), "Login : " + resultCode, Toast.LENGTH_SHORT).show();
} else {
int statusCode = response.code();
Log.d("networkServiceModule", "response Code : "+statusCode);
}
}
@Override
public void onFailure(Throwable t) {
Log.d("onFailure", "onFailure Code "+t.toString());
}
});
}
但每当我按“登录”时都会出现此错误。按钮。
java.net.ConnectException: failed to connect to /192.168.1.147 (port 8080) after 10000ms: isConnected failed: EHOSTUNREACH (No route to host)
这里有一些login.jsp的代码
<body>
<%
int login_status = 0; //0: empty data, 1: login success, 2: wrong pwd, 3: id not found
String ID = request.getParameter("id");
String PW = request.getParameter("pw");
String str = null;
String url = "http://192.168.1.6:8080/Attendance/login.jsp";
if (ID == null)
login_status = 0;
else if (PW == null)
login_status = 0;
else {
Connection conn = null;
Statement stmt = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/testdb?autoReconnect=true&useSSL=false", "root", "1234");
if (conn == null)
throw new Exception("db connection failed");
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM lists WHERE Email = '" + ID + "';");
if (rs.next()) { //existing ID
String Email = rs.getString("Email");
String Passwd = rs.getString("Passwd");
if (PW.equals(Passwd)) { //1: login success
login_status = 1;
}
...
}
JSONObject jsonObject = new JSONObject();
jsonObject.put("result_code", login_status);
out.print(jsonObject);
%>
</body>
.jsp有什么问题吗?
我想打印json,它只有一个int(result_code)作为java(Android studio)的响应。
这是我的NetworkService接口和Studio
上的PostJson类public interface NetworkService {
@POST("/Attendance/login.jsp")
Call<PostJson> postLogin(@Body PostJson postJson);
@GET("/Attendance/login.jsp")
Call<Login> getLogin();
}
-
public class PostJson {
String id;
String pw;
public PostJson(String Id, String Pw){
id = Id;
pw = Pw;
}
String result_code;
public String getResult_code(){
return result_code;
}
}
我是编程的初学者。 您的意见对我来说将是一个很大的帮助。 在此先感谢:)