lundi 20 juin 2016

JSONResponseHandler not implementing new cz.msebera.android.httpclient class

Right, so I know the old org.apache.http classes have been deprecated in the newer sdks, so I found out that the new one to use is the cz.msebera version.

Upon importing this one instead, when I implement a JSONResponseHandler and @Override its onSuccess and onFailure methods, I get an error telling me

Method does not override method from its superclass.

On further inspection, the superclass is still importing and using the org.apache.http class, which of course is a problem seeing as my methods are trying to override it with the cz.msebera Header[] instead.

I'm importing this httpclient class:

import cz.msebera.android.httpclient.Header;

This is my AsyncTask class:

private class JSONTask extends AsyncTask<Void, Void, JSONObject> {

    @Override
    protected JSONObject doInBackground(Void... params) {
        String url = "json source";

        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;

        try {
            URL url2 = new URL(url);

            urlConnection = (HttpURLConnection) url2.openConnection();
            urlConnection.connect();

            InputStream inputStream = urlConnection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(inputStream));

            StringBuffer buffer = new StringBuffer();
            String line = "";

            while ((line = reader.readLine()) != null) {
                buffer.append(line);
            }

            return new JSONObject(buffer.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        } finally {
            if (urlConnection != null) {
                urlConnection.disconnect();
            }
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

JSONTask implementation:

try {
        if ((new JSONTask().execute().get().toString()) != null) {
            ...
        }
     }...

Kumulos ResponseHandler with same error:

Kumulos.call("userinfo", params, new ResponseHandler(){

        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {

        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

        }
    });

I've looked on various posts here and on the internet and they're all either older posts which just seem to describe how to implement the response handler with the org.apache classes, or new ones saying that you simply have to use and import the cz.msebera classes now instead, and the rest of the implementation remains the same.

So my question is, am I missing something? Is there something wrong with my Android Studio version/update, something else I am required to do, or is this an actual issue now?

In the meantime, I've looked to implement my functionality using JSONAsyncTasks and a standard URL connection setup, which doesn't seem to be very efficient or fast, as my app hangs quite noticeably when retrieving and populating views with the data.

This has also presented a problem as I was hoping to make use of Kumulos' backend for my app, but their libraries also make use of ResponseHandlers and therefore this error is occurring there also, and I cannot change their libraries and use a different implementation this time.

Any help would be greatly appreciated on this as it's driving me a bit crazy.

If any more information is required to help diagnose a possible problem, please go ahead and mention it.

Aucun commentaire:

Enregistrer un commentaire