Skip to content

Instantly share code, notes, and snippets.

@Pavneet-Sing
Forked from anonymous/activity_main.xml
Last active July 18, 2017 15:33
Show Gist options
  • Save Pavneet-Sing/470b2d13d1b2987a0de4a84382b434fd to your computer and use it in GitHub Desktop.
Save Pavneet-Sing/470b2d13d1b2987a0de4a84382b434fd to your computer and use it in GitHub Desktop.
<?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>
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();
}
}
}
@Pavneet-Sing
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment