-2

I am a novice to Android Development and Java, I am a C# Developer. I cannot debug through this. I tried to get the System.out.println to spit out the exception message and stack trace but they are not working for some reason.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.util.Date;
import java.net.URL;
import org.json.JSONObject;

public class Weather {

public Base Base = new Base();
public Cloud Cloud = new Cloud();
public Coord Coord = new Coord();
public Info Info = new Info();
public Location Location = new Location();
public Temperature Temperature = new Temperature();
public Wind Wind = new Wind();

public Boolean fetch(String location) {

    try {

        String urlString = "http://api.openweathermap.org/data/2.5/weather?q=" + location + "&units=metric";
        URL url = new URL(urlString);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        BufferedReader reader = new BufferedReader( new InputStreamReader ( connection.getInputStream() ) );
        StringBuffer json = new StringBuffer(1024);
        String tmp = "";
        while ( ( tmp = reader.readLine() ) != null )
            json.append(tmp).append("\n");
        reader.close();
        JSONObject data = new JSONObject( json.toString() );
        if (data.getInt("cod") != 200) {

            throw new Exception("cod is not 200");

        } else {

            Base.base = data.getString("base");

            Cloud.Value = data.getJSONObject("clouds").getInt("all");

            Coord.lat = data.getJSONObject("coord").getDouble("lat");
            Coord.lon = data.getJSONObject("coord").getDouble("lon");

            // Array Processing
            JSONObject id = data.getJSONArray("weathr").getJSONObject(0);
            JSONObject main = data.getJSONArray("weather").getJSONObject(1);
            JSONObject description = data.getJSONArray("weather").getJSONObject(2);
            JSONObject icon = data.getJSONArray("weather").getJSONObject(3);

            Info.description = description.getString("description");
            Info.icon = icon.getString("icon");
            Info.id = id.getInt("id");
            Info.main = main.getString("main");

            Location.cod = data.getInt("cod");
            Location.dt = new Date((long) data.getInt("dt") * 1000);
            Location.country = data.getJSONObject("sys").getString("country");
            Location.id = data.getInt("id");
            Location.message = data.getJSONObject("sys").getDouble("message");
            Location.name = data.getString("name");
            Location.sunrise = new Date((long) data.getJSONObject("sys").getInt("sunrise"));
            Location.sunset = new Date((long) data.getJSONObject("sys").getInt("sunset"));

            Temperature.humidity = data.getJSONObject("main").getInt("humidity");
            Temperature.max = data.getJSONObject("main").getInt("temp_max");
            Temperature.min = data.getJSONObject("main").getInt("temp_min");
            Temperature.pressure = data.getJSONObject("main").getInt("pressure");
            Temperature.temp = data.getJSONObject("main").getInt("temp");

            Wind.deg = data.getJSONObject("wind").getInt("deg");
            Wind.speed = data.getJSONObject("wind").getDouble("speed");

        }

        return true;        

    } catch (Exception ex) {

        System.out.println("Mayday, " + ex.getMessage().toString() +  ex.getStackTrace().toString());
        return false;

    }
}

So, how can I fix this, Can you also provde a better way to debug..

UPDATE:- I got the StackTrace out but I cannot understand why the error is happening. Can you guys explain this

08-01 00:41:05.010: W/System.err(8557): android.os.NetworkOnMainThreadException
08-01 00:41:05.010: W/System.err(8557):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
08-01 00:41:05.010: W/System.err(8557):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
08-01 00:41:05.010: W/System.err(8557):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
08-01 00:41:05.010: W/System.err(8557):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
08-01 00:41:05.010: W/System.err(8557):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
08-01 00:41:05.010: W/System.err(8557):     at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
08-01 00:41:05.010: W/System.err(8557):     at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
08-01 00:41:05.010: W/System.err(8557):     at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
08-01 00:41:05.010: W/System.err(8557):     at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
08-01 00:41:05.010: W/System.err(8557):     at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
08-01 00:41:05.010: W/System.err(8557):     at libcore.net.http.HttpEngine.connect(HttpEngine.java:310)
08-01 00:41:05.010: W/System.err(8557):     at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
08-01 00:41:05.010: W/System.err(8557):     at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
08-01 00:41:05.010: W/System.err(8557):     at libcore.net.http.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:273)
08-01 00:41:05.020: W/System.err(8557):     at libcore.net.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:168)
08-01 00:41:05.020: W/System.err(8557):     at antu.mango.weather.Weather.fetch(Weather.java:27)
08-01 00:41:05.020: W/System.err(8557):     at antu.mango.drizzle.MainActivity.onCreate(MainActivity.java:26)
Antu
  • 13
  • 4
  • possible duplicate of [android.os.NetworkOnMainThreadException](http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception) – Tom Jul 31 '15 at 21:51

2 Answers2

2

Instead of trying to print out the stack trace manually, try this:

ex.printStackTrace();

This will give you an annotated, class by class, line by line error message stating what happened.

Also, it's really, really, REALLY bad practice to put everything in one giant try/catch block. Try breaking up your code into logical blocks and try/catching each one so you can be more fine-grained with your error checking.

nameless912
  • 367
  • 1
  • 12
  • Okay, I tried to do it. Now I got where the error is. But the reason is why is this error there...... It is at the "BufferedReader reader". I have updated the question – Antu Jul 31 '15 at 21:40
  • If you google NetworkOnMainThread exception, you'll find that it means that you can't do any networking-related tasks on the main thread in Android. This is a safeguard to prevent the very bad practice of the network blocking the GUI. What you're going to want to do is learn how to use an AsyncTask. Once again, google it, the android API docs are excellent. – nameless912 Aug 01 '15 at 18:01
0

If you are developing in Android, I recommend you to use

Log.d("SOME TEXT", data_to_analyze);

It's like System.out.print(data_to_analyze); but in Android. You can see what you want in the android Logcat

Jimmy A. León
  • 541
  • 3
  • 6
  • 22