android與javaee通訊:登入介面超級簡化版

來源:互聯網
上載者:User

標籤:

今天完成了利用android和tomcat伺服器完成了簡單介面的通訊。

主要就是使用者輸入id和密碼,介面顯示登入成功還是失敗。

昨天完成代碼後,尚未來得及調試,今天通過調試,發現了以下幾點錯誤:

1.測試的時候忘了啟動tomcat伺服器(好吧這是一個很蠢的錯誤。。)

2.用戶端設定的編碼格式中的"UTF-8"寫成了"UTF_8"(好吧我知道這也很蠢。。。)

3.傳送資料的時候服務端返回的資料和用戶端接收資料的格式不一致,導致了亂碼。

4.試圖在新開的用於訪問網路的線程中更改用戶端顯示介面導致錯誤。

通過調試,成功的解決了這些問題,最後實現了簡化登入。實現步驟大致如下:

1》在資料庫中建立使用者表(我只設定了id,password參數)

2》在java ee中建立java類user

3>在user中寫出擷取使用者資訊的方法以便在servlet中調用(部分代碼)

public int findUser(int id,String password) throws ClassNotFoundException, SQLException{
//建立和資料庫的連結,尋找使用者是否存在

Class.forName("com.mysql.jdbc.Driver");

//這裡填入資訊即可
String dbName = "";
String tableName = "r";
String user = "";
String mysql_password = "i";

String url = "jdbc:mysql://localhost:3306/" + dbName + "?user="
+user + "&password=" + mysql_password;

Connection conn = (Connection) DriverManager.getConnection(url);

//擷取資料庫資料,執行查詢操作
Statement statement = (Statement) conn.createStatement();

String sql = "select * from " + tableName;

ResultSet rs = (ResultSet) statement.executeQuery(sql);

//存在狀態量標誌,1代表存在,0代表不存在
int exist = 0;

while(rs.next()){
int user_name = rs.getInt(1);
String user_password = rs.getString(2);
if(user_name == id && user_password.equals(password)){
exist = 1;
break;
}

}
return exist;
}

4》建立servlet檔案,寫入響應及回複代碼(部分代碼):

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
request.setCharacterEncoding("UTF-8");
response.setContentType("text/json;charset=UTF-8");
String reqMessage,respMessage;
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"UTF-8"));
StringBuffer sb = new StringBuffer("");
String temp;
while((temp = br.readLine()) != null){
sb.append(temp);
}
br.close();

reqMessage = sb.toString();
JSONArray jsonArray = JSONArray.fromObject(reqMessage);
JSONObject data = (JSONObject)jsonArray.get(0);
int user_id = data.getInt("id");
String user_password = data.getString("password");
user u = new user();
int exist = 0;
try {
exist = u.findUser(user_id, user_password);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
PrintWriter pw = response.getWriter();
pw.write(exist+"");
pw.flush();
pw.close();

}

 

至此,服務端的代碼就基本完成啦,然後就是用戶端的代碼了。

用戶端的核心代碼如下(部分代碼):

HttpParams httpParams = new BasicHttpParams();
HttpClient hc = new DefaultHttpClient(httpParams);
String addr = "http://172.16.121.16:8080/xiangyue/loginServlet";
HttpPost hp = new HttpPost(addr);
System.out.println("串連成功!");
System.out.println(reqValue);
try {
hp.setEntity(new StringEntity(reqValue.toString(),"UTF-8"));
HttpResponse hr = hc.execute(hp);

if(hr.getStatusLine().getStatusCode() == 200){
returnValue = EntityUtils.toString(hr.getEntity(),"UTF-8");
System.out.println(returnValue);
if(returnValue.equals("1")){
//System.out.println("登入成功!");
//Toast.makeText(MainActivity.this,"登入成功!",Toast.LENGTH_LONG).show();
exist = 1;
}
else{
//System.out.println("此使用者不存在!");
//Toast.makeText(MainActivity.this,"使用者名稱或密碼錯誤!",Toast.LENGTH_LONG).show();
exist = 0;
}
}else{
System.out.println("響應失敗!");
}
} catch (UnsupportedEncodingException e) {
// TODO 自動產生的 catch 塊
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO 自動產生的 catch 塊
e.printStackTrace();
} catch (IOException e) {
// TODO 自動產生的 catch 塊
e.printStackTrace();
}

 

至此核心代碼就完成了,稍微添加介面什麼的就可以進行測試了(所需包需全部成功匯入,環境配置完成)。如果有什麼需要修改最佳化的地方,望大神指教~

 

android與javaee通訊:登入介面超級簡化版

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.