简体   繁体   中英

Consume REST API with Android

I have created a rest API using Spring boot it works perfectly i can add and retrieve data with JSON format:

[{
    "idProduct": 1,
    "description": null,
    "prix": 0.0
}, {
    "idProduct": 2,
    "description": "firstProduct",
    "prix": 52413.0
}, {
    "idProduct": 3,
    "description": "PRD3",
    "prix": 41413.0
}]

I am trying to consume it with an Android client, but when I execute it, it runs without exception, though doesn't display anything.

the android Activity :

package com.hassen.client_test;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private Button btnChercher;
    private TextView tvJson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btnChercher = (Button) findViewById(R.id.btnchercher);
        tvJson = (TextView) findViewById(R.id.tvJson);
        btnChercher.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View v) {
                   new JsonTask().execute("http://192.168.1.20:8080/products");
                   //   http://hmkcode.appspot.com/rest/controller/get.json
               }


           }
        );
    }


    public class JsonTask extends AsyncTask<String,String,String>{

        @Override
        protected String doInBackground(String... params) {
            HttpURLConnection connection=null;
            BufferedReader reader=null;

            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();

                connection.connect();

                InputStream stream = connection.getInputStream();

                reader = new BufferedReader(new InputStreamReader(stream));

                StringBuffer buffer = new StringBuffer();

                String line="";

                while ((line=reader.readLine())!=null){
                    buffer.append(line);
                }

                String finalJson = buffer.toString();

                JSONObject parentObject = new JSONObject(finalJson);
                JSONArray parentArray = parentObject.getJSONArray("parentObject");
                JSONObject finalObject = parentArray.getJSONObject(0);

                String description = finalObject.getString("description");

               // String titre = finalObject.getString("title");

                return description;





            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } finally {
                if(connection!=null){
                    connection.disconnect();
                }

                if(reader!=null){
                    try {
                        reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }


            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            tvJson.setText(result);
        }
    }
}

Try this

JSONArray arr = new JSONArray(json string);

for(int i = 0; i < arr.length(); i++){

    JSONObject c = arr.getJSONObject(i);        
    String product= c.getString("idProduct");
    String description= c.getString("description");
    String prix= c.getString("prix");
}

Now you can return which field you want.

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