我使用帶回調接口的抽象。我將發送請求發送到服務器和服務器響應302狀態碼。如何將響應頭中的位置設置爲變量字符串類型?然後,我想用全屏webview與此位置開始新的活動。響應302位置變爲抽象
這段代碼凌空類發送請求:
public StringPostRequest(Context context, String url, Map<String, String> params) {
this.params = params;
this.url = url;
mQueue = Volley.newRequestQueue(context, new NukeSSLCerts());
}
public void getString(final VolleyCallback callback) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, url ,
new Response.Listener<String>(){
@Override
public void onResponse(String response) {
req = response;
callback.onSuccess(req);
}
}
,new Response.ErrorListener(){
@Override
public void onErrorResponse(final VolleyError error){
final int status = error.networkResponse.statusCode;
// Handle 30x
if(HttpURLConnection.HTTP_MOVED_PERM == status || status == HttpURLConnection.HTTP_MOVED_TEMP || status == HttpURLConnection.HTTP_SEE_OTHER) {
final String location = error.networkResponse.headers.get("Location");
callback.onSuccess(location);
}
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return params;
}
};
mQueue.add(stringRequest);
}
public interface VolleyCallback{
void onSuccess(String req);
}
在主要活動中我創建地圖的POST請求。
Map<String,String> params = new HashMap<>();
params.put("foo", "bar");
params.put("foo1", "bar1");
params.put("foo2", "bar2");
StringPostRequest getRedirectUrl = new StringPostRequest(this,url,params);
getRedirectUrl .getString(new StringPostRequest.VolleyCallback() {
@Override
public void onSuccess(String req) {
Log.d("reqInMainact", req);
RedirectUrl[0] = req;
}
});
return RedirectUrl[0];
但在響應頭位置寫不出來的redirectUrl [0](( 請幫助我
這將是偉大的,如果你可以提供你的代碼 –
好的)更新的問題 –
你究竟返回'RedirectUrl [0];'到?你不能從回調中返回。您在**回調 –