如何从网站获取我的数据,以便我可以解析它?

时间:2016-07-27 12:55:14

标签: java android web-services

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

我的解析器位于底部,我需要从webservice获取数据并读取每一行,放入我的stringbuilder中,以便解析器可以读取和解析它。我认为(???)我正确地连接到数据库,但是如何使数据变得可读?谢谢

编辑:尝试运行时,我收到此错误:

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) 

1 个答案:

答案 0 :(得分:0)

您可以使用:

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

那会给你一串输出。如果是JSON,请添加:

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