改造无法连接到hapi服务器localhost?

时间:2017-12-02 16:31:36

标签: java android retrofit2 hapijs

我使用改造作为我的httprequest构建Android应用程序以连接到我的hapi服务器。我的服务器工作正常,我已经在postman和我的WebApps上测试了它。但我的改造无法连接到它。下面是我的代码。为了简单起见,我删除了不必要的行:

LoginActivity.java

package com.sianghee.reviu;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.design.widget.TextInputEditText;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import butterknife.ButterKnife;
import butterknife.BindView;
import butterknife.OnClick;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

import com.sianghee.reviu.interfaces.APIService;
import com.sianghee.reviu.lib.Session;
import com.sianghee.reviu.models.UserLogin;
import static com.sianghee.reviu.lib.Helper.returnError;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

public class LoginActivity extends Activity {
    private static final String TAG = "LoginActivity";
    private static final int REQUEST_SIGNUP = 0;
    Session session;
    public static final String BASE_URL = "localhost:3000";

    private EditText emailText;
    private EditText passwordText;

     @BindView(R.id.btn_login) Button _loginButton;
    @BindView(R.id.link_signup) TextView _signupLink;

    // TODO: Init sewaktu pertama kali form diload
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        emailText = (EditText)findViewById(R.id.input_email);
        passwordText = (EditText)findViewById(R.id.input_password);

    }

    // TODO: bind login and do login
    @OnClick(R.id.btn_login)
    public void submitLogin() {
        login();
    }

    // TODO: bind signup and do register
    ....

    public void login() {
        Log.d(TAG, "Login");

        if (!validate()) {
            onLoginFailed();
            return;
        }

        _loginButton.setEnabled(false);

        final ProgressDialog progressDialog = new ProgressDialog(this);
        progressDialog.setIndeterminate(true);
        progressDialog.setMessage("Authenticating...");
        progressDialog.show();

        String email = emailText.getText().toString();
        String password = passwordText.getText().toString();

        // TODO: Implement your own authentication logic here.
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        APIService api = retrofit.create(APIService.class);
        UserLogin userLogin = new UserLogin(email, password);

        Call<UserLogin> call = api.login(userLogin);
        call.enqueue(new Callback<UserLogin>() {
            @Override
            public void onResponse(Call<UserLogin> call, Response<UserLogin> response) {
                String result;
                progressDialog.dismiss();
                try {
                    result = response.body().toString();

                    if (returnError(result).isEmpty()) {

                        JSONObject obj;
                        obj = new JSONObject(result);
                        session.setLoginSession(obj.getString("scope"), obj.getString("token"));

                        Intent i = new Intent(LoginActivity.this, MainActivity.class);
                        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(i);
                        finish();

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            .......
}

APIService.java

package com.sianghee.reviu.interfaces;

import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.POST;

import com.sianghee.reviu.models.UserLogin;

/**
 * Created by andy on 11/26/17.
 */

public interface APIService {
    @POST("auth")
    Call<UserLogin> login(@Body UserLogin auth);
}

UserLogin.java

package com.sianghee.reviu.models;

public class UserLogin {
    String email;
    String password;

    public UserLogin(String email, String password) {
        this.email = email;
        this.password = password;
    }
}

每当我点击登录按钮时,它都无法连接到服务器。下面是logcat:

12-02 23:24:42.771 3660-3660/com.sianghee.reviu I/InstantRun: starting instant run server: is main process
12-02 23:24:43.116 3660-3690/com.sianghee.reviu D/OpenGLRenderer: HWUI GL Pipeline
12-02 23:24:43.213 3660-3660/com.sianghee.reviu I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.
12-02 23:24:43.219 3660-3660/com.sianghee.reviu I/TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead.

                                                                   [ 12-02 23:24:43.283  3660: 3690 D/         ]
                                                                   HostConnection::get() New Host Connection established 0x896438c0, tid 3690
12-02 23:24:43.471 3660-3690/com.sianghee.reviu I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
12-02 23:24:43.471 3660-3690/com.sianghee.reviu I/OpenGLRenderer: Initialized EGL, version 1.4
12-02 23:24:43.471 3660-3690/com.sianghee.reviu D/OpenGLRenderer: Swap behavior 1
12-02 23:24:43.471 3660-3690/com.sianghee.reviu W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
12-02 23:24:43.471 3660-3690/com.sianghee.reviu D/OpenGLRenderer: Swap behavior 0
12-02 23:24:43.473 3660-3690/com.sianghee.reviu D/EGL_emulation: eglCreateContext: 0x9b543860: maj 2 min 0 rcv 2
12-02 23:24:43.475 3660-3690/com.sianghee.reviu D/EGL_emulation: eglMakeCurrent: 0x9b543860: ver 2 0 (tinfo 0xa4a8f700)
12-02 23:24:43.527 3660-3690/com.sianghee.reviu D/EGL_emulation: eglMakeCurrent: 0x9b543860: ver 2 0 (tinfo 0xa4a8f700)
12-02 23:24:43.856 3660-3660/com.sianghee.reviu I/AssistStructure: Flattened final assist data: 1684 bytes, containing 1 windows, 5 views
12-02 23:24:49.113 3660-3673/com.sianghee.reviu I/zygote: Do partial code cache collection, code=30KB, data=26KB
12-02 23:24:49.113 3660-3673/com.sianghee.reviu I/zygote: After code cache collection, code=30KB, data=26KB
12-02 23:24:49.113 3660-3673/com.sianghee.reviu I/zygote: Increasing code cache capacity to 128KB
12-02 23:24:51.355 3660-3673/com.sianghee.reviu I/zygote: Do partial code cache collection, code=61KB, data=59KB
12-02 23:24:51.356 3660-3673/com.sianghee.reviu I/zygote: After code cache collection, code=61KB, data=59KB
12-02 23:24:51.356 3660-3673/com.sianghee.reviu I/zygote: Increasing code cache capacity to 256KB
12-02 23:24:54.016 3660-3673/com.sianghee.reviu I/zygote: Do full code cache collection, code=123KB, data=99KB
12-02 23:24:54.016 3660-3673/com.sianghee.reviu I/zygote: After code cache collection, code=107KB, data=66KB
12-02 23:24:56.503 3660-3660/com.sianghee.reviu D/LoginActivity: Login
12-02 23:24:56.573 3660-3673/com.sianghee.reviu I/zygote: Do partial code cache collection, code=114KB, data=93KB
12-02 23:24:56.573 3660-3673/com.sianghee.reviu I/zygote: After code cache collection, code=114KB, data=93KB
12-02 23:24:56.573 3660-3673/com.sianghee.reviu I/zygote: Increasing code cache capacity to 512KB
12-02 23:24:56.574 3660-3673/com.sianghee.reviu I/zygote: JIT allocated 56KB for compiled code of void android.view.View.<init>(android.content.Context, android.util.AttributeSet, int, int)
12-02 23:24:56.603 3660-3660/com.sianghee.reviu D/NetworkSecurityConfig: No Network Security Config specified, using platform default
12-02 23:24:56.803 3660-3660/com.sianghee.reviu W/error: java.net.ConnectException: Failed to connect to /127.0.0.1:3000
12-02 23:24:57.094 3660-3690/com.sianghee.reviu D/EGL_emulation: eglMakeCurrent: 0x9b543860: ver 2 0 (tinfo 0xa4a8f700)
12-02 23:24:57.126 3660-3690/com.sianghee.reviu D/EGL_emulation: eglMakeCurrent: 0x9b543860: ver 2 0 (tinfo 0xa4a8f700)

我的登录服务器API网址是: http://127.0.0.1:3000/auth

在此之前注意到Log.w()方法的错误:

  

java.net.ConnectException:无法连接到/127.0.0.1:3000

也许这只是我的常见错误,请帮助。

3 个答案:

答案 0 :(得分:2)

127.0.0.1是您的计算机的本地主机/环回地址a(托管服务器的地址)。 127.0.0.1映射到计算机的IP地址,因此http://127.0.0.1:3000/auth只能从您的计算机访问。

现在,为了从任何其他计算机(例如您的手机)访问http://127.0.0.1:3000/auth,您的计算机(服务器)和设备(电话)应该在同一网络上(例如,相同的Wi-Fi)您需要通过http://IPAddressOfYourMachine:3000/auth访问它。

在MacOs / Linux上,您可以在终端上使用ifconfig命令查找IP地址。在Windows计算机上,命令为ipconfig。 IP地址的格式应为192.168.x.y。

所以最终的BASE_URL应该是http://192.168.x.y:3000

答案 1 :(得分:1)

确保您的手机(仿真器)和计算机位于同一网络中 并以ip地址http://192.168.8.106:3030为例,而不是http://localhost:3030

答案 2 :(得分:0)

改造没有任何问题。问题出在BASE_URL中,用于从Android设备到服务器。

请检查计算机安装服务器的本地IP地址(例如:192.168.1.103),然后用以下示例替换BASE_URL

public static final String BASE_URL = "http://192.168.1.103:3000";