2

I'm developing an Android App Using Android Studio And Back-end Using Asp.Net Web API.

Web Api Side

[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 };
}

On This Link

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

if you want to set the name parameter value you may use

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

Android Studio Side

I've used this code

  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);

when i run the application and press the button this is the error shows in 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:

Loai
  • 732
  • 16
  • 32
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Nov 17 '15 at 11:56
  • 1
    do not use this bad tutorial from androidhive ... or at least read it to the end! ApplicationController.getInstance() returns null because you forgot to copy&paste something – Selvin Nov 17 '15 at 11:57
  • I have a slight issue understanding your question are you asking how to post at the same time as retrieving a response? or are you asking how to send POST and GET data in one call? if the latter then you can use key value pairs and appended the get parametres to the end of the url "http://example.com?ID="+ id +"&name="+name – TheHidden Nov 17 '15 at 11:58
  • http://stackoverflow.com/a/17572601/3593066 check this link it has different links which will help you – Mustanser Iqbal Nov 17 '15 at 12:06
  • @Selvin I Just Pasted the Wrong Code Block , i've update the question now – Loai Nov 17 '15 at 12:12
  • 1
    now please learn HTTP's basics .... HTTP 404 is .... well, HTTP 404 (there is nothing to exaplain, everyones know what 404 is) – Selvin Nov 17 '15 at 12:14
  • @Selvin i've saw this https://www.youtube.com/watch?v=FzwBYPzCIHk but trying to make the request with a params – Loai Nov 17 '15 at 12:14

1 Answers1

1

You can refer to the following sample:

        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);
BNK
  • 23,994
  • 8
  • 77
  • 87
  • that's simple one, thank you. how about i want to pass more that 1 parameter (each parameter will separated by comma right ?) – Loai Nov 17 '15 at 16:32
  • For url parameters: `String url = String.format("http://yoursite.com/api/test?param1=%1$s&param2=%2$s", value1, value2);` – BNK Nov 18 '15 at 01:23
  • @BNK Hi man can you help me in this question ? I am stuck at it.http://stackoverflow.com/questions/33717120/sqlite-get-all-data-and-pass-it-to-an-adapter?noredirect=1#comment55205556_33717120 – Moudiz Nov 18 '15 at 08:48
  • @Moudiz my android app often gets data from the server via web service (web api), – BNK Nov 18 '15 at 09:01
  • @BNK how do you handle caching then? – Moudiz Nov 18 '15 at 09:04
  • @Moudiz cashing or caching? – BNK Nov 18 '15 at 09:06
  • I eidt my comment , caching I mean @BNK – Moudiz Nov 18 '15 at 09:07
  • @Moudiz: if the server supports output-caching (cache-control with max-age in response header, for example), volley caches for you already. If the server does not support, you can refer my answer at http://stackoverflow.com/questions/31897189/android-setup-volley-to-use-from-cache/32022946#32022946 and http://stackoverflow.com/questions/33376564/android-cache-mechanism-return-multi-result – BNK Nov 18 '15 at 09:08
  • @BNK I am using picasso , and yes the image are cached , but a friend told me I should executed the url again to get the cached image. but if the connection exists I cannot get the url to get them from cached – Moudiz Nov 18 '15 at 09:12
  • @Moudiz: I have not tried caching with picasso, you can read http://stackoverflow.com/questions/23978828/how-do-i-use-disk-caching-in-picasso http://stackoverflow.com/questions/19995007/local-image-caching-solution-for-android-square-picasso-vs-universal-image-load to see if can help or not – BNK Nov 18 '15 at 09:22
  • @Moudiz if possible, can you upload your full project about Sqlite (to github/google drive...) so that I can download and check? – BNK Nov 18 '15 at 11:12
  • @BNK I will upload after some hours – Moudiz Nov 18 '15 at 11:19