[英]ANDROID - Can't connect database from localhost in Android Device
我的代碼在Android Emulator中工作。 我可以從本地主機連接到數據庫...
但是當我在Android設備上運行時。 該應用程序運行不正常。
您能提供從本地主機到Android設備的連接數據庫參考嗎? 我已經在Google中搜索過,但沒有得到。
public class Login extends Activity implements View.OnTouchListener {
EditText usernameInput, passwordInput;
Button loginButton;
String username, password;
ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
TextView createAccountLink;
private static String login_url = "http://10.0.2.2/ujian_online/login.php";
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
ArrayList<HashMap<String, String>> arraylist;
JSONArray products = null;
String image_link;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
usernameInput = (EditText) findViewById(R.id.username);
passwordInput = (EditText) findViewById(R.id.password);
loginButton = (Button) findViewById(R.id.login_button);
jsonParser = new JSONParser();
createAccountLink = (TextView) findViewById(R.id.createAccountLabel);
createAccountLink.setOnTouchListener(this);
loginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new CheckAccounts().execute();
}
});
//HANCURIN SESSION
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(v.getId() == R.id.createAccountLabel) {
Intent i = new Intent(getApplicationContext(), Registration.class);
startActivity(i);
finish();
}
return true;
}
public class CheckAccounts extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(Login.this);
pDialog.setMessage("System checks your account.....");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
username = usernameInput.getText().toString();
password = passwordInput.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));
JSONObject json = jsonParser.makeHttpRequest(login_url, "POST", params);
try {
int success = json.getInt("success");
Log.d("SUKSES", String.valueOf(success));
int id = 0;
if (success == 1) {
products = json.getJSONArray(TAG_PRODUCTS);
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
id = c.getInt("id");
}
new SessionManagement(getApplicationContext()).putInteger("ID", id);
Intent i = new Intent(getApplicationContext(), MainMenu.class);
startActivity(i);
finish();
} else {
pDialog.setMessage("Your username or password is wrong");
pDialog.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
}
}
}
您正在使用10.0.2.2,這是模擬器的特殊URL,僅指向計算機本地主機。 對於真實設備,請使用計算機的實際IP地址,並且位於同一網絡上。
您使用的url僅應在仿真器中使用。
這樣使用
private static String login_url = "http://real_ip_of_your_machine/ujian_online/login.php";
使用ipconfig(Windows)或ifconfig(Linux / Mac)獲取您的行進IP。 另外,您的機器和設備應位於同一網絡上。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.