package com.monishn.android.volley;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
// json array response url
private String urlJsonArry = "www.shaoniiuc.com/my_json";
private static String TAG = MainActivity.class.getSimpleName();
private Button btnMakeObjectRequest, btnMakeArrayRequest;
// Progress dialog
private ProgressDialog pDialog;
private TextView txtResponse;
// temporary string to show the parsed response
private String jsonResponse;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnMakeArrayRequest = (Button) findViewById(R.id.btnArrayRequest);
txtResponse = (TextView) findViewById(R.id.txtResponse);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
btnMakeArrayRequest.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// making json array request
makeJsonArrayRequest();
}
});
}
/**
* Method to make json array request where response starts with [
* */
private void makeJsonArrayRequest() {
showpDialog();
JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,new Response.Listener<JSONArray>(){
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
// Parsing json array response
// loop through each json object
jsonResponse = "";
for (int i = 0; i < response.length(); i++) {
JSONObject person = (JSONObject) response.get(i);
String id = person.getString("id");
String name = person.getString("name");
jsonResponse += "Name: " + name + "\n\n";
jsonResponse += "Id: " + id + "\n\n";
}
txtResponse.setText(jsonResponse);
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(req);
}
private void showpDialog() {
if (!pDialog.isShowing())
pDialog.show();
}
private void hidepDialog() {
if (pDialog.isShowing())
pDialog.dismiss();
}
}
Logcat数据
08:59:19.981 27919-27919/com.monishn.android.volley E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.Context android.content.Context.getApplicationContext()' on a null object reference
at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:107)
at com.monishn.android.volley.AppController.getRequestQueue(AppController.java:33)
at com.monishn.android.volley.AppController.addToRequestQueue(AppController.java:46)
at com.monishn.android.volley.MainActivity.makeJsonArrayRequest(MainActivity.java:107)
at com.monishn.android.volley.MainActivity.access$000(MainActivity.java:23)
at com.monishn.android.volley.MainActivity$1.onClick(MainActivity.java:53)
at android.view.View.performClick(View.java:5207)
at android.view.View$PerformClick.run(View.java:21168)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-22 08:59:21.847 27919-27919/com.monishn.android.volley
答案 0 :(得分:2)
public class AppController extends Application {
protected static AppController sInstance;
private RequestQueue mRequestQueue;
@Override
public void onCreate() {
super.onCreate();
mRequestQueue = Volley.newRequestQueue(this);
sInstance = this;
}
public synchronized static AppController getInstance() {
return sInstance;
}
public RequestQueue getRequestQueue() {
return mRequestQueue;
}
}
在清单中(这一定是错误)
<application
android:name=".AppController"
android:allowBackup="true"
....
因为您正在AppController
并添加像这样的请求队列
AppController.getInstance().getRequestQueue().add(req)