如何使用 Android Studio 和 Web Api 使用相同的请求 POST 和 GET

How to POST And GET with the same Request Using Android Studio And Web Api

我正在使用 Android Studio 开发 Android 应用程序 和后端使用 Asp.Net Web API.

网页Api侧面

[System.Web.Http.AcceptVerbs("GET", "POST")]
[System.Web.Http.HttpGet]
public dynamic GetTest(string name)
{
    List<person> persons = new List<person>()
    {
       new person { ID = 1, Name = "Loai" },
       new person { ID = 2, Name = "rami" },
       new person { ID = 3, Name = "Omar" },
       new person { ID = 4, Name = name}
     };
    IEnumerable<person> p = persons.ToList();
    return new { Students = p };
}

在此Link

http://qjtest.azurewebsites.net/api/test

如果您想设置 name 参数值,您可以使用

http://qjtest.azurewebsites.net/api/test?name=ayman

Android 工作室一侧

我用过这个代码

  final String URL = "http://qjtest.azurewebsites.net/api/test";
            HashMap<String, String> params = new HashMap<String, String>();
            params.put("name", "ayman");
            JsonObjectRequest req = new JsonObjectRequest(Request.Method.GET, URL, new JSONObject(params),
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            try {
                                JSONArray jsonArray = response.getJSONArray("Students");
                                for (int i = 0; i < jsonArray.length(); i++) {
                                    JSONObject student = jsonArray.getJSONObject(i);
                                    String ID = student.getString("ID");
                                    String Name = student.getString("Name");
                                    textview.append(ID + " - " + Name + "\n");
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.e("Error: ", error.getMessage());
                }
            });
            requestQueue.add(req);

当我 运行 应用程序并按下按钮时,这是 logcat

中显示的错误
11-17 15:08:48.035 9628-9654/com.sqlite_test.alhalabi.loai.test_gson E/Volley: [155]

BasicNetwork.performRequest: Unexpected response code 404 for http://qjtest.azurewebsites.net/api/test 11-17 15:08:48.041 9628-9628/com.sqlite_test.alhalabi.loai.test_gson E/Volley: 1 2.onErrorResponse: Error:

您可以参考以下示例:

        String url = "http://qjtest.azurewebsites.net/api/test?name=ayman";

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, null, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                Toast.makeText(mContext, "onResponse:\r\n" + response.toString(), Toast.LENGTH_SHORT).show();
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(mContext, "onErrorResponse:\r\n" + error.toString(), Toast.LENGTH_SHORT).show();
            }
        });

        requestQueue.add(jsonObjectRequest);