Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android “错误”;应为BEGIN“U对象,但为字符串”;仅在远程服务器上发生_Android_Retrofit_Restful Authentication_Okhttp - Fatal编程技术网

Android “错误”;应为BEGIN“U对象,但为字符串”;仅在远程服务器上发生

Android “错误”;应为BEGIN“U对象,但为字符串”;仅在远程服务器上发生,android,retrofit,restful-authentication,okhttp,Android,Retrofit,Restful Authentication,Okhttp,当从我的RESTfulAPI(Rails)访问登录路径时,我遇到了这个错误,但只针对特定的地址 这里的交易,我有3个不同的地址,我可以用来测试我的Android应用程序。一个是我的本地机器,我在本地启动rest服务器进行测试和开发,作为构建变体,我选择指向本地机器的开发 登录路径运行良好。以下是我的路线示例(我正在使用改装): 在我的LoginActivity,我这样称呼它: User u = new User(); u.email = String.valueOf(edit_text_em

当从我的RESTfulAPI(Rails)访问登录路径时,我遇到了这个错误,但只针对特定的地址

这里的交易,我有3个不同的地址,我可以用来测试我的Android应用程序。一个是我的本地机器,我在本地启动rest服务器进行测试和开发,作为构建变体,我选择指向本地机器的开发

登录路径运行良好。以下是我的路线示例(我正在使用改装):

在我的LoginActivity,我这样称呼它:

 User u = new User();
 u.email = String.valueOf(edit_text_email.getText());
 u.password = String.valueOf(edit_text_password.getText());

 RestService service = RestManager.getService();
 JsonElement jsonUser = u.toJson(); //my user
 Call<User> call = service.login(jsonUser);
 call.enqueue(new Callback<User>() {
     @Override
     public void onResponse(Response<User> response, Retrofit retrofit) {
         if (response.isSuccess()) {
             User user = response.body();

             user.password = u.password;
             user.save();


         } else {
             //some treatment routines        
         }
     }

     @Override
     Public void onFailure(Throwable t) {

         Log.e(TAG, "doLoginRoutines Failure: " + t.getMessage().toString());


     }
     });

我终于找到了服务器上发生的事情。这个错误实际上发生在其他地方,但在我在这里报告的那个点被抛出

对于我的暂存和prod服务器,我们使用https,但我的本地服务器不是这样,因此我没有正确设置URL以包含暂存和生产服务器的安全HTTP,因此我的okHTTP没有正确找到rest路由


有趣的是,应该将错误记录为route not found(路径未找到),而不是我得到的这条。

改装日志会怎么说?你也到那里了吗
{“用户”:{“电子邮件”:myEmail@gmail.com“,“password”:“myPassword”}
?我希望您的User.class中有一些不正确的地方,同时双重确认您的本地服务器响应和暂存服务器响应是相同的。将setLogLevel(LogLevel.FULL)添加到改型.Buider()调用中,并检查请求和响应。也许你会发现一个问题。你能确认你的登台或生产服务器正在像你的本地主机一样返回一个用户对象吗?他们似乎没有返回用户对象,或者该对象的结构与本地主机用户对象不同。您可以使用自己的自定义GsonConvertyFactory,它扩展了converter.factory类。
public static RestService getService(){

    OkHttpClient okClient = new OkHttpClient();
    okClient.interceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            Response response = chain.proceed(chain.request());
            return response;
        }
    });

    Retrofit client = new Retrofit.Builder()
            .baseUrl(URL) //the URL comes from my BuildConfig
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    return client.create(RestService.class);
}

public static RestService getServiceWithHeaders(Context context){
    applicationContext = context;
    Response result = null;

    HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
    logging.setLevel(HttpLoggingInterceptor.Level.BODY);
    session = new UserSessionManager(applicationContext);
    final User u = session.getUser();

    OkHttpClient okClient = new OkHttpClient();
    okClient.interceptors().add(logging);
    okClient.interceptors().add(new Interceptor() {
        @Override
        public Response intercept(Chain chain) throws IOException {
            if (u.email != null) {
                Request original = chain.request();

                Request newRequest = original.newBuilder()
                        .header("X-User-Email", u.email)
                        .header("X-User-Token", u.authentication_token)
                        .method(original.method(), original.body())
                        .build();

                return chain.proceed(newRequest);
            } else {
                Response response = chain.proceed(chain.request());
                return response;
            }
        }
    });


    Retrofit client = new Retrofit.Builder()
            .baseUrl(URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(okClient)
            .build();
    return client.create(RestService.class);
}
 User u = new User();
 u.email = String.valueOf(edit_text_email.getText());
 u.password = String.valueOf(edit_text_password.getText());

 RestService service = RestManager.getService();
 JsonElement jsonUser = u.toJson(); //my user
 Call<User> call = service.login(jsonUser);
 call.enqueue(new Callback<User>() {
     @Override
     public void onResponse(Response<User> response, Retrofit retrofit) {
         if (response.isSuccess()) {
             User user = response.body();

             user.password = u.password;
             user.save();


         } else {
             //some treatment routines        
         }
     }

     @Override
     Public void onFailure(Throwable t) {

         Log.e(TAG, "doLoginRoutines Failure: " + t.getMessage().toString());


     }
     });
dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.squareup.retrofit:retrofit:2.0.0-beta2'
    compile 'com.squareup.retrofit:converter-gson:2.0.0-beta2'
    compile 'com.squareup.retrofit2:adapter-rxjava:2.0.1'
    compile 'com.squareup.okhttp:logging-interceptor:2.6.0'
    compile 'com.squareup.picasso:picasso:2.3.4'
    compile 'com.squareup.okhttp:okhttp:2.4.0'
    compile 'com.squareup.okhttp:okhttp-urlconnection:2.2.0'
    compile 'com.android.support:support-v4:20.0.0'
    compile 'com.android.support:appcompat-v7:20.0.+'
    compile 'com.michaelpardo:activeandroid:3.1.0-SNAPSHOT'
    compile 'io.reactivex:rxjava:1.1.2'
    compile 'io.reactivex:rxandroid:1.1.0'
    compile 'org.greenrobot:eventbus:3.0.0'
}