Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mahditavakoli1312/6a27eb6a8e472bdc343b28a1a64690da to your computer and use it in GitHub Desktop.
Save mahditavakoli1312/6a27eb6a8e472bdc343b28a1a64690da to your computer and use it in GitHub Desktop.
package first.example.apijsoninternet;
import androidx.appcompat.app.AppCompatActivity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
Button bt;
TextView tv1, tv2;
String wheather1=null, weather2=null, weather3;
String url = "http://api.openweathermap.org/data/2.5/weather?lat=36.297001&lon=59.606201&appid=5706d8dd536bbf4d2bdc8debeb27e145";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = findViewById(R.id.tv1);
tv2 = findViewById(R.id.tv2);
bt = findViewById(R.id.bt);
bt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ReadJson readJson = new ReadJson();
readJson.execute();
}
});
}
class ReadJson extends AsyncTask<String, String, String> {
@Override
protected String doInBackground(String... strings) {
InputStream JSONStream = getStreamFromURL(url,"GET");
String JSONString = streamToString(JSONStream);
parseJSON(JSONString);
return null;
}
@Override
protected void onPostExecute(String s) {
tv1.setText(wheather1);
tv2.setText(weather2);
}
}
InputStream getStreamFromURL(String urlString,String method){
URL url = null;
try {
url = new URL(urlString);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
huc.setReadTimeout(10000);
huc.setConnectTimeout(15000);
huc.setRequestMethod(method);
huc.setDoInput(true);
huc.connect();
return huc.getInputStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
String streamToString (InputStream is){
String result=null;
String line;
try {
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while((line=br.readLine())!=null){
result+=(line+"\n");
}
}
catch(Exception ex){ex.printStackTrace();
}
return result;
}
void parseJSON(String jsonString){
try {
JSONObject jo = new JSONObject(jsonString);
JSONArray ja = jo.getJSONArray("weather");
JSONObject weather = ja.getJSONObject(0);
wheather1 = weather.getString("description");
JSONObject name = jo.getJSONObject("wind");
weather2 = name.getDouble("speed")+"";
} catch (JSONException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment