发送邮件请求后无法从标头rsponse获取令牌

时间:2018-06-14 17:45:51

标签: java android rest openstack

我的帖子数据

 {
    "auth": {
        "identity": {
            "methods": [
                "password"
            ],
            "password": {
                "user": {
                    "id": "bbe57ffd805346f88930466fcf7cbe0d",
                    "password": "6790"
                }
            }
        },
        "scope": {
            "project": {
                "id": "76b6432f3ea649e3acdd9fd91643ef05"
            }
        }
    }
}

我的模型类是

Auth class

package com.example.server.restapi.api.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Auth {

    @SerializedName("identity")
    @Expose
    private Identity identity;
    @SerializedName("scope")
    @Expose
    private Scope scope;

    public Identity getIdentity() {
        return identity;
    }

    public void setIdentity(Identity identity) {
        this.identity = identity;
    }

    public Scope getScope() {
        return scope;
    }

    public void setScope(Scope scope) {
        this.scope = scope;
    }

}

示例模型类

package com.example.server.restapi.api.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Example {

    @SerializedName("auth")
    @Expose
    private Auth auth;

    public Auth getAuth() {
        return auth;
    }

    public void setAuth(Auth auth) {
        this.auth = auth;
    }

}

我的身份等级

package com.example.server.restapi.api.model;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Identity {

    @SerializedName("methods")
    @Expose
    private List<String> methods = null;
    @SerializedName("password")
    @Expose
    private Password password;

    public List<String> getMethods() {
        return methods;
    }

    public void setMethods(List<String> methods) {
        this.methods = methods;
    }

    public Password getPassword() {
        return password;
    }

    public void setPassword(Password password) {
        this.password = password;
    }

}

密码等级

package com.example.server.restapi.api.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Password {

    @SerializedName("user")
    @Expose
    private User user;

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

}

项目类

package com.example.server.restapi.api.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Project {

    @SerializedName("id")
    @Expose
    private String id;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

}

范围类

package com.example.server.restapi.api.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class Scope {

    @SerializedName("project")
    @Expose
    private Project project;

    public Project getProject() {
        return project;
    }

    public void setProject(Project project) {
        this.project = project;
    }

}

用户类

package com.example.server.restapi.api.model;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

public class User {

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("password")
    @Expose
    private String password;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

}

MainAcitivity类

package com.example.server.restapi;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

import com.example.server.restapi.api.model.Auth;
import com.example.server.restapi.api.model.Identity;
import com.example.server.restapi.api.model.Project;
import com.example.server.restapi.api.model.Scope;
import com.example.server.restapi.api.model.User;
import com.example.server.restapi.api.model.service.UserClient;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Headers;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
public static String token;
TextView textView;




    Retrofit.Builder builder = new Retrofit.Builder().baseUrl("http://192.168.0.106:5000/v3/auth/").
            addConverterFactory(GsonConverterFactory.create());

    Retrofit retrofit = builder.build();
    UserClient userClient = retrofit.create(UserClient.class);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView= findViewById(R.id.textView);
        List<String> methods = new ArrayList<String>();
        methods.add("password");
       Identity identity = new Identity();
       identity.setMethods(methods);
        User user = new User();
        user.setPassword("6790");
        user.setId("bbe57ffd805346f88930466fcf7cbe0d");
        Project project = new Project();
        project.setId("76b6432f3ea649e3acdd9fd91643ef05");

        login();




    }

    public void login()
    {

        Auth auth = new Auth();
       Call<ResponseBody> call= userClient.login(auth);


       call.enqueue(new Callback<ResponseBody>() {
           @Override
           public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

               Headers headers = response.headers();

               token = response.headers().get("x-subject-token");
               textView.setText(token);}

           @Override
           public void onFailure(Call<ResponseBody> call, Throwable t) {

               Toast.makeText(MainActivity.this, "error", Toast.LENGTH_SHORT).show();

           }
       });




    }
}

我正在使用postman测试openstack rest api并在Android中实现。 我想从响应头中仅提取x-subject-token而不想要响应正文数据。

0 个答案:

没有答案