所以我在YouTube上找到了这个代码(Credit转到" Indragni Soft Solutions")我想改变它以便与其他网页一起使用。 所以这很简单,有一个listview,我希望listview显示在这里工作的所有人(http://www.muckendorf-wipfing.at/12-0-Amts-+und+Sprechstunden.html),但不是办公室工作人员剩下的其他东西。要尽可能清楚:我想要标签中的所有内容。因此,div#content-r中的所有内容都应该进入我的列表,旁边有图片,当用户点击其中一个时,应显示全文。但这并不是必须的,因为如果只有顶部名称,图片左侧和名称下的电子邮件,它也会起作用。正如我所说,这是我试过的代码:
function getData(data) {
for (var i = 0; i < data.length; i++) {
var value = data[i];
var item = {};
var array = value.split(",");
for (var j = 0; j < array.length; j++) {
var element = array[j].split(":");
var key = element[0].trim();
var value = element[1].trim();
item[key] = value;
}
result.push(item);
}
return result;
}
这是CountryJSONParser(由Aylar-HP创建):
package com.mrousavy.gemeindemuckendorfwipfing;
public class EcoActivity extends AppCompatActivity {
ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lv);
String strUrl = "http://wptrafficanalyzer.in/p/demo1/first.php/countries"; //This String should be http://www.muckendorf-wipfing.at/12-0-Amts-+und+Sprechstunden.html but I let it like this so you can see how the template-result looks
DownloadTask downloadTask = new DownloadTask();
downloadTask.execute(strUrl);
mListView = (ListView) findViewById(R.id.lv_countries);
}
private String downloadUrl(String strUrl) throws IOException {
String data = "";
InputStream iStream = null;
try {
URL url = new URL(strUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
iStream = urlConnection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(iStream));
StringBuffer sb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line);
}
data = sb.toString();
br.close();
} catch (Exception e) {
Log.d("Exception while downloading url", e.toString());
} finally {
iStream.close();
}
return data;
}
private class DownloadTask extends AsyncTask<String, Integer, String> {
String data = null;
@Override
protected String doInBackground(String... url) {
try {
data = downloadUrl(url[0]);
} catch (Exception e) {
Log.d("Background Task", e.toString());
}
return data;
}
@Override
protected void onPostExecute(String result) {
ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();
listViewLoaderTask.execute(result);
}
}
private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter> {
JSONObject jObject;
@Override
protected SimpleAdapter doInBackground(String... strJson) {
try {
jObject = new JSONObject(strJson[0]);
CountryJSONParser countryJsonParser = new CountryJSONParser();
countryJsonParser.parse(jObject);
} catch (Exception e) {
Log.d("JSON Exception1", e.toString());
}
CountryJSONParser countryJsonParser = new CountryJSONParser();
List<HashMap<String, Object>> countries = null;
try {
countries = countryJsonParser.parse(jObject);
} catch (Exception e) {
Log.d("Exception", e.toString());
}
String[] from = {"country", "flag", "details"};
int[] to = {R.id.tv_country, R.id.iv_flag, R.id.tv_country_details};
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.lv_layout, from, to);
return adapter;
}
@Override
protected void onPostExecute(SimpleAdapter adapter) {
mListView.setAdapter(adapter);
for (int i = 0; i < adapter.getCount(); i++) {
HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
String imgUrl = (String) hm.get("flag_path");
ImageLoaderTask imageLoaderTask = new ImageLoaderTask();
HashMap<String, Object> hmDownload = new HashMap<String, Object>();
hm.put("flag_path", imgUrl);
hm.put("position", i);
imageLoaderTask.execute(hm);
}
}
}
private class ImageLoaderTask extends AsyncTask<HashMap<String, Object>, Void, HashMap<String, Object>> {
@Override
protected HashMap<String, Object> doInBackground(HashMap<String, Object>... hm) {
InputStream iStream = null;
String imgUrl = (String) hm[0].get("flag_path");
int position = (Integer) hm[0].get("position");
URL url;
try {
url = new URL(imgUrl);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
iStream = urlConnection.getInputStream();
File cacheDirectory = getBaseContext().getCacheDir();
File tmpFile = new File(cacheDirectory.getPath() + "/wpta_" + position + ".png");
FileOutputStream fOutStream = new FileOutputStream(tmpFile);
Bitmap b = BitmapFactory.decodeStream(iStream);
b.compress(Bitmap.CompressFormat.PNG, 100, fOutStream);
fOutStream.flush();
fOutStream.close();
HashMap<String, Object> hmBitmap = new HashMap<String, Object>();
hmBitmap.put("flag", tmpFile.getPath());
hmBitmap.put("position", position);
return hmBitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(HashMap<String, Object> result) {
String path = (String) result.get("flag");
int position = (Integer) result.get("position");
SimpleAdapter adapter = (SimpleAdapter) mListView.getAdapter();
HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(position);
hm.put("flag", path);
adapter.notifyDataSetChanged();
}
}
}
所以我的问题是;如何更改该代码以使用我的案例?我不知道如何从网站上获取div#content-r,然后将其放入我的列表..:/谢谢!