-2

I have a problem with HttpGet that I've searched on stackoverflow and on other websites, I even followed some examples but nothing to do, it doesn't work.

This is my code:

    HttpClient          client = new DefaultHttpClient();
    HttpGet             request = new HttpGet("http://myurl.com/getit?token=" + _token);
    HttpResponse        response;
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    String              jsonString;
    Gson                gson;
    Modules[]           modulesList;

    try {
        response = client.execute(request);
        //jsonString = EntityUtils.toString(response.getEntity());
        //gson = new GsonBuilder().setPrettyPrinting().create();
        //modulesList = gson.fromJson(jsonString, Modules[].class);
    } catch (Exception e) {
        System.out.println("FAIL: " + e.getMessage());
    }

It always return a null pointer exception. I've tried to display the URL, copy/paste in Chrome and the website does display my JSON.

  • Did you add the permission ""? – Martin Cazares Jan 26 '15 at 17:35
  • Thank you for your answer. Yes I did add this permission, actually I use HttpPost in another page to log in on my Android App (with the same API) and it works well – user3824849 Jan 26 '15 at 17:52
  • In that case you should add the Crash Log for a more accurate answer – Martin Cazares Jan 26 '15 at 17:53
  • Sorry this is my first post here, it's saying : FAIL: null – user3824849 Jan 26 '15 at 18:01
  • 1
    OpenGL is graphics related. Perhaps your app is crashing doing something else. – iheanyi Jan 26 '15 at 18:03
  • Actually, the app isn't crashing, just throwing an error and I can't get the JSON – user3824849 Jan 26 '15 at 18:06
  • The error you showed has nothing to do with the error you're describing in your question. You need to show the error that you are getting and the associated stack trace. The "FAIL" null is what you are printing in your catch statement. We need to see the actual exception data. – iheanyi Jan 26 '15 at 18:07
  • Can you change that to `e.printStackTrace()` and see what you get then? Seems your exception has no message. – Dawnkeeper Jan 26 '15 at 18:09
  • possible duplicate of [Unfortunately MyApp has stopped. How can I solve this?](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – njzk2 Jan 26 '15 at 18:24
  • Do you think it's possible that the app is throwing an error because it's doing too much things on the main thread ? – user3824849 Jan 26 '15 at 18:31
  • Why did you remove your stacktrace it clearly showed your error in it. – draksia Jan 26 '15 at 19:17

2 Answers2

2

The problem is quite clear in the first line of your stacktrace

/com.epitech.ferrei_j.epiandroid W/System.err﹕ android.os.NetworkOnMainThreadException 01-26

Post 2.3 Android you are not allowed to use the main UI thread to do tasks that can take a while to complete. Network IO is one of these.

You need to use an AsyncTask to execute the operation on its own thread.

See AsyncTask for examples on how to do this.

draksia
  • 2,371
  • 2
  • 20
  • 25
0

I usually write mine like this:

  private String downloadUrl(String myurl) throws IOException {
        InputStream is = null;
        // Only display the first 500 characters of the retrieved
        // web page content.
        int len = 500;

        try {
            URL url = new URL(myurl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(READTIMEOUT /* milliseconds */);
            conn.setConnectTimeout(CONNECTTIMEOUT /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            // Starts the query
            conn.connect();
            int response = conn.getResponseCode();
            //check to read content body if 400 error and above occurs.
            if (response >= HttpStatus.SC_BAD_REQUEST)
                is = conn.getErrorStream();
            else
                is = conn.getInputStream();

            // Convert the InputStream into a string
            String contentAsString = readIt(is, len);
            return contentAsString;

            // Makes sure that the InputStream is closed after the app is
            // finished using it.
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

    public String readIt(InputStream stream, int len) throws IOException,
            UnsupportedEncodingException {
        // Reader reader = null;
        // reader = new InputStreamReader(stream, "UTF-8");
        // char[] buffer = new char[stream.available()];
        // reader.read(buffer);
        // return new String(buffer);

        final BufferedReader r = new BufferedReader(new InputStreamReader(
                stream));
        final StringBuilder total = new StringBuilder();
        String line;
        while ((line = r.readLine()) != null) {
            total.append(line);
        }

        return total.toString();
    }

So all you have to do afterwards is :

jsonString = downloadUrl( "http://myurl.com/getit?token=" + _token );
ayz4sci
  • 2,220
  • 1
  • 16
  • 17