该应用程序适用于模拟器,但不适用于实际设备

时间:2017-11-27 12:35:58

标签: android

我只是想在我的Android应用程序帮助实际场景就像,我已经制作了一个Android应用程序,存储和从mysql数据库获取数据,所以扭曲是每当我在Android模拟器上运行应用程序它运行良好,但我一旦运行尝试在实际设备上运行它似乎没有发生任何事情然而登录和注册按钮似乎什么都不做他们不打电话给api,我使用wamp作为本地服务器,我的设备和笔记本电脑是相同的wifi网络路由器,所以我没有得到它,顺便说一下logcat没有显示任何错误,它也在模拟器上运行完美 我的注册活动是将用户添加到数据库中

public class Register extends AppCompatActivity {

private static final String TAG = Register.class.getSimpleName();
ProgressDialog progressDialog;
private EditText signupInputName, signupInputEmail, signupInputPassword;
public Button btnSignUp;
public Button btnLinkLogin;
private SQLiteHandler db;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    progressDialog = new ProgressDialog(this);
    progressDialog.setCancelable(false);
    SessionManager  session = new SessionManager(getApplicationContext());
    db = new SQLiteHandler(getApplicationContext());

    // Check if user is already logged in or not
    if (session.isLoggedIn()) {
        // User is already logged in. Take him to main activity
        Intent intent = new Intent(Register.this,
                Matchboard.class);
        startActivity(intent);
        finish();
    }

    signupInputName = (EditText) findViewById(R.id.name);
    signupInputEmail = (EditText) findViewById(R.id.email);
    signupInputPassword = (EditText) findViewById(R.id.password);
    btnSignUp = (Button) findViewById(R.id.btnRegister);
    btnLinkLogin = (Button) findViewById(R.id.btnLinkToLoginScreen);


    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String name = signupInputName.getText().toString().trim();
            String email = signupInputEmail.getText().toString().trim();
            String password = signupInputPassword.getText().toString().trim();

            if (!name.isEmpty() && !email.isEmpty() && !password.isEmpty()) {
                registerUser(name, email, password);
            } else {
                Toast.makeText(getApplicationContext(),
                        "Please enter your details!", Toast.LENGTH_SHORT).show();
            }
        }
    });
    btnLinkLogin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent i = new Intent(getApplicationContext(),Login.class);
            startActivity(i);
            finish();
        }
    });
}




private void registerUser(final String name,  final String email, final String password) {
    // Tag used to cancel the request
    String tag_string_req = "req_register";

    progressDialog.setMessage("Adding you ...");
    showDialog();

    StringRequest strReq = new StringRequest(Request.Method.POST,
            AppConfig.URL_REGISTER, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            Log.d(TAG, "Register Response: " + response.toString());
            hideDialog();

            try {
                JSONObject jObj = new JSONObject(response);
                boolean error = jObj.getBoolean("error");

                if (!error) {

                    String uid = jObj.getString("uid");
                    JSONObject user = jObj.getJSONObject("user");
                    String name = user.getString("name");
                    String email = user.getString("email");
                    String created_at = user
                            .getString("created_at");

                    // Inserting row in users table
                    db.addUser(name, email, uid, created_at);

                     Toast.makeText(getApplicationContext(), "Hi "+name+",You are successfully Added!", Toast.LENGTH_SHORT).show();

                    // Launch login activity
                    Intent intent = new Intent(
                            Register.this,
                            Login.class);
                    startActivity(intent);
                    finish();

                } else {

                    String errorMsg = jObj.getString("error_msg");
                    Toast.makeText(getApplicationContext(),
                            errorMsg, Toast.LENGTH_SHORT).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e(TAG, "Registration Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(),
                    error.getMessage(), Toast.LENGTH_SHORT).show();
            hideDialog();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            // Posting params to register url
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", name);
            params.put("email", email);
            params.put("password", password);

            return params;
        }
    };
    // Adding request to request queue
    AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

private void showDialog() {
    if (!progressDialog.isShowing())
        progressDialog.show();
}

private void hideDialog() {
    if (progressDialog.isShowing())
        progressDialog.dismiss();
}

}

1 个答案:

答案 0 :(得分:0)

我相信当您在本地计算机上调用本地主机时,但当应用程序在您的实际设备上时,可能存在一些IP问题。您可能错误地访问它。 问题是您的实际设备和系统应该在同一个网络上。 使用ipconfig命令检查IP,然后在同一网络上重试。