我正在我的Android应用中建立登录名。我正在尝试使我的应用程序使用凌空与Node.JS服务器通信以发出网络请求。 node.js服务器已经安装并且正在运行。我确保我的PC和Android手机在同一网络上(我真的不知道这是否有必要)。此外,Mongo DB也已设置并正在运行。但是当我尝试登录时收到错误响应。这是我的代码:
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String getLoginEmail = loginEmail.getText().toString();
String getLoginPassword = loginPassword.getText().toString();
if (!TextUtils.isEmpty(getLoginEmail) && !TextUtils.isEmpty(getLoginPassword)) {
loginUser(getLoginEmail, getLoginPassword);
} else if...
}
});...
private void loginUser(final String gottenLoginEmail, final String gottenLoginPassword) {
final ProgressDialog progressDialog = new ProgressDialog(LoginActivity.this);
progressDialog.setMessage(getString(R.string.logging_in));
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.show();
StringRequest stringRequest = new StringRequest(Request.Method.POST, Constants.URL_LOGIN, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("onResponse", response);
progressDialog.dismiss();
try {
JSONObject jsonObject = new JSONObject(response);
Log.i("Response", response); //logged response
//check if response at first position (index 0) equals success
if (jsonObject.names().get(0).equals("success")) {
startActivity(new Intent(LoginActivity.this, MainActivity.class));
finish();
} else {
...
}
} catch (JSONException e) {
Log.i("Exception", e.getMessage()); //logged exception
Snackbar.make(findViewById(R.id.login_activity), e.getMessage(), Snackbar.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.i("Error", "ERRROR: " + error.getMessage()); //logged error. Refer to debugger image below
progressDialog.dismiss();
Snackbar.make(findViewById(R.id.login_activity), error.getMessage(), Snackbar.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("email", gottenLoginEmail);
params.put("password", gottenLoginPassword);
return params;
}
};
RequestHandler.getInstance(LoginActivity.this).addToRequestQueue(stringRequest);
}
在我的Constants Java类中,我有以下内容:
public class Constants {
public static final String ROOT_URL = "http://192.168.43.1:5000/api/user/auth/";
public static final String URL_LOGIN = ROOT_URL + "signin";
public static final String URL_REGISTER = ROOT_URL + "signup";
//192.168.43.1 is the DHCP server of the network my pc and android phone is connected to
//5000 is the node.js application port i.e. localhost:5000
}
请帮助我。我已经为此战斗了三天...
当我运行该应用程序时,我会在调试器控制台中得到它
API端点登录路径为“ localhost:5000 / api / user / auth / signin”。
答案 0 :(得分:0)
我认为您的麻烦是防火墙尝试禁用它 如果您的OC是Windows,请遵循本指南https://www.youtube.com/watch?v=dlBgoVMXIWo