我想建立一個Android與PHP和MySQL登錄應用程序。 我使用下面的代碼。當我運行應用程序時,我總是在屏幕上看到消息「連接錯誤」。我應該如何讓try塊中的代碼正常工作?Android登錄應用程序與PHP和MySQL不工作
public void onClick(View v)
{
httpclient = new DefaultHttpClient() ;
httppost=new HttpPost("Http://10.0.2.2/www/login.php");
//assign input text to string
String username=etUser.getText().toString();
String password=etPass.getText().toString();
try{
nameValuePairs.add(new BasicNameValuePair("username",username)) ;
nameValuePairs.add(new BasicNameValuePair("password",password)) ;
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response=httpclient.execute(httppost);
entity=response.getEntity();
if (entity!=null){
InputStream instream=entity.getContent();
JSONObject jsonResponse=new JSONObject(convertStreamToString(instream));
String retUser=jsonResponse.getString("user");
String retPass=jsonResponse.getString("pass");
if(username.equals(retUser)&&password.equals(retPass)){
SharedPreferences sp= getSharedPreferences("login details",0);
SharedPreferences.Editor spedit =sp.edit();
spedit.putString("user",username);
spedit.putString("pass",password);
spedit.commit();
Toast.makeText(getBaseContext(),"success !",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getBaseContext(),"Invalid Lodin details",Toast.LENGTH_SHORT).show();
}
}
}catch(Exception e){
e.printStackTrace();
Toast.makeText(getBaseContext(),"connection error",Toast.LENGTH_SHORT).show();
}
}
});
}
logcat告訴你什麼?自從你上次提出同樣的問題以來,你試過了什麼?請參閱http://stackoverflow.com/q/15485323/824914 – 2013-03-19 15:21:07
是您的apache服務器運行?你有iny MySQL錯誤在PHP? – 2013-03-19 15:22:24
首先編輯這個http://10.0.2.2/www/login.php到'http:// 10.0.2.2/www/login.php'並檢查瀏覽器是否正常工作。 – 2013-03-19 15:29:23