Android Volley 对 JsonObjectRequest 的引用不明确
Android Volley ambiguous reference to JsonObjectRequest
我正在尝试对网络服务使用简单的 Volley
JsonObjectRequest,但出现此错误:
error: reference to JsonObjectRequest is ambiguous, both constructor JsonObjectRequest(int,String,String,Listener<JSONObject>,ErrorListener) in JsonObjectRequest and constructor JsonObjectRequest(int,String,JSONObject,Listener<JSONObject>,ErrorListener) in JsonObjectRequest match
我正在从 Gradle
安装 volley,这是我在项目中使用的简单代码。
public class Simple_JsonRequest extends Activity {
private TextView mTvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act__json_request);
mTvResult = (TextView) findViewById(R.id.tv_result);
Button btnJsonRequest = (Button) findViewById(R.id.btn_json_request);
btnJsonRequest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
RequestQueue queue = MyVolley.getRequestQueue();
JsonObjectRequest myReq = new JsonObjectRequest(Request.Method.GET,
"http://echo.jsontest.com/key/value/one/two",
null,
createMyReqSuccessListener(),
createMyReqErrorListener());
queue.add(myReq);
}
});
}
private Response.Listener<JSONObject> createMyReqSuccessListener() {
return new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
mTvResult.setText(response.getString("one"));
} catch (JSONException e) {
mTvResult.setText("Parse error");
}
}
};
}
private Response.ErrorListener createMyReqErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTvResult.setText(error.getMessage());
}
};
}
}
因为 JsonObjectRequest 有两个构造函数,
一个是
JsonObjectRequest(int,String,JSONObject,Listener<JSONObject>,ErrorListener)
另一个是
JsonObjectRequest(int,String,String,Listener<JSONObject>,ErrorListener)
,所以如果你把第三个参数设置为null,编译器就不知道你要使用哪个构造函数,所以才会出现这样的错误。
要解决您的问题,您只需将第三个参数替换为new JSONObject()
或""
即可。
你如何替换第 3 个参数的 null
,用更具体的东西然后试试?
我正在尝试对网络服务使用简单的 Volley
JsonObjectRequest,但出现此错误:
error: reference to JsonObjectRequest is ambiguous, both constructor JsonObjectRequest(int,String,String,Listener<JSONObject>,ErrorListener) in JsonObjectRequest and constructor JsonObjectRequest(int,String,JSONObject,Listener<JSONObject>,ErrorListener) in JsonObjectRequest match
我正在从 Gradle
安装 volley,这是我在项目中使用的简单代码。
public class Simple_JsonRequest extends Activity {
private TextView mTvResult;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act__json_request);
mTvResult = (TextView) findViewById(R.id.tv_result);
Button btnJsonRequest = (Button) findViewById(R.id.btn_json_request);
btnJsonRequest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
RequestQueue queue = MyVolley.getRequestQueue();
JsonObjectRequest myReq = new JsonObjectRequest(Request.Method.GET,
"http://echo.jsontest.com/key/value/one/two",
null,
createMyReqSuccessListener(),
createMyReqErrorListener());
queue.add(myReq);
}
});
}
private Response.Listener<JSONObject> createMyReqSuccessListener() {
return new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
mTvResult.setText(response.getString("one"));
} catch (JSONException e) {
mTvResult.setText("Parse error");
}
}
};
}
private Response.ErrorListener createMyReqErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTvResult.setText(error.getMessage());
}
};
}
}
因为 JsonObjectRequest 有两个构造函数,
一个是
JsonObjectRequest(int,String,JSONObject,Listener<JSONObject>,ErrorListener)
另一个是
JsonObjectRequest(int,String,String,Listener<JSONObject>,ErrorListener)
,所以如果你把第三个参数设置为null,编译器就不知道你要使用哪个构造函数,所以才会出现这样的错误。
要解决您的问题,您只需将第三个参数替换为new JSONObject()
或""
即可。
你如何替换第 3 个参数的 null
,用更具体的东西然后试试?