简体   繁体   English

带有REST API连接的android应用程序

[英]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 . 我正在为我自己的项目开发Android应用程序,还有另一个人在使用API​​ REST控制面板,考虑到我不知道REST和API以及WEB服务,我现在正在学习如何访问API并发送数据作为JSON和GET数据,但我不确定发生了什么。 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 . 例如,我的应用程序中有登录字段,用户必须输入他/她的信息,Android应用程序应将数据发送到控制面板以检查此信息。 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/ http://square.github.io/retrofit/

Retrofit is a REST Client for Android and Java. Retrofit是Android和Java的REST客户端。

Well you can use HTTP & SPDY client like or etc. or you can try doing free courses in Udacity. 那么你可以使用像等的HTTP和SPDY客户端,或者你可以尝试在Udacity中免费课程。 Which is Udacity Android Basics: Networking 这是Udacity Android Basics:Networking

1- You have to learn HTTP CONNECTION using any library i recommend Volley for you here the the link 1-您必须使用任何库来学习HTTP连接,我建议Volley为您提供链接

2- You have to learn JsonObject and JsonArray here 2-你必须在这里学习JsonObject和JsonArray

3- this is an example for you : 3-这是一个例子:

   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. 您应该阅读有关HTTPRequests的内容。 You can use POST request to send some data to the server or you may use GET requests to receive data from server. 您可以使用POST请求将一些数据发送到服务器,也可以使用GET请求从服务器接收数据。 Also there are some useful libraries such as Retrofit and Volley that you can use to send GET, POST , .... request in a simple way. 还有一些有用的库,如Retrofit和Volley,您可以用它们以简单的方式发送GET,POST,....请求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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