我是Android Volley请求的新手。我已设法发布帖子并获取请求到服务器并获得响应。
public void getCardDetails() throws UnsupportedEncodingException, NoSuchAlgorithmException, InvalidKeyException {
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// response
Log.d("Response from server","Customer Account"+response);
try {
results = signInActivity.splitQuery(response);
String encodedMessage = results.get("encodedMessage");
String serverSignature = results.get("signature");
//Log.d("CustomerAccount","encodedMessage:"+ encodedMessage);
String decodeMessage = security.decodeBase64(encodedMessage);
Log.d("CustomerAccount","DecodedMessage"+ decodeMessage);
customerDetailsXML = decodeMessage;
try {
String computedSignature = security.hashMac(decodeMessage, signature_key);
int responseCode = xmlHandler.getResponseCode(decodeMessage);
Log.d("CustomerAccount","Response Code: "+responseCode);
if (responseCode == 1001){
getCardBalance();
Log.d("CustomerAccount","Success");
}
} catch (InvalidKeyException e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", error.toString());
}
}
) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-type", "application/x-www-form-urlencoded;charset=\"utf-8\"");
headers.put("Accept", "application/x-www-form-urlencoded");
headers.put("Cache-Control", "no-cache");
headers.put("Pragma", "no-cache");
headers.put("User-Agent", "Mozilla/5.0");
headers.put("Content-length", dataStream.toString());
return headers;
}
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("encodedMessage", encodedMessage_req);
params.put("signature", signature);
return params;
}
};
Volley.newRequestQueue(this).add(postRequest);
}
上述请求以String XML格式获取服务器响应。我希望能够使用我执行请求的方法来返回XML,这样我就可以根据请求方法操作它。
有关如何实现这一目标的任何指示?
答案 0 :(得分:0)
做这样的事情:
创建一个接口类:
public interface MyListener{
public void myMethod(String response);
}
更改您的方法输入:public void getCardDetails(MyListener listener)
在public void onResponse(String response)
添加:
`listener.myMethod(response);`