2015-07-22 29 views
0

我想從本網站添加一些信息「http://diyanet.gov.tr/tr/namazvakitleri」。在這個網站有一個選擇選項菜單,數據與更多選擇選項菜單相關。我想從jsoup網站採取一些infromation到android

我的意思是如果有人選擇菜單id=State並且比選擇菜單id=City顯示的信息。示例Html代碼如下。當州和市選擇時,最後的信息出現。我想選擇selected option value of state而不是selected option value of city之後我在我的應用程序中使用div 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> 

回答

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

enter image description here

+0

謝謝你這麼多。幾乎我研究了這個問題約3周。再次感謝。我最大的敬意...... –

+2

@MehmetGÜL如果你花了大約3周的時間,你可以花上一秒鐘,並將基準答案標記爲「已接受」。 – TDG