gpt4 book ai didi

java - 为扩展 JsonRequest 的类创建构造函数

转载 作者:太空宇宙 更新时间:2023-11-04 11:37:07 25 4
gpt4 key购买 nike

我在 android studio 中创建了一个扩展 JsonRequest 的类但是 android studio 给了我一个错误,并希望我创建一个构造函数,因为库(volley 库)中没有默认的构造函数。实际上我正在使用 volley 库来解析 android 中 ListView 中的 json 文件。因为 json 都是波斯语文本,所以我创建了这个类,将其更改为 utf-8 编码。请告诉我所需的构造函数应该是什么样子。我尝试过一些但不正确。

public class Utf8JsonRequest extends JsonRequest<JSONObject> {
...
@Override
protected Response<JSONObject> parseNetworkResponse (NetworkResponse response) {
try {
String utf8String = new String(response.data, "UTF-8");
return Response.success(new JSONObject(utf8String), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
// log error
return Response.error(new ParseError(e));
} catch (JSONException e) {
// log error
return Response.error(new ParseError(e));
}
}
}

最佳答案

这是构造函数的示例:

// the constructor should have at least these parameters
// to be passed onto super()
public Utf8JsonRequest(int method,
String url,
String requestBody,
Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener) {
super(method, url, requestBody, listener, errorListener);

// you can initialize other stuff if you wish
}

关于java - 为扩展 JsonRequest<JSONObject> 的类创建构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43163200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com