Reputation: 1731
I am trying to parse some data using JSoup, this is all happening in a asynctask (doInBackground) part of my MainActivity. Unfortunately all the elements (9) are empty when I execute the app.
When I debug below codeline, I actually get the complete website, it`s all there. The method readMultipleLinesRespone() is located in another class HttpUtility where I also call my Post and Get requests.
I tested this upfront by saving the website as a file and using JSoups assets ability, it worked perfectly then.
The setupAdapter() method in onPostExecute fills a ExpandableListview with data, should this info be nessecary. If you need more info pls ask.
Can somebody assist and tell me what I am doing wrong?
response1 = util.readMultipleLinesRespone(); <--- debugged and all data (seems) to be there but isn`t.
Edit: If I print response1, there is indeed no data to parse. Logcat output:
E/Resonse:: [Ljava.lang.String;@3d3410a
Below is the method readMultipleLinesRespone from HttpUtility class:
public String[] readMultipleLinesRespone() throws IOException { InputStream inputStream = null; if (httpConn != null) { inputStream = httpConn.getInputStream(); } else { throw new IOException("Connection is not established."); } BufferedReader reader = new BufferedReader(new InputStreamReader( inputStream)); List<String> response = new ArrayList<String>(); String line = ""; while ((line = reader.readLine()) != null) { response.add(line); } reader.close(); return (String[]) response.toArray(new String[0]); }
The asynctask where it`s all hapening:
private class FetchWebsiteData extends AsyncTask<Void, Void, Void> { ProgressDialog mProgressDialog; @Override protected void onPreExecute() { this.mProgressDialog = new ProgressDialog(getActivity()); mProgressDialog.setMessage("Laden..."); mProgressDialog.setIndeterminate(false); mProgressDialog.show(); } @Override protected Void doInBackground(Void... result) { try { util.sendGetRequest("https://mobile.somesite.nl/Data", null); response1 = util.readMultipleLinesRespone(); } catch (IOException e) { e.printStackTrace(); } if (response1.length > 0) { Document doc = Jsoup.parse(response1.toString()); // Get the html document title Elements els = doc.select("span[class=item-value pull-right]"); if (els.size() > 0) { fac_naam = els.get(0).text(); fac_straat = els.get(1).text(); fac_post = els.get(2).text(); con_tel = els.get(3).text(); con_email = els.get(4).text(); betaal_reknr = els.get(5).text(); betaal_houd = els.get(6).text(); zig_gebruiker = els.get(7).text(); zig_wacht = els.get(8).text(); } } return null; } @Override protected void onPostExecute(Void result) { super.onPreExecute(); setupAdapter(); mProgressDialog.dismiss(); } }
Upvotes: 1
Views: 551
Reputation: 1731
In the meantime I solved the problem. I did not pass the response string correctly to the asynctask which parses the required elements. Just required a public string in which the response is being set and passed (not an elegant way but it works):
public static String HttpResponse = "";
In the HttpUtility class:
public String[] readMultipleLinesRespone() throws IOException {
...
TabFragment1.HttpResponse = response.toString();
...
return (String[]) response.toArray(new String[0]);
}
Then pass it to the asynctask:
@Override
protected Void doInBackground(Void... result) {
try {
util.sendGetRequest(LoginActivity.PersData_URL, null);
util.readMultipleLinesRespone();
} catch (IOException e) {
e.printStackTrace();
}
if (HttpResponse.length() > 0) {
Document doc = Jsoup.parse(HttpResponse.toString());
// Get the html document title
Elements els = doc.select("span[class=item-value pull-right]");
...
}
return null;
}
Upvotes: 1