入住此示例應用程序:
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
![enter image description here](https://i.stack.imgur.com/TnSmQ.png)
謝謝你這麼多。幾乎我研究了這個問題約3周。再次感謝。我最大的敬意...... –
@MehmetGÜL如果你花了大約3周的時間,你可以花上一秒鐘,並將基準答案標記爲「已接受」。 – TDG