简体   繁体   中英

android application with REST API connection

I am working now on Android application for my own project , there is another person who working on API REST control panel , consider that i have no idea about REST and API and WEB SERVICES , I am learning now how to access the API and send data as JSON and GET data , but i am not sure what is happening clearly . for example i have login field in my application , and the user have to enter his/her information and the android app should send the data to control panel to check this info . the guy i am working with tell me to search for token to access the web services but i can not under stand the concept alone , I need to explain to me what should i do now and what should i learn to complete my application correctly . Best Regards

You should take look on this :

http://square.github.io/retrofit/

Retrofit is a REST Client for Android and Java.

Well you can use HTTP & SPDY client like or etc. or you can try doing free courses in Udacity. Which is Udacity Android Basics: Networking

1- You have to learn HTTP CONNECTION using any library i recommend Volley for you here the the link

2- You have to learn JsonObject and JsonArray here

3- this is an example for you :

   RequestQueue queue = Volley.newRequestQueue(this);// this = context
    final String url = yourURL
    // prepare the Request
    JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, url, null,
            new Response.Listener<JSONArray>()
            {


                @Override
                public void onResponse(JSONArray response) {
                    //Log.d(TAG, "Login Response: " + response.toString());
                    // displayToast(R.string.toast_email_success);
                    if (response != null) {
                        try {

                            JSONArray JA = response;
                            int [] id = new int[JA.length()];
                            String [] name = new String[JA.length()];
                            String [] premalink = new String[JA.length()];
                            String [] descreption = new String[JA.length()];
                            String [] price_html = new String[JA.length()];
                            String [] stock_status = new String[JA.length()];
                            int [] stock_quantity = new int[JA.length()];
                            String [] image_src = new String[JA.length()];


                            for (int i = 0; i < JA.length(); i++) {
                                JSONObject JO = (JSONObject) JA.get(i);
                                JSONArray JA_inside_image = new JSONArray(JO.getJSONArray("images"));
                                if(JA_inside_image!=null)
                                {
                                    JSONObject JO_inside = (JSONObject) JA_inside_image.get(0);
                                    image_src[i]=JO_inside.getString("src");


                                }
                                if(JO.get("id")!=null) {
                                    id[i] = JO.getInt("id");
                                }
                                else
                                {
                                    id[i]=0;
                                }
                                if(JO.get("name")!=null) {
                                    name[i]=JO.getString("name");
                                    if(name[i]=="Product")
                                        break;
                                }
                                else
                                {
                                    name[i]="Not Available Name";
                                }
                                if(JO.get("permalink")!=null) {
                                    premalink[i]=JO.getString("permalink");

                                }
                                else
                                {
                                    premalink[i]="Not Available Link";
                                }
                                if(JO.get("description")!=null) {
                                    descreption[i]=JO.getString("description");

                                }
                                else
                                {
                                    descreption[i]="Not Available Descreption";
                                }


                                if(JO.get("price_html")!=null) {
                                    price_html[i]=JO.getString("price_html");

                                }
                                else
                                {
                                    price_html[i]="Not Available Link";
                                }

                                if(JO.get("stock_status")!=null) {
                                    stock_status[i]=JO.getString("stock_status");

                                }
                                else
                                {
                                    stock_status[i]="Not Available Status";
                                }

                                if(JO.get("stock_quantity")!=null) {
                                    stock_quantity[i]=JO.getInt("stock_quantity");

                                }
                                else
                                {
                                    stock_quantity[i]=0;
                                }







                            }
                            loadingProgressBar.setVisibility(View.GONE);

                        } catch (JSONException e)

                        {
                            e.printStackTrace();
                            loadingProgressBar.setVisibility(View.GONE);

                        }

                        }

                    else {
                        loadingProgressBar.setVisibility(View.GONE);

                        Toast.makeText(getApplicationContext(), "Sorry Somesthing 

Wrong Happend try again later", Toast.LENGTH_LONG).show();
                        }
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        loadingProgressBar.setVisibility(View.GONE);

//                        Log.d("Error.Response", error.getLocalizedMessage());
                    }
                }
        );

You should read something about HTTPRequests. You can use POST request to send some data to the server or you may use GET requests to receive data from server. Also there are some useful libraries such as Retrofit and Volley that you can use to send GET, POST , .... request in a simple way.

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