package com.hussienalrubaye.services; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import org.json.JSONObject; import java.io.BufferedInputStream; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class MainActivity extends AppCompatActivity { EditText etCity; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); etCity=(EditText) findViewById(R.id.etCity); } public void buGet(View view) { String url="https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20weather.forecast%20where%20woeid%20in%20(select%20woeid%20from%20geo.places(1)%20where%20text%3D%22"+ etCity.getText().toString() +"%22)&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys"; new MyAsyncTaskgetNews().execute(url); } // get news from server public class MyAsyncTaskgetNews extends AsyncTask<String, String, String> { @Override protected void onPreExecute() { //before works } @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub try { String NewsData; //define the url we have to connect with URL url = new URL(params[0]); //make connect with url and send request HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); //waiting for 7000ms for response urlConnection.setConnectTimeout(7000);//set timeout to 5 seconds try { //getting the response data InputStream in = new BufferedInputStream(urlConnection.getInputStream()); //convert the stream to string NewsData = ConvertInputToStringNoChange(in); //send to display data publishProgress(NewsData); } finally { //end connection urlConnection.disconnect(); } }catch (Exception ex){} return null; } protected void onProgressUpdate(String... progress) { try { JSONObject json= new JSONObject(progress[0]); JSONObject query=json.getJSONObject("query"); JSONObject results=query.getJSONObject("results"); JSONObject channel=results.getJSONObject("channel"); JSONObject astronomy=channel.getJSONObject("astronomy"); String sunset=astronomy.getString("sunset"); String sunrise=astronomy.getString("sunrise"); //display response data Toast.makeText(getApplicationContext(),"sunset:"+ sunset + ",sunrise:"+ sunrise,Toast.LENGTH_LONG).show(); } catch (Exception ex) { } } protected void onPostExecute(String result2){ } } // this method convert any stream to string public static String ConvertInputToStringNoChange(InputStream inputStream) { BufferedReader bureader=new BufferedReader( new InputStreamReader(inputStream)); String line ; String linereultcal=""; try{ while((line=bureader.readLine())!=null) { linereultcal+=line; } inputStream.close(); }catch (Exception ex){} return linereultcal; } }