2017-12-02 310 views
0

我正在使用retrofit构建android应用程序作为我的httprequest连接到我的hapi服务器。我的服务器工作正常,我已经在邮递员和我的WebApps上测试过了。但我的改造无法连接到它。下面是我的代码。为了简单起见阅读我删除线是没有必要的:改装无法连接到hapi服务器本地主机?

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

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

+0

如果你的服务器是在本地主机上,怎么办你从android设备到达它? –

回答

0

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

+0

'127.0.0.1是你的机器的localhost/loopback地址(在哪个服务器上托管)。 '号码127.0.0.1是Android设备本身的IP地址。每个设备都是它自己的本地主机。 – greenapps

+0

什么是常见的错误,我认为服务器应该是127.0.0.1,因为我在我的机器上试图从模拟器的android。我已经将它更改为我的机器IP,并且它工作得很好,谢谢你们。 – kola

+0

@greenapps,是的,每个设备都是它自己的本地主机。 127.0.0。1可以是本机或Android设备的IP地址。根据这个问题,我更倾向于使用127.0.0.1来引用服务器(托管它的机器)。 –

0

改造也没有任何问题。问题出在BASE_URL从你的android设备到你的服务器。

请检查服务器安装在您的计算机的本地IP地址(例如:192.168.1.103),然后更换与BASE_URL是如下面的例子

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