这是我使用 OkHttp 的 Async Get 的 OkHttp Post Form Parameter Method
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;
}
这是我的自定义回调。
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());
}
}
}
这是我实例化的解析方法。
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();
}
}
我目前正在测试它是否有效,不幸的是我收到了回报
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
这是我在活动上的 onCreate 上使用的来启动整个过程:
private Connection connect = Connection.getInstance();
connect.postGetCountries(new GetCountriesCallback());
我不明白为什么 InputStream 被关闭。