0

I'm trying get a JSON from this url using this class.

I'm using getJSONFromUrl like that:

JSONParser jParser = new JSONParser();//Instantiate class JSONObject json = jParser.getJSONFromUrl("http://tcc-teste.aws.af.cm/api/get/aluno/1");//using getJSNOFromUrl method 

And I'm geting this error on logcat:

08-28 22:46:06.560: E/AndroidRuntime(905): FATAL EXCEPTION: main 08-28 22:46:06.560: E/AndroidRuntime(905): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.br.edu.unifoa/com.example.br.edu.unifoa.MainActivity}: android.os.NetworkOnMainThreadException 08-28 22:46:06.560: E/AndroidRuntime(905): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) 08-28 22:46:06.560: E/AndroidRuntime(905): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 08-28 22:46:06.560: E/AndroidRuntime(905): at android.app.ActivityThread.access$600(ActivityThread.java:141) 08-28 22:46:06.560: E/AndroidRuntime(905): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 08-28 22:46:06.560: E/AndroidRuntime(905): at android.os.Handler.dispatchMessage(Handler.java:99) 08-28 22:46:06.560: E/AndroidRuntime(905): at android.os.Looper.loop(Looper.java:137) 08-28 22:46:06.560: E/AndroidRuntime(905): at android.app.ActivityThread.main(ActivityThread.java:5103) 
1
  • Just read the error, it says no network on main thread!?!?!?!, use AsynchTask or something else... Commented Aug 29, 2013 at 2:55

3 Answers 3

1

Use this before you Parsing

public static int SDK_INT = android.os.Build.VERSION.SDK_INT; if (SDK_INT >= 10) { ThreadPolicy tp = ThreadPolicy.LAX; StrictMode.setThreadPolicy(tp); } 

add this permission in manifest file

<uses-permission android:name="android.permission.INTERNET" /> 
Sign up to request clarification or add additional context in comments.

1 Comment

You can use async tasks also.but if you have small amount of data to parse it is ok.
0

You are trying to parse on UI Thread (Main thread) whihc is causing this error. Run an asynctask and do it. It wont crash then. Any networking or long running work should not be done on UI thread as it blocks it.

1 Comment

Happy to help :) .. it happened to most of us when we started :)
0

thank you brother but change like this public static int SDK_INT = android.os.Build.VERSION.SDK_INT;

{ if (SDK_INT >= 10) { StrictMode.ThreadPolicy tp = StrictMode.ThreadPolicy.LAX; StrictMode.setThreadPolicy(tp); } } 

1 Comment

This answer does not seem to match the query. Can you please explain why this answer solves the question, if they match?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.