-
-
Save Pavneet-Sing/470b2d13d1b2987a0de4a84382b434fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent"> | |
<TextView | |
android:id="@+id/textView" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_centerHorizontal="true" | |
android:layout_centerVertical="true" | |
android:text="TextView" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.miccy.myapplication; | |
import android.os.AsyncTask; | |
import android.support.v7.app.AppCompatActivity; | |
import android.os.Bundle; | |
import android.util.Log; | |
import android.widget.TextView; | |
import java.io.InputStream; | |
import java.io.InputStreamReader; | |
import java.net.HttpURLConnection; | |
import java.net.URL; | |
public class MainActivity extends AppCompatActivity { | |
public class WeatherAPI extends AsyncTask<String, Void, String>{ | |
@Override | |
protected String doInBackground(String... apiUrl) { | |
URL url; | |
HttpURLConnection httpURLConnection = null; | |
InputStream is; | |
InputStreamReader isr; | |
int data; | |
// String result = ""; | |
try{ | |
url = new URL(apiUrl[0]); | |
httpURLConnection = (HttpURLConnection) url.openConnection(); | |
is = httpURLConnection.getInputStream(); | |
isr = new InputStreamReader(is); | |
// data = isr.read(); | |
// while(data!=-1){ | |
// result += (char) data; | |
// data = isr.read(); | |
// } | |
// more concise approach | |
StringBuilder strB = new StringBuilder(); // create a String builder , more efficient internally | |
while((data = isr.read()) != -1) // read and assign simultaneously | |
strB.append((char)ch); // add char to builder | |
return strB.toString(); // return string | |
} | |
catch(Exception e){ | |
e.printStackTrace(); | |
} | |
return null; | |
} | |
} | |
@Override | |
protected void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
WeatherAPI objectapi = new WeatherAPI(); | |
String finalCode = ""; | |
TextView textView = (TextView) findViewById(R.id.textView); | |
try{ | |
finalCode = objectapi.execute("http://api.openweathermap.org/data/2.5/weather?q=London&APPID=ef10661606adcfb9eb4b4808b44d07c2").get(); | |
Log.i("API:",finalCode); | |
textView.setText(finalCode); | |
} | |
catch (Exception e){ | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A solution to issue in gist at https://gist.github.com/anonymous/960f9a9fc49ee50e20e75b487ed19c69
The issue represents an infinite loop where the value of data field is never changer inside loop hence infinite loop