Android 如何使截击呼叫同步
在我的应用程序中,我使用截击进行联网。现在,后端团队在web服务调用中做了一些更改,这是在每次api调用之前,我需要再调用一个serviceoauth服务,它将在JSON响应中提供访问令牌。然后,在我的实际servicelogin服务调用中将此访问令牌用作url中的查询。意味着我需要打两个电话,一个接一个 在我的代码中实现了此更改,例如登录服务: 步骤1调用提供访问令牌的oauth服务。 步骤2:在url中使用此访问令牌作为登录服务的查询 现在的问题是调用不同步,我在登录调用后收到访问令牌作为响应,因此得到一个错误 登录服务呼叫: OAuth服务呼叫:Android 如何使截击呼叫同步,android,web-services,oauth,android-volley,Android,Web Services,Oauth,Android Volley,在我的应用程序中,我使用截击进行联网。现在,后端团队在web服务调用中做了一些更改,这是在每次api调用之前,我需要再调用一个serviceoauth服务,它将在JSON响应中提供访问令牌。然后,在我的实际servicelogin服务调用中将此访问令牌用作url中的查询。意味着我需要打两个电话,一个接一个 在我的代码中实现了此更改,例如登录服务: 步骤1调用提供访问令牌的oauth服务。 步骤2:在url中使用此访问令牌作为登录服务的查询 现在的问题是调用不同步,我在登录调用后收到访问令牌作为响
}首先在登录按钮上调用OAuthsevice,然后单击响应方法调用登录Api OAuth服务呼叫: 只需像这样更改类构造函数 AuthAuthAuthentication类的In-OnResponse方法 你想怎么用就怎么用。假设您必须在登录中使用单击
这很简单。。。只需调用第一个请求的第二个截击onResponse方法,您是否可以共享一些登录coll oAuth方法的codeonclick。。。oAuth方法onresponse调用login方法还有其他方法吗?因为我有很多api调用,并且我不能在GetAuthToken中编写太多的if语句。为什么要取消我的答案。。?
public void onClickLogin(View v) {
// Tag used to cancel the request
String tagJSONobj = "json_obj_req";
String url;
if(Constants.RUN_AUTH_API) {
authAuthentication = new AuthAuthentication(tinyDB, SignInActivity.this);
authAuthentication.getAuthToken();
url = https://abc.xyz.com/Services/api/UserValidation/userValidate.do?access_token= + tinyDB.getString(Constants.MY_SHARED_PREF_AUTH_TOKEN);
}else
{
url = Constants.SIGNIN_URL;
}
showDialog();
JSONObject object = new JSONObject();
try {
object.put("userName", name);
object.put("password", password);
object.put("appType", "MOB APP");
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST, url, object,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
hidePDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
System.out.print("error is" + error.getMessage());
error.printStackTrace();
Toast.makeText(SignInActivity.this, getResources().getString(R.string.login_service_error_message), Toast.LENGTH_SHORT).show();
}
}) ;
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tagJSONobj);
}
}
public class AuthAuthentication {
private static final String TAG = AuthAuthentication.class.getSimpleName();
private TinyDB tinyDB;
private Context context;
public AuthAuthentication(TinyDB tinyDB, Context context){
this.tinyDB = tinyDB;
this.context = context;
}
public void getAuthToken() {
String tag_json_obj = "json_obj_req";
String url = https://abc.xyz.com/Services/oauth/token?grant_type=password&client_id=restapp&client_secret=restapp&username=admin&password=admin";
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
url, "",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d(TAG, " Response" + response.toString());
tinyDB.putString(Constants.MY_SHARED_PREF_AUTH_TOKEN, "" + response.getString(AppTags.TAG_AUTH_TOKEN));
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(context, context.getResources().getString(R.string.unable_to_process), Toast.LENGTH_SHORT).show();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}
public class AuthAuthentication {
private static final String TAG = AuthAuthentication.class.getSimpleName();
private TinyDB tinyDB;
private Context context;
public AuthAuthentication(TinyDB tinyDB, Context context){
this.tinyDB = tinyDB;
this.context = context;
}
public void getAuthToken() {
String tag_json_obj = "json_obj_req";
String url = https://abc.xyz.com/Services/oauth/token?grant_type=password&client_id=restapp&client_secret=restapp&username=admin&password=admin";
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
url, "",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.d(TAG, " Response" + response.toString());
tinyDB.putString(Constants.MY_SHARED_PREF_AUTH_TOKEN, "" + response.getString(AppTags.TAG_AUTH_TOKEN));
onClickLogin();/// here to peform login
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(context, context.getResources().getString(R.string.unable_to_process), Toast.LENGTH_SHORT).show();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}
public interface VolleyResponse {
void processFinish(String output);
}
public class AuthAuthentication {
private static final String TAG = AuthAuthentication.class.getSimpleName();
private TinyDB tinyDB;
private Context context;
private VolleyResponse delegate;
public AuthAuthentication(TinyDB tinyDB, Context context,VolleyResponse delegate){
this.tinyDB = tinyDB;
this.context = context;
this.delegate= delegate;
}
--------
-------
}
@Override
public void onResponse(JSONObject response) {
try {
Log.d(TAG, " Response" + response.toString());
tinyDB.putString(Constants.MY_SHARED_PREF_AUTH_TOKEN, "" + response.getString(AppTags.TAG_AUTH_TOKEN));
//send response of volley
delegate.processFinish(tinyDB); //it will broadcast your response
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
login.setOnClickListerner(new View.OnClickListenr(){
@Override
public void onClick(View view){
AuthAuthentication auth= new AuthAuthentication(tinyDB,mContext,new VolleyResponse() {
@Override
public void processFinish(String output) {
//output conatins response
loginApicall();
}
}.getAuthToken(); ///if not work then auth.getAuthToken
}
});