[英]Autentication Usign Volley in Android
I work with KSOAP2 but i wish usign Volley and i can't connect a web service with Authentication.我使用KSOAP2,但我希望使用Volley并且我无法通过身份验证连接 Web 服务。 I have tried overwriting the method getHeader but in the log shows the following.我曾尝试覆盖方法 getHeader 但在日志中显示以下内容。
E/Volley: [593] BasicNetwork.performRequest: Unexpected response code 500 for http://myservice/register.asmx
this is my code:这是我的代码:
public void peticion() {
final RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("response", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VolleyError", error.getMessage());
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("UsuarioName", "user123");
headers.put("UsuarioPass", "123456789");
return headers;
}
};
queue.add(stringRequest);
}
EDIT Fragment the my WebService编辑片段我的 WebService
<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/MLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Header> <Autenticacion xmlns="http://tempuri.org/"> <suarioPass>string</UsuarioPass> <UsuarioName>string</UsuarioName> </Autenticacion> </soap12:Header>
with ksoap work but with volley no.使用 ksoap 工作,但没有截击。
-The data are correct - 数据正确
-The service work -服务工作
I dont understand why not work with volley我不明白为什么不使用截击
Thank you for your help感谢您的帮助
I think you can refer to the following code (I have tested with ASP.Net Web API), then use its logic for your app:我认为您可以参考以下代码(我已经使用 ASP.Net Web API 进行了测试),然后将其逻辑用于您的应用程序:
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i(LOG_TAG, response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(LOG_TAG, error.toString());
}
}) {
@Override
public byte[] getBody() throws AuthFailureError {
String paramsEncoding = "UTF-8";
Map<String, String> params = new HashMap<>();
params.put("grant_type", "password");
params.put("username", "user");
params.put("password", "pass");
StringBuilder encodedParams = new StringBuilder();
try {
for (Map.Entry<String, String> entry : params.entrySet()) {
encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
encodedParams.append('=');
encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
encodedParams.append('&');
}
return encodedParams.toString().getBytes(paramsEncoding);
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
}
}
@Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
String json;
if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
try {
json = new String(volleyError.networkResponse.data,
HttpHeaderParser.parseCharset(volleyError.networkResponse.headers));
} catch (UnsupportedEncodingException e) {
return new VolleyError(e.getMessage());
}
return new VolleyError(json);
}
return volleyError;
}
};
queue.add(stringRequest);
Volley 不支持协议SOAP
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.