这是我关于stackoverflow的第一个问题,很抱歉我问了一些显而易见的事情,但我真的卡住了,因为我只是Android中的初学者和Delphi / Visual Studio,它们完全不同:)
这就是我希望使用Web服务器的REST API获取用户的信息。顺序应如下: - 使用GET获取令牌 - 根据先前的请求和登录/密码授权用户使用令牌
我正在尝试将代码放在按钮的OnListener中,如下所示:
公共类logVF扩展了AppCompatActivity {
private Button sign_in_but;
private RequestQueue requestQueue;
private static final String URL_token = https://www.vereinsflieger.de/interface/rest/auth/accesstoken";
private static final String URL_signin = "https://www.vereinsflieger.de/interface/rest/auth/signin";
private StringRequest request_tk;
private StringRequest request_user;
private JSONObject jsonObject;
private JSONObject jsonObject_sg;
private String accToken;
private boolean vf_con = false;
private boolean sg_in = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.log_vf_form);
sign_in_but = (Button) findViewById(R.id.sign_in_button);
requestQueue = Volley.newRequestQueue(this);
sign_in_but.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
request_tk = new StringRequest(Request.Method.GET, URL_token, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
jsonObject = new JSONObject(response);
//jsonObject.
if ((int) jsonObject.get("httpstatuscode") == 200) {
vf_con = true;
Toast.makeText(getApplicationContext(), "vereinsflieger.de contacted", Toast.LENGTH_SHORT).show();
accToken = (String) jsonObject.get("accesstoken");
} else {
Toast.makeText(getApplicationContext(), "vereinsflieger.de CANNOT be contacted", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "FAIL! Check connection!", Toast.LENGTH_SHORT).show();
}
});
request_user = new StringRequest(Request.Method.POST, URL_signin, new Response.Listener<String>() {
@Override
public void onResponse(String response_sg) {
try {
jsonObject_sg = new JSONObject(response_sg);
if ((int) jsonObject_sg.get("httpstatuscode") ==200) {
Toast.makeText(getApplicationContext(), "Signed in to vereinsflieger.de", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "FAIL! Check login data!", Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<>();
// the POST parameters:
params.put("accesstoken", accToken);
params.put("username", "user");
params.put("password", convertPassMd5("passw"));
return params;
}
};
requestQueue.add(request_tk);
requestQueue.add(request_user);
}
});
}
public static String convertPassMd5(String pass) {
String password = null;
MessageDigest mdEnc;
try {
mdEnc = MessageDigest.getInstance("MD5");
mdEnc.update(pass.getBytes(), 0, pass.length());
pass = new BigInteger(1, mdEnc.digest()).toString(16);
while (pass.length() < 32) {
pass = "0" + pass;
}
password = pass;
} catch (NoSuchAlgorithmException e1) {
e1.printStackTrace();
}
return password;
}
}
所以,基本上我试图一个接一个地执行两个请求
requestQueue.add(request_tk);
requestQueue.add(request_user);
但由于异步执行,第一次按钮点击期间第二次请求永远不会从第一次获取数据。
如何让它们同步或等到第一次查询完成?..
Lame问题,但非常感谢回答:)