JsonObjectRequest 和 RequestQueue 错误
JsonObjectRequest and RequestQueue error
解决方案:我的问题是上下文,以防其他人遇到同样的问题。
我正在尝试在我的 android 应用中使用 JsonObjectRequest 和 RequestQueue 使用 Volley lib 发出请求,但我不断收到错误消息。
首先,我尝试仅使用 JsonObjectRequest(不使用 RequestQueue),但没有获得任何数据,因此我尝试使用 RequestQueue。
这是我的代码:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, urlMarket, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray result = response.getJSONArray("result");
//we loop the response
for (int i = 0; i < result.length(); i++) {
coin.setHigh(Double.parseDouble(result.getJSONObject(i).getString("High")));
coin.setLow(Double.parseDouble(result.getJSONObject(i).getString("Low")));
coin.setLast(Double.parseDouble(result.getJSONObject(i).getString("Last")));
coin.setVolInBtc(Double.parseDouble(result.getJSONObject(i).getString("BaseVolume")));
coin.setBid(Double.parseDouble(result.getJSONObject(i).getString("Bid")));
coin.setAsk(Double.parseDouble(result.getJSONObject(i).getString("Ask")));
coin.setPrevDay(result.getJSONObject(i).getString("PrevDay"));
}
} catch (Exception e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
requestQueue.add(request);
这是我初始化requestQueue的方式:
requestQueue=Volley.newRequestQueue(context);
这是我得到的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{space.bewa.bewa_v10/space.bewa.bewa_v10.activities.DashboardActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir()' on a null object reference
at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:43)
at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:78)
如有任何帮助,我们将不胜感激
你context
是null
像这样初始化你的context
Context context=YourActivity.this;
或
Context context=getApplicationContext();
解决方案:我的问题是上下文,以防其他人遇到同样的问题。
我正在尝试在我的 android 应用中使用 JsonObjectRequest 和 RequestQueue 使用 Volley lib 发出请求,但我不断收到错误消息。
首先,我尝试仅使用 JsonObjectRequest(不使用 RequestQueue),但没有获得任何数据,因此我尝试使用 RequestQueue。
这是我的代码:
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, urlMarket, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { try { JSONArray result = response.getJSONArray("result"); //we loop the response for (int i = 0; i < result.length(); i++) { coin.setHigh(Double.parseDouble(result.getJSONObject(i).getString("High"))); coin.setLow(Double.parseDouble(result.getJSONObject(i).getString("Low"))); coin.setLast(Double.parseDouble(result.getJSONObject(i).getString("Last"))); coin.setVolInBtc(Double.parseDouble(result.getJSONObject(i).getString("BaseVolume"))); coin.setBid(Double.parseDouble(result.getJSONObject(i).getString("Bid"))); coin.setAsk(Double.parseDouble(result.getJSONObject(i).getString("Ask"))); coin.setPrevDay(result.getJSONObject(i).getString("PrevDay")); } } catch (Exception e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { error.printStackTrace(); } }); requestQueue.add(request);
这是我初始化requestQueue的方式:
requestQueue=Volley.newRequestQueue(context);
这是我得到的错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{space.bewa.bewa_v10/space.bewa.bewa_v10.activities.DashboardActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir()' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.File android.content.Context.getCacheDir()' on a null object reference at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:43) at com.android.volley.toolbox.Volley.newRequestQueue(Volley.java:78)
如有任何帮助,我们将不胜感激
你context
是null
像这样初始化你的context
Context context=YourActivity.this;
或
Context context=getApplicationContext();