Mario Zeffiro
Mario Zeffiro

Reputation: 1

Getting the JSON from google spreadsheets

Hi I'm trying to make an android application, for a school project, that creates an interface to some data contained in a google sheet. I'm having a problem when trying to retrieve its json string and through a buffer convert it in a String, problem is the standard way to do it doesn't work. Here is the code that I'm using to retrieve the data, the link to which I'm connecting, and the Log i get when I run it: https://script.googleusercontent.com/macros/echo?user_content_key=YUm908_F4USanZMXYLM5SSNopIaGpE9lsPLN4Gd76Ev-ps1HtglllhSfOon8LijwbaCtE5yErItehXcvQml5MHPUIoB-gnlVOJmA1Yb3SEsKFZqtv3DaNYcMrmhZHmUMWojr9NvTBuBLhyHCd5hHa1ZsYSbt7G4nMhEEDL32U4DxjO7V7yvmJPXJTBuCiTGh3rUPjpYM_V0PJJG7TIaKp6DlhKiyTgeD37GVXNPmWi9BLr90SAXNavw2PD7IFU7Gool08_D5VdRirKpWCIA4qy6ynGhL6kv9iPAz3iOzAmo&lib=MbpKbbfePtAVndrs259dhPT7ROjQYJ8yx:

public class JSONTask {
    private String txtJson=null;

    public String JSONTask(URL url){
        HttpURLConnection connection= null;
        BufferedReader reader = null;

        Log.v("JSONTAKS","READY TO GET JSON");
        Log.v("JSONTAKS URL",url.toString());

        try{
            connection=(HttpURLConnection) url.openConnection();
            connection.connect();
            Log.v("JSONTask","READY TO BUFFER");

            InputStream stream=connection.getInputStream();
            reader =new BufferedReader(new InputStreamReader(stream));

            StringBuffer buffer = new StringBuffer();
            String line="";
            Log.v("JSONTask",stream.toString());

            while((line = reader.readLine())!=null){
                buffer.append(line+"\n");
                Log.v("JSONTASK", buffer.toString());

            }
            Log.v("JSONTASK txt",buffer.toString());
            txtJson=buffer.toString();
            Log.v("JSONTASK txt",txtJson);

        }catch(Exception e){
            e.printStackTrace();
            Log.v("EXCEPTION", "EXCEPTION THROWN");
        }finally {
            if(connection!=null){
                connection.disconnect();
            }
        }try{
            if(reader!=null){
                reader.close();
            }
        }catch (Exception e){
            e.printStackTrace();
        }


        return txtJson;
    }

https://i.sstatic.net/p99eR.png

Upvotes: 0

Views: 107

Answers (1)

andras
andras

Reputation: 3655

It is highly recommended on android to use existing libraries. If there is no specific reason why you write your own methods for REST, you should check out http://square.github.io/okhttp/

The code you need to retrieve is on the site:

    OkHttpClient client = new OkHttpClient();

    String run(String url) throws IOException {
         Request request = new Request.Builder()
         .url(url)
         .build();

         Response response = client.newCall(request).execute();
         return response.body().string();
    }

Upvotes: 1

Related Questions