简体   繁体   中英

Using Android Volley with ASP .NET Web API

I am trying to retrieve a resource from my asp .net restful service from an android client. I have a MessagesController which has the method GetMessages:

[Authorize]
public IQueryable<Message> GetMessages()
{
    return db.Messages;
}

To access this I have sent a request to /token and have obtained a key which I use from the client application to access the resource. The trouble I have is that the client is receiving a 302 http error when trying to access the resource.

This is the method in the android client:

public void setAllSendersAndAllMessages()
{
    String url = "Messages/GetMessages";
    showpDialog();
    String Url = baseUrl + url;


    JsonArrayRequest req = new JsonArrayRequest(Url,
            new Response.Listener<JSONArray>() {

                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());

                    try {

                        allSenders = new String[response.length()];
                        allMessages = new String[response.length()];
                        for (int i = 0; i < response.length(); i++) {

                            JSONObject message = (JSONObject) response.get(i);

                            allSenders[i] = message.getString("sender");
                            allMessages[i] = message.getString("messageContent");
                        }
                        setInitialViews();

                    } catch (JSONException e) {
                        e.printStackTrace();
                        Toast.makeText(context, "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
                    }

                    hidepDialog();
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            Toast.makeText(context,error.getMessage(), Toast.LENGTH_SHORT).show();
            hidepDialog();
        }
    }){
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String>  headers = new HashMap<>();
            headers.put("Authorization", "bearer "+ token);
            return headers;
        }
    };

    mRequestQueue.add(req);

}

I am overriding the getHeaders() method to send the authorization token to the service. This key and header works perfectly when using 'Postman' but fails in the android client for some unknown reason. If anyone can offer some advice it would be greatly appreciated!

HTTP 302 is URL error, URL must be like : http://server/WepApi/api/orders

Server : Your IIS server Name , or Your server IP address

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM