我想使用 java 作为 Web 服务器在 android 中制作登录应用程序,并在没有查询字符串的 java 中传递控



我想在Android中创建登录页面,对于Web服务,我选择了java,所以我想以json格式获取数据(我已经用java编写了代码(,现在对于登录,如果登录成功,我想存储成功,否则 fail.my 问题是我想将用户名和密码从Android中的控件传递到Java我应该怎么做我总是"失败",因为每次调用isUser((时空值在功能内部传递,尽管我的凭据是正确的

我的班级.java

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONString;
class MyClass {
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
int mobno;String passwd;
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DATABASE_URL = 
"jdbc:mysql://localhost/psamajdb";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
MyClass(){
this.passwd=passwd;
this.mobno=mobno;       
try {
Class.forName(JDBC_DRIVER);
con = DriverManager.getConnection(DATABASE_URL, USERNAME, PASSWORD);
stmt=con.createStatement();
} catch (Exception e) {
System.out.println("Exception e  = " + e);
}}
public JSONArray convertToJson(ResultSet rs) throws Exception{
JSONArray jsonArray = new JSONArray();
while (rs.next()) {
int total_rows = rs.getMetaData().getColumnCount();
for (int i = 0; i < total_rows; i++) {
JSONObject obj = new JSONObject();
obj.put(rs.getMetaData().getColumnLabel(i + 1)
.toLowerCase(), rs.getObject(i + 1));
jsonArray.put(obj);
}
}
return jsonArray;
}
public String[] jsonToString(JSONArray arr){
if(arr==null)
{
return null;
}
String[] str1=new String[arr.length()];
for (int i = 0; i < str1.length; i++) {
str1[i]=arr.optString(i);
}
return str1;
}
public JSONObject  isUser(int Mobno,String Passwd){
Mobno=mobno;
Passwd=passwd;
System.out.println(mobno);
System.out.println(passwd);
JSONObject obj=new JSONObject();
String qry="select id from info where mobno="+Mobno+" and 
passwd="+"'"+Passwd+"'";
PreparedStatement pst;
try {
stmt=con.createStatement();
rs=stmt.executeQuery(qry);
if(rs.next())
{
System.out.println("Successful");
String msg="success";
String value="Success";
obj=generateJson(msg,value);
}else
{
System.out.println("Failed");
String msg="failed";
String value="Failed";
obj=generateJson(msg,value);
}
} catch (SQLException ex) {
Logger.getLogger(MyServlet.class.getName()).log(Level.SEVERE, 
null, ex);
}
return obj;
}
private JSONObject generateJson(String msg, String value) {
JSONObject obj=new JSONObject();
if(value.equals("Success"))
{
try {
obj.put(msg, value);
} catch (JSONException ex) {
Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, 
null, ex);
}
}else{
try {
obj.put(msg, value);
} catch (JSONException ex) {
Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, 
null, ex);
}
}
return obj;  
}
}

我的服务.java

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONString;
@WebServlet(urlPatterns = {"/MyServlet"})
public class MyServlet extends HttpServlet {
String data;
private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
private static final String DATABASE_URL = 
"jdbc:mysql://localhost/testdb";
private static final String USERNAME = "root";
private static final String PASSWORD = "";
protected void processRequest(HttpServletRequest request, 
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet MyServlet</title>");            
out.println("</head>");
out.println("<body>");
doPost(request, response);         
out.println("</body>");
out.println("</html>");
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse 
response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse 
response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
int mobno;
String passwd;          
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try{   
MyClass obj=new MyClass();
mobno=obj.mobno;
passwd=obj.passwd;
System.out.println("here");
JSONObject jobj=obj.isUser(mobno, passwd);
out.println(jobj);
System.out.println(jobj);
}catch(Exception e){
out.println(e);
}    
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>

}

MyClass的构造函数当前如下所示:

MyClass()    {
this.passwd = passwd;  // logic error
this.mobno = mobno;    // logic error
... other lines omitted
}

在构造函数的上下文中,this.passwdpasswd引用同一对象。 由于它在构造函数中,因此值为 null。 检查 IDE 或编译器是否存在警告。 与另一行相同。

因此,您可能希望将passwdmobno作为构造函数的参数,如下所示:

MyClass(String passwd, int mobno)    {
this.passwd = passwd;
this.mobno = mobno;
... other lines omitted
}

以正确分配值。 需要询问request以获取这些值,以便可以正确创建新的MyClass实例。 询问请求取决于从应用发送值的方式。

相关内容

  • 没有找到相关文章

最新更新