0
 public class ODKortrijkWebservice extends AsyncTask < ODKortrijkInterface, Void, String > {
      private ODKortrijkInterface listener;@
      Override
      protected String doInBackground(ODKortrijkInterface...arg0) {
          listener = arg0[0];
          InputStream is = null;
          int len = 10000;
          StringBuilder winkelBuilder = new StringBuilder();

          // execute search
          try {

              URL url = new URL("http://data.kortrijk.be/middenstand/winkels_markten");
              try {
                  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                  urlConnection.setReadTimeout(10000 /* milliseconds */ );
                  urlConnection.setConnectTimeout(15000 /* milliseconds */ );
                  urlConnection.setRequestMethod("GET");
                  urlConnection.setDoInput(true);
                  urlConnection.connect();
                  int response = urlConnection.getResponseCode();
                  Log.d("Inputstream", "The response is: " + response);
                  is = urlConnection.getInputStream();
                  // Todo: add every line to my winkelBuilder??
              } finally {
                  if (is != null) {
                      is.close();
                  }
              }

          } catch (Exception e) {
              e.printStackTrace();
          }
          return winkelBuilder.toString();
      }


      protected void onPostExecute(String result) {
          ArrayList < Winkel > winkels = processResult(result);
          listener.updateScreen(winkels);
      }

      private ArrayList < Winkel > processResult(String result) {
          ArrayList < Winkel > winkels = new ArrayList < Winkel > ();
          Winkel winkel = new Winkel();
          try {
              // parse XML
              XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
              factory.setNamespaceAware(true);
              XmlPullParser xpp = factory.newPullParser();
              xpp.setInput(new StringReader(result));

              int eventType = xpp.getEventType();
              boolean isItem = false;
              while (eventType != XmlPullParser.END_DOCUMENT) {
                  if (eventType == XmlPullParser.START_TAG) {
                      if (xpp.getName().equalsIgnoreCase("company")) {
                          winkel = new Winkel();
                          isItem = true;
                      } else if (xpp.getName().equalsIgnoreCase("bedrijfsnaam") && isItem) {
                          xpp.next();
                          winkel.setNaam(xpp.getText());
                      } else if (xpp.getName().equalsIgnoreCase("postcde") && isItem) {
                          xpp.next();
                          winkel.setPostcode(xpp.getText());
                      } else if (xpp.getName().equalsIgnoreCase("deelgemeente") && isItem) {
                          xpp.next();
                          winkel.setGemeente(xpp.getText());
                      } else if (xpp.getName().equalsIgnoreCase("gemeente") && isItem) {
                          xpp.next();
                          winkel.setDeelGemeente(xpp.getText());
                      } else if (xpp.getName().equalsIgnoreCase("adres") && isItem) {
                          winkel.setAdres(xpp.getText());
                          winkels.add(winkel);
                          isItem = false;
                      }

                  }
                  eventType = xpp.next();
              }
          } catch (Exception e) {
              e.printStackTrace();
          }
          return winkels;
      }
  }

I have my parser at the bottom, I need to get the data from the webservice and read every line, put in my stringbuilder so it can be read and parsed by the parser. I think (???) I'm connecting to the database correctly, but how do I make the data I'm getting readable? Thank you

Edit: When trying to run it, I get this error:

07-27 13:31:57.993 17259-17288/com.example.hoofdgebruiker.winkelskortrijk E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                                            Process: com.example.hoofdgebruiker.winkelskortrijk, PID: 17259
                                                                                            java.lang.RuntimeException: An error occured while executing doInBackground()
                                                                                                at android.os.AsyncTask$3.done(AsyncTask.java:304)
                                                                                                at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
                                                                                                at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
                                                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
                                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                                                at java.lang.Thread.run(Thread.java:818)
                                                                                             Caused by: java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
                                                                                                at com.example.hoofdgebruiker.winkelskortrijk.Utill.ODKortrijkWebservice.doInBackground(ODKortrijkWebservice.java:28)
                                                                                                at com.example.hoofdgebruiker.winkelskortrijk.Utill.ODKortrijkWebservice.doInBackground(ODKortrijkWebservice.java:24)
                                                                                                at android.os.AsyncTask$2.call(AsyncTask.java:292)
                                                                                                at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                                                at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
                                                                                                at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
                                                                                                at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
                                                                                                at java.lang.Thread.run(Thread.java:818) 
user3117628
  • 752
  • 1
  • 10
  • 29

1 Answers1

0

You can use this:

BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8"))); String parseThis = reader.readLine();

That'll give you a string of the output. If it's in JSON, add:

JSONObject jsonObj = new JSONObject(parseThis); JSONArray arrayOfStuff = jsonObj.getJSONArray(array_tag);

Steeno
  • 122
  • 5
  • If I use that, it won't automatically read every line, correct? How do I read every line? Thank you btw – user3117628 Jul 27 '16 at 13:19
  • If it's in JSON, it's in one line, so parseThis will hold the whole JSONObject. If it actually is multiple, you can just call `reader.readLine()` again, it will jump to the next one. And you're very welcome. Good luck. – Steeno Jul 27 '16 at 13:31