使用Android的REST API

时间:2016-05-17 13:38:39

标签: android rest android-asynctask

我使用Spring启动创建了一个rest API,它可以很好地工作,我可以使用JSON格式添加和检索数据:

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

我正在尝试使用Android客户端来使用它,但是当我执行它时,它会毫无例外地运行,但不会显示任何内容。

android活动:

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

1 个答案:

答案 0 :(得分:0)

试试这个

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

现在您可以返回所需的字段。