-4

I'm newbye in android world and I'm trying to retrieve a JSONArray from this URL: http://www.softmarketing.it/json/view/azienda/7

TabHostExample.java:

public class TabHostExample extends TabActivity {

private static String url = "http://www.softmarketing.it/json/view/azienda/7";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tab_host_example);

    //JSONParser parser= new JSONParser();
    //JSONObject myJSON= parser.makeHttpRequest(url, "POST", null);
    try{
        // Create a new HTTP Client
        DefaultHttpClient defaultClient = new DefaultHttpClient();
        // Setup the get request
        HttpGet httpGetRequest = new HttpGet("http://www.softmarketing.it/json/view/azienda/7");

        // Execute the request in the client
        HttpResponse httpResponse = defaultClient.execute(httpGetRequest);
        // Grab the response
        BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
        String json = reader.readLine();

        // Instantiate a JSON object from the request response
        JSONObject jsonObject = new JSONObject(json);

    } catch(Exception e){
        // In your production code handle any errors and catch the individual exceptions
        e.printStackTrace();
    }

           .....

In the LogCat I see this:

12-16 08:14:41.987: E/MYAPP(1183): exception: null
12-16 08:21:34.927: E/MYAPP(1245): exception: null

and the variable e has these values:

e: cause: NetworkOnMainThreadEception

and others values...

Could you help me please? It's been three days that I'm trying to solve the parsing...Thanks

Kamlesh Arya
  • 4,864
  • 3
  • 21
  • 28
user3107388
  • 81
  • 1
  • 5
  • 5
    You are performing a network connection on the main thread. This is not allowed in Android. You have to use an AsyncTask. http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Alexis C. Dec 16 '13 at 13:29
  • Could you explain me what I have to do with an example? – user3107388 Dec 16 '13 at 13:32
  • There's a lot of tutorials on the web. I'm sure you will find one =) – Alexis C. Dec 16 '13 at 13:39

2 Answers2

0

So, you will need to get the json content in a separate thread than the UI... and then parse it.

You can use an Asynctask and HttpGetClient

Or

You can use Android Asynchronous Http Client library. Very simple lib, tiny size (25kb), easy to use and Asynchronous The code will be something like :

AsyncHttpClient client = new AsyncHttpClient();
client.get("http://www.softmarketing.it/json/view/azienda/7", new AsyncHttpResponseHandler() {
    @Override
    public void onSuccess(String response) {
      // Here is your content !
      // now you have to parse it
      System.out.println(response);
    }
});

Available here

Then, you will have to parse to file as you want.(If it's json format I recommend you Gson library)

The rest is on you !

azerto00
  • 1,001
  • 6
  • 18
  • I tried with your solution but I receive this exception: 12-16 09:06:33.037: E/AsyncHttpRequest(1295): java.lang.SecurityException: Permission denied (missing INTERNET permission?) – user3107388 Dec 16 '13 at 14:08
  • AS the exception said, you have to add INTERNET permission in your manifest ... add in the manifest element – azerto00 Dec 16 '13 at 14:10
  • added the permission but now I have this error: 12-16 18:17:22.368: E/AndroidRuntime(1150): java.lang.RuntimeException: Unable to instantiate application android.permission.INTERNET: java.lang.ClassNotFoundException: Didn't find class "android.permission.INTERNET" on path: DexPathList[[zip file "/data/app/com.example.mytabstest-1.apk"],nativeLibraryDirectories=[/data/app-li‌​b/com.example.mytabstest-1, /system/lib]] help please :( – user3107388 Dec 17 '13 at 07:57
0

Android doesn't allow you to perform slow operations on the main thread (GUI thread), these include IO operations such as network activity.

If this wasn't the case and your android app was posting to an online service, because the thread would be busy waiting for the server response, the GUI would lock up, and user input would be ignored until the thread had finished sending and receiving the response to the web service. If this goes on for too long, I think around 5 seconds, your application would show up an ANR message (Application Not Responding). This will allow the user to either wait, and hope your app recovers, or force stop the app, this is the most likely option.

You also need to ensure that you have the android:name="android.permission.INTERNET" within your projects AndroidManifest file.

Boardy
  • 35,417
  • 104
  • 256
  • 447
  • added android:name="android.permission.INTERNET" but app crashes with this error: 12-16 18:17:22.368: E/AndroidRuntime(1150): java.lang.RuntimeException: Unable to instantiate application android.permission.INTERNET: java.lang.ClassNotFoundException: Didn't find class "android.permission.INTERNET" on path: DexPathList[[zip file "/data/app/com.example.mytabstest-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.mytabstest-1, /system/lib]] – user3107388 Dec 16 '13 at 23:18
  • Post your AndroidManifestFile for anyone to be able to help – Boardy Dec 17 '13 at 09:50
  • Thanks but I decided to rewrite my project including a class for parsing the json...If I have some problem I will post it...thanks all' anyway – user3107388 Dec 17 '13 at 13:05