JsonObjectRequest和RequestQueue错误

问题描述 投票:0回答:1

解决方案:我的问题在于上下文,以防其他人遇到同样的问题。

我试图在我的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) 任何形式的帮助将非常感激
android json android-volley jsonobjectrequest
1个回答
0
投票

as per my above comment

contextnull

初始化你的qazxsw我喜欢这个

context

要么

Context context=YourActivity.this; 
© www.soinside.com 2019 - 2024. All rights reserved.