所以我對此很新穎。我正在關注網絡上的一個教程,展示瞭如何連接這兩個教程。但是我所做的每件事都只是跳起來抓住一部分,它說「沒有連接」。我錯過了什麼?我還連接到一個http服務器,使用PHP腳本查詢數據庫中的用戶名和密碼。將Android連接到MySQL
try
{
nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("username", username));
nameValuePairs.add(new BasicNameValuePair("password", password));
httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpClient.execute(httpPost);
if(response.getStatusLine().getStatusCode()==200)
{
entity = response.getEntity();
if(entity != null)
{
InputStream instream = entity.getContent();
JSONObject jsonResponse = new JSONObject(instream.toString());
String retUser = jsonResponse.getString("upime");
String retPass = jsonResponse.getString("geslo");
if(username.equals(retUser) && password.equals(retPass))
{
SharedPreferences sp = getSharedPreferences("logindetails", 0);
SharedPreferences.Editor spedit = sp.edit();
spedit.putString("upime", username);
spedit.putString("geslo", password);
spedit.commit();
Toast.makeText(getBaseContext(), "Login successful", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), "Failed to login", Toast.LENGTH_SHORT).show();
}
}
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "No connection", Toast.LENGTH_LONG).show();
}
可以使用給我輸出的時候 「無連接」 被替換爲e.getMessage() – 2015-01-21 11:38:12
如果我使用e.getMessage()而不是「無連接」字符串? – Polkovnik 2015-01-21 11:44:02
你得到什麼? – 2015-01-21 11:45:01