How to parse XML return from OkHttp?
Asked Answered
P

1

8

This is my OkHttp Post Form Parameter Method using OkHttp's Async Get

public Call postGetCountries(Callback callback) {
    RequestBody body = new FormEncodingBuilder()
        .add("op", "op")
        .build();

    Log.d(TAG_PARAMS, "op=sgetcountrylist, app_type=1");

    Request request = new Request.Builder()
        .url(GATEWAY_URL)
        .post(body)
        .build();

    Call call = CLIENT.newCall(request);
    call.enqueue(callback);

    return call;
}

This is my custom Callback.

private class GetCountriesCallback implements Callback {
    @Override
    public void onFailure(Request request, IOException e) {
        Log.e("OkHttp", e.getMessage());
    }

    @Override
    public void onResponse(Response response) throws IOException {
        Log.d("PASSED", "PASS");
        Log.d(Connection.TAG_RETURN, response.body().string());

        try {
            InputStream is = response.body().byteStream();
            List test = connectionParser.parse(is, "op");

        } catch (XmlPullParserException e) {
            Log.e("PARSE ERROR", e.getMessage());
        }
    }
}

This is my instantiated parse method.

public List parse(InputStream in, String op) throws XmlPullParserException, IOException {
    try {
        XmlPullParser parser = Xml.newPullParser();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in, null);
        parser.nextTag();
        return readFeed(parser, op);
    } finally {
        in.close();
    }
}

I'm currently testing if it works unfortunately I receive a return of

10-06 11:54:42.492 6336-6892/ D/PASSED: PASS
10-06 11:54:42.692 6336-6892/ E/PARSE ERROR: Invalid stream or encoding: java.io.IOException: closed (position:START_DOCUMENT null@1:1) caused by: java.io.IOException: closed

This is what I use on my onCreate on the activity to start the whole process:

private Connection connect = Connection.getInstance();
connect.postGetCountries(new GetCountriesCallback());

I don't understand as to why the InputStream gets closed.

Pittance answered 6/10, 2015 at 4:0 Comment(0)
H
7

Two things could be going on. First, you can only read the body once. If you want to read it more than once, you need to store the result somewhere. You are reading the body twice, once here --

Log.d(Connection.TAG_RETURN, response.body().string());

and then here --

InputStream is = response.body().byteStream();
List test = connectionParser.parse(is, "op");

by the time you start to parse, you have already exhausted the available input in the stream. The quick solution is to remove the log statement.

Another thing that might be tripping you up, or could trip you up in the future is onResponse is called even in the event of HTTP returning an error code. You should check the Response's code() or isSuccesful() methods to decide if you should even attempt to parse the response.

Harbinger answered 6/10, 2015 at 4:39 Comment(1)
This returns the xml with < and > as &lt; and &gt; how to solve this ?Adeliaadelice

© 2022 - 2024 — McMap. All rights reserved.