我在此行的括号内收到一个名为expression的错误。 Volley.newRequestQueue(MainActivity);
。此类在我的MainActivity之外的其他活动上。以下是我的代码片段:
public void youFunctionForVolleyRequest(final ServerCallbackJava callback) {
RequestQueue queue = Volley.newRequestQueue(MainActivity);
String url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=22.2913,113.947&destinations=WanChai&mode=driving&key="REMOVED";
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, null, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
callback.onSuccess(response); // call call back function here
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//VolleyLog.d("Volley error json object ", "Error: " + error.getMessage());
}
})
{
@Override
public String getBodyContentType ()
{
return "application/json";
}
};
// Adding request to request queue
queue.add(jsonObjReq);
}
}
答案 0 :(得分:1)
由于您在活动/片段中创建了一个类,因此在调用该类时,您必须将活动的上下文传递给此函数,并替换 RequestQueue queue = Volley.newRequestQueue(MainActivity); < / strong>使用 RequestQueue queue = Volley.newRequestQueue(mContext); 在您的类中声明的上下文。
答案 1 :(得分:0)
在VolleyRequests类中创建全局变量
Context context;
并在构造函数
上初始化它public VolleyRequests(Context mcontext) { this.context= mcontext; }
然后使用此
RequestQueue queue = Volley.newRequestQueue(context);
在MainActivity中调用它时使用:
VolleyRequests volleyrequests = new VolleyRequests (this);
答案 2 :(得分:0)
如果您的代码位于Activity.class
:
RequestQueue queue = Volley.newRequestQueue(this);//or
RequestQueue queue = Volley.newRequestQueue(Activity.this);//or
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
如果您的代码位于Fragment.class
:
RequestQueue queue = Volley.newRequestQueue(getContext());//or
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());//or
RequestQueue queue = Volley.newRequestQueue(Fragment.this.getContext());//or
RequestQueue queue = Volley.newRequestQueue(Fragment.this.getApplicationContext());
答案 3 :(得分:0)
您可以借助以下代码段来完成此操作:
public class Main2Activity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button button = (Button)findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
request r = new request(Main2Activity.this);
r.youFunctionForVolleyRequest();
}
});
}
class request{
Context ctx;
public request(Context mContext) {
ctx = mContext;
}
public void youFunctionForVolleyRequest() {
RequestQueue queue = Volley.newRequestQueue(ctx);
String url = "https://maps.googleapis.com/maps/api/distancematrix/json?units=metric&origins=22.2913,113.947&destinations=WanChai&mode=driving&key=";
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, null, new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response) {
// callback.onSuccess(response); // call call back function here
System.out.println("success");
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
//VolleyLog.d("Volley error json object ", "Error: " + error.getMessage());
System.out.println("failed");
}
})
{
@Override
public String getBodyContentType ()
{
return "application/json";
}
};
// Adding request to request queue
queue.add(jsonObjReq);
}
}
}
答案 4 :(得分:0)
在新的类构造函数中,传递上下文,以便在volley中使用它。
public class ClassWithVolley{
private Context context;
public ClassWithVolley(Context context){
this.context = context;
}
// continue