I want to create an app that allows me to search for a city in this weather site. I would like you to return the site corresponding to the search performed. I tried to do this with an EditText and a Button, but the search does not seem to work, because the page returned is the same as the initial one.
How can I solve this problem?
This is my code:
final EditText editText = findViewById(R.id.edit);
final TextView textView = findViewById(R.id.testo);
Button button = findViewById(R.id.clicca);
button.setOnClickListener(new View.OnClickListener() {
Connection.Response res = null;
Document doc;
@Override
public void onClick(View v) {
try {
doc = Jsoup.connect("https://www.ilmeteo.it/meteo/cerca")
.data("citta", "bari")
.post();
} catch (IOException e) {
e.printStackTrace();
}
textView.setText(doc.location());
}
});
This is the site's HTML code:
<div id="search">
<a id="search-logo" href="https://www.ilmeteo.it" title="IL Meteo - Home Page"></a>
<a id="search-arrow" href="javascript:;" onclick="toggleSearchMenu('main');"></a>
<form id="form-search0" name="search0" action="https://www.ilmeteo.it/meteo/cerca" method="get" onsubmit="return CheckSearchForm0()">
<input id="search-main" name="citta" value="" size="17" maxlength="64" class="txtSearch" onfocus="this.className='txtSearch';openSearchMenu('main');virginSearch=false;" onblur="if(this.value=='')this.className='txtSearch txtSearchE'" title="Cerca comune o località" autocomplete="Off" tabindex="1" onkeyup="ajax_showOptions(this,'type=IT&sort=smart',event)" type="text">
</form>
<a id="search-button" href="javascript:;" onclick="$('#form-search0').submit()"></a>
<div id="fav-search-cont"><span id="fav-search"></span></div>
</div>
EDIT
Thank you all! Your answers have solved my problem :) But I have problems with another weather site. How do I instead from this other site do the same operation as before?
P.S .: the problem of this site is the mandatory click of the city to search and that next to the city in the URL there is a code, like this "http://www.meteo.it/meteo/roma-58091".
This is the second site's HTML code:
<div class="pksrc">
<form class="search-form" onsubmit="return false">
<fieldset class="icon-lens">
<input type="hidden" id="searchid" disabled="" value="">
<input type="hidden" id="searchtarget" value="_blank">
<input type="text" class="query " id="searchinput" name="search" value="" placeholder="Cerca località" autocomplete="off">
<input type="submit" value="submit">
</fieldset>
</form>
<div id="search-menu"></div>
<ul id="search-option">
<li><a href="/meteo/milano-15146" title="Milano">Milano</a></li>
<li><a href="/meteo/roma-58091" title="Roma">Roma</a></li>
<li><a href="/meteo/napoli-63049" title="Napoli">Napoli</a></li>
</ul>
</div>
Answer for your second question:
This is the way I found out to navigate to the "city" page you want to search.
Step 1:
Pass the starting letter of the city to the request and get a JSON response.
Ex: If you want to search "Milano", then get the results for letter "m" using this URL http://www.meteo.it/autosuggest/m.json?
The sample JSON response is:
{
"url": [
{
"ita": "meteo",
"sea": "meteo-mare",
"ski": "meteo-montagna",
"eur": "meteo",
"wor": "meteo"
}
],
"results": [
{
"code": "15146",
"value": "Milano (MI)",
"value_it": "milano",
"value_en": "milan",
"url": "ita"
},
{
"code": "20030",
"value": "Mantova (MN)",
"value_it": "mantova",
"value_en": "mantua",
"url": "ita"
},
]
}
From the JSON response get the milano city's code & value_it
Ex: code=15146 & value_it=milano
Step2:
Construct the URL using retrieved values. Ex: http://www.meteo.it/meteo/value_it-code
http://www.meteo.it/meteo/milano-15146
Example for Comacchio city:
Request URL : http://www.meteo.it/autosuggest/c.json
JSON Response:
{
"code": "38006",
"value": "Comacchio (FE)",
"value_it": "comacchio",
"value_en": "comacchio",
"url": "ita"
}
Construct URL using JSON values:
http://www.meteo.it/meteo/comacchio-38006