我想在此网站上添加一些信息“http://diyanet.gov.tr/tr/namazvakitleri”。在这个网站上有一个选择选项菜单,数据与更多选择菜单相关。
我的意思是,如果有人选择了选择菜单id=State
而选择了选择菜单id=City
,则会显示相关信息。示例Html代码如下。当州和城市选择时,最后的信息出现了。我想在我的应用中使用div selected option value of state
的数据之后选择selected option value of city
而不是class=vakitler
。但是我无法正确地从这个网站获取这些数据。任何人都可以帮助我。
<DIV id="stateHtml">
<SELECT name="State" id="State" style="width: 113px;"><OPTION value="">Seciniz</OPTION>
<OPTION value="500">ADANA</OPTION>
<OPTION value="501">ADIYAMAN</OPTION>
<OPTION value="502">AFYON</OPTION>
<OPTION value="503">AĞRI</OPTION>
<OPTION value="504">AKSARAY</OPTION>
<OPTION value="505">AMASYA</OPTION>
<OPTION selected="selected" value="506">ANKARA</OPTION>
<OPTION value="507">ANTALYA</OPTION>
<SELECT name="City" id="City" style="width: 113px;">
<OPTION value="">Seciniz</OPTION>
<OPTION value="9205">AKYURT</OPTION>
<OPTION selected="selected" value="9206">ANKARA</OPTION>
<OPTION value="9207">AYAŞ</OPTION>
<OPTION value="9208">BALA</OPTION>
<OPTION value="9209">BEYPAZARI</OPTION>
</SELECT></DIV>
<DIV class="vakitler">
<DIV id="liImsak"><SPAN>İmsak</SPAN><SPAN id="spImsak">03:41</SPAN></DIV>
<DIV id="liGunes"><SPAN>Güneş</SPAN><SPAN id="spGunes">05:30</SPAN></DIV>
<DIV id="liOgle"><SPAN>Öğle</SPAN><SPAN id="spOgle">13:02</SPAN></DIV>
<DIV id="liIkindi"><SPAN>İkindi</SPAN><SPAN id="spIkindi">16:55</SPAN></DIV>
<DIV id="liAksam"><SPAN>Akşam</SPAN><SPAN id="spAksam">20:22</SPAN></DIV>
<DIV id="liYatsi"><SPAN>Yatsı</SPAN><SPAN id="spYatsi">22:02</SPAN></DIV>
答案 0 :(得分:2)
检查此示例应用:
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
public class MainActivity extends AppCompatActivity {
TextView dataView;
Spinner countrySpinner, stateSpinner;
HashMap<String, String> countryMap, stateMap;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
countrySpinner = (Spinner) findViewById(R.id.country);
stateSpinner = (Spinner) findViewById(R.id.state);
dataView = (TextView) findViewById(R.id.textView);
button = (Button) findViewById(R.id.button);
countrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String countryName = countrySpinner.getSelectedItem().toString();
String countryId = countryMap.get(countryName);
new StateSpinnerTask(countryId).execute();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (countrySpinner != null && countrySpinner.getSelectedItem() != null &&
stateSpinner.getSelectedItem() != null) {
String countryName = countrySpinner.getSelectedItem().toString();
String stateName = stateSpinner.getSelectedItem().toString();
String countryId = countryMap.get(countryName);
String stateId = stateMap.get(stateName);
if (countryId != null && stateId != null)
new PrintDataTask(countryId, stateId).execute();
}
}
});
new CountrySpinnerTask().execute();
}
private class StateSpinnerTask extends AsyncTask<String, String, ArrayList<String>> {
String countryId;
public StateSpinnerTask(String countryId) {
this.countryId = countryId;
}
@Override
protected ArrayList<String> doInBackground(String... params) {
stateMap = new HashMap<>();
ArrayList<String> arrayList = new ArrayList<>();
Document doc = null;
try {
doc = Jsoup.connect("http://diyanet.gov.tr/PrayerTime/" +
"FillMainPageState?countryCode="
+ countryId).ignoreContentType(true).get();
JSONArray jsonArray = new JSONArray(doc.body().text().toString());
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = (JSONObject) jsonArray.get(i);
stateMap.put(jsonObject.get("Text").toString(),
jsonObject.get("Value").toString());
arrayList.add(jsonObject.get("Text").toString());
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return arrayList;
}
@Override
protected void onPostExecute(ArrayList<String> arrayList) {
ArrayAdapter adapter = new ArrayAdapter<>(getApplicationContext(),
android.R.layout.simple_spinner_item, arrayList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
stateSpinner.setAdapter(adapter);
}
}
private class CountrySpinnerTask extends AsyncTask<String, String, ArrayList<String>> {
@Override
protected ArrayList<String> doInBackground(String... params) {
countryMap = new HashMap<>();
ArrayList<String> arrayList = new ArrayList<>();
Document document = null;
try {
document = Jsoup.connect("http://diyanet.gov.tr/en/home").get();
} catch (IOException e) {
e.printStackTrace();
}
if (document != null) {
Element countryElement = document.select("select[id=Country]").first();
Elements countryElements = countryElement.getElementsByTag("option");
for (Element element : countryElements) {
countryMap.put(element.text(), element.attr("value"));
arrayList.add(element.text());
}
}
return arrayList;
}
@Override
protected void onPostExecute(ArrayList<String> arrayList) {
ArrayAdapter adapter = new ArrayAdapter<>(getApplicationContext(),
android.R.layout.simple_spinner_item, arrayList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
countrySpinner.setAdapter(adapter);
}
}
private class PrintDataTask extends AsyncTask<String, String, JSONObject> {
String countryName, stateName;
public PrintDataTask(String countryName, String stateName) {
this.countryName = countryName;
this.stateName = stateName;
}
@Override
protected JSONObject doInBackground(String... params) {
JSONObject jsonObject = null;
try {
Connection.Response response = Jsoup
.connect("http://diyanet.gov.tr/PrayerTime/MainPrayerTimesSet")
.ignoreContentType(true)
.data("countryName", countryName)
.data("name", stateName)
.method(Connection.Method.POST)
.execute();
jsonObject = new JSONObject(response.parse().body().text().toString());
} catch (JSONException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return jsonObject;
}
@Override
protected void onPostExecute(JSONObject jsonObject) {
if (jsonObject != null)
try {
dataView.setText(jsonObject.get("Imsak")
+ " " + jsonObject.get("Gunes")
+ " " + jsonObject.get("Ogle")
+ " " + jsonObject.get("Ikindi")
+ " " + jsonObject.get("Aksam")
+ " " + jsonObject.get("Yatsi"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
您可以从here
下载完整源代码