0

Ok

try {

            Log.d("request!", "starting");
            // getting product details by making HTTP request
            JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
                    params);

            // check your log for json response
            Log.d("Login attempt", json.toString());

            }
        } catch (JSONException e) {

//ERROR WHEN INERTING CODE HERE!.

}<code>

Hi guys!, I just want to know why my app crashes when I write code in the indicated place (see above).. If I leave it blank, nothing happens...

The thing is, I want to show a

Toast.makeText(MainActivity.this, "Couldn't reach the server", Toast.LENGTH_LONG).show();

Why do you think that is?. Thanks in advance

frankelot
  • 13,666
  • 16
  • 54
  • 89

2 Answers2

2

Are you trying to access UI elements from outside the main thread by any chance? Try using Log.v instead of Toast and see if that helps.

VJ Vélan Solutions
  • 6,434
  • 5
  • 49
  • 63
  • Yesss :) it worked, thanks guys. I'm new to android. I can't access to UI elements if I'm not in the main thread! got it! – frankelot Sep 24 '13 at 02:09
0

I'm assuming because you're error is

"Couldn't reach the server"

you are trying to make network calls, which means your code is in a thread of some sort? You cannot touch UI elements from inside the main thread so move:

Toast.makeText(MainActivity.this, "Couldn't reach the server", Toast.LENGTH_LONG).show();

Into the postExecute() method of an AsyncTask or use a Handler

From the docs

Do not access the Android UI toolkit from outside the UI thread.

nedaRM
  • 1,837
  • 1
  • 14
  • 28
  • Yesss :) it worked, thanks guys. I'm new to android. I can't access to UI elements if I'm not in the main thread! got it! – frankelot Sep 24 '13 at 02:09