Reputation: 2916
I am learning the Android with web API. So I am using the Volley. I found the tutorial from
http://www.androidhive.info/2014/09/android-json-parsing-using-volley/
https://developer.android.com/training/volley/index.html
Here I create the one class call as AppController.
package utils;
import utils.LruBitmapCache;
import android.app.Application;
import android.text.TextUtils;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
public class AppController extends Application {
public static final String TAG = AppController.class
.getSimpleName();
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public ImageLoader getImageLoader() {
getRequestQueue();
if (mImageLoader == null) {
mImageLoader = new ImageLoader(this.mRequestQueue,
new LruBitmapCache());
}
return this.mImageLoader;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
}
After that my button click event I create the JsonObjectRequest and I called this method.
AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
Then it is giving the error
java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick
So I used this method.
Volley.newRequestQueue(this).add(jsonObjReq);
Then it is working. I want to know why first method is not working. (AppController)
Edited
public void loginClick(View view){
EditText username = (EditText) findViewById(R.id.userName);
EditText password = (EditText) findViewById(R.id.password);
String tag_json_obj = "json_obj_req";
String url = "http://api.androidhive.info/volley/person_object.json";
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading...");
pDialog.show();
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET,
url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d("RESULT", response.toString());
pDialog.hide();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("RESULT", "Error: " + error.getMessage());
// hide the progress dialog
pDialog.hide();
}
});
Volley.newRequestQueue(this).add(jsonObjReq);
// AppController.getInstance().addToRequestQueue(jsonObjReq,tag_json_obj);
}
Upvotes: 1
Views: 5557
Reputation: 2916
I found the solution. I need to add "android:name="utils.AppController" "line in the AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name="utils.AppController"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Upvotes: 1