- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在创建一个注册应用程序,我已经获得了客户端 ID 和 secret ,现在我必须执行获取请求并获取访问 token 和刷新 token 。我有这个链接https://supptop.mymix.net/api/token?grant_type=password&client_id=yourclientID&client_secret=yourclientSecret&email=email@gmail.com&password=password'
。
这是界面。
public interface SupportopApi {
@GET("/api/token")
Call<ResponseBody> getToken(@Query("grant_type") String grant_type,
@Query("client_id") String client_id,
@Query("client_secret") String client_secret,
@Query("email") String email,
@Query("password") String password);}
好的,现在是下一个类。
public class ApiClient {
private static ApiClient instance;
private SupportopApi supportopApi;
private ApiClient(String endpoint) {
OkHttpClient.Builder clientBuilder = new OkHttpClient.Builder()
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS);
clientBuilder.addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request();
request = request.newBuilder()
.addHeader("Content-Type", "application/json")
.build();
return chain.proceed(request);
}
});
supportopApi = new Retrofit.Builder()
.baseUrl(endpoint)
.client(clientBuilder.build())
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(SupportopApi.class);
}
public Call<ResponseBody> gettoken(String grant_type, String client_id, String client_secret,
String email, String password){
return supportopApi.getToken(grant_type, client_id, client_secret, email, password);}
好的,这是我打电话的类(class)。
public class FragmentRegistration extends Fragment {
View mainView;
EditText username, email, password, name;
Button button;
ApiClient pentairAPIClient = ApiClient.getInstance();
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mainView = inflater.inflate
(R.layout.registration, container, false);
username = mainView.findViewById(R.id.username);
email = mainView.findViewById(R.id.email);
password = mainView.findViewById(R.id.password);
name = mainView.findViewById(R.id.name);
button = mainView.findViewById(R.id.register);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String s = name.getText().toString();
String split[] = s.split(" ");
updateApp();
}
});
return mainView;
}
public void updateApp() {
FragmentRegistration context = this;
Call<ResponseBody> call = pentairAPIClient.registration(supportopObj);
call.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
activationCall();
} else {
Toast.makeText(getContext(), "Something went wrong",
Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getContext(), "Error...", Toast.LENGTH_SHORT).show();
}
});
}
public void activationCall() {
Call<ResponseBody> callActive = pentairAPIClient.activation(supportopObjActivate);
callActive.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if (response.isSuccessful()) {
try {
String data = response.body().string();
JSONObject obj = new JSONObject(data);
String client_id = obj.getString("client_id");
String client_secret = obj.getString("client_secret");
Call<ResponseBody> getToken = pentairAPIClient.gettoken("password",client_id, client_secret,
email.getText().toString(), password.getText().toString());
getToken.enqueue(new Callback<ResponseBody>() {
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
if(response.isSuccessful()){
Toast.makeText(getContext(), String.valueOf(response.body()), Toast.LENGTH_SHORT).show();
}
else Toast.makeText(getContext(), "Something wrong", Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getContext(), "You're on failure", Toast.LENGTH_SHORT).show();
}
});
} catch (JSONException | IOException e) {
e.printStackTrace();
}
} else
try {
Toast.makeText(getContext(), response.body().string(),
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onFailure(Call<ResponseBody> call, Throwable t) {
Toast.makeText(getContext(), "Error in activation",
Toast.LENGTH_SHORT).show();
}
});
}}
我不知道必须将链接粘贴到哪里,我将在哪里包含我的 client_id client_secret、邮件、密码。
最佳答案
尝试使用@Header
https://futurestud.io/tutorials/retrofit-2-manage-request-headers-in-okhttp-interceptor
我认为 client_id 和 client_secret 是 header , grant_type, email , password 是参数。
例如
public class LoginBody {
private String email;
private String password;
}
Call<String> call = service.request(AdminTokenUtils.getAuthorizationWeb(),body);
Call<String> request(
@Header("Authorization") String token,
@Body LoginBody body
);
关于java - 如何在改造中获取访问 token ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48446618/
我正在使用 Retrofit 和 RxJava,但似乎无法做我想做的事。 这是我对 Web 服务的声明: Observable rawRemoteDownload(@Header("Cookie")
我正在开发一个android应用程序,并使用改造发送请求和获取响应。像这样: public interface Service { @POST("/GetInfo") InfoResp
我刚刚从响应中获取代码,它说我的请求参数错误,那么我的 api 调用应该是什么样子? 这是来自文档的硬编码 API 调用 https://api.themoviedb.org/3/discover/m
实际上,当我使用具有 radio 频率的设备时,我的应用程序出现了问题。如果设备超出 radio 范围但尝试发送文件,我会收到 onFailure 消息,我对用户说没有网络,但主要问题是,一旦设备返回
我一直在关注其他答案,但缺少一个我找不到的步骤,这导致调用成功但数据未被正确解析,因为我进行的第一次调用返回了一个对象列表,但只返回了一个对象全部为空 我的模型.java public cla
对于我正在使用的服务器,我们有一个子域和一个目录,它们都绑定(bind)在一起。使用 Retrofit,您需要指定 baseURL,它似乎不允许目录。有什么方法可以实现吗? 例子: https://d
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 6 年前。 Improve t
我对 Android 还很陌生.. 我正在尝试使用 Retrofit 从 MySQL 检索数据.. 我在代码中没有发现任何错误,但是当我从设备运行应用程序时,它卡在“正在获取数据的进度对话框”上...
我正在使用 Dagger2 + Retrofit + RxAndroid + OkHttp3 + 新架构组件开发一个 Android 应用程序。 最小 sdk = 16。 问题:在 API 16 上运
我需要在插入硬编码查询后设置查询。 我的 API 地址是: myapiaddress/names?q=Yoni&gender=Man&(here i need to enter dynamic
我正在创建天气应用程序,让用户可以选择按任何城市搜索天气。当用户输入有效的城市名称时,我的应用程序工作正常,但当用户输入无效的字符串 ex: asndfs,bdfbsj 然后我的应用程序终止。 如何处
我有一个 Json 字符串。我无法使用带有两个列表(类别和产品)的 Retrofit onResponse。我怎么打电话,回调?我应该用什么?通常不是列表,对吧? { "Categories": [{
我正在实现一个两级嵌套的 recyclerView 并且两个回收器 View 都使用 retrofit 进行 API 调用。这是发出同步请求的方法: public void loadSectionSt
我正在使用 Qt 编写一个应用程序并且想要一个“Metro 风格”的界面。一切都完成了,除了我不知道如何让小部件出现和消失。例如,在 WPF 中,您可以为 (UIElement.RenderTrans
我在 java 应用程序中使用 Retrofit 来访问 api-rest。 我使用简单的代码: RestAdapter.Builder().setEndpoint(uri).setLogLevel(
我需要将下面报告的数据字符串转换为以时间戳(第一个数字元素)为键的字典。我该怎么做? element=20151201091000|22844.4|22786.2|22801.6|22839.7|10
我正在尝试使用 @QueryMap 发送多个参数(就像我通常做的那样)但是这次使用改造通过 POST。 改造 API @POST("/request.php") void sendRequest(@Q
我正在使用 Retrofit 进行后端通信:如果状态码不是 200 则回调调用失败方法。但是我想在失败方法中获取状态代码以进行进一步的代码调节 @Override pu
我正在使用自定义记录器记录到 Logcat 和文件(当文件启用时)。 这在接收来自测试人员的错误报告时非常有用(因为我在应用程序中还有一个按钮可以发送错误报告并附上日志)。 问题:我正在使用 Retr
很抱歉,如果我的标题含糊不清,但我找不到更好的。 我有一个以这种方式公开服务的休息 Api:/api/{type}/{id} 4 个“类型”,因此返回 4 个类类型。 所有这些类都继承自同一个父类(s
我是一名优秀的程序员,十分优秀!