[英]Android Volley - Getting No Response using StringRequest to XML API
[英]Not Getting StringRequest Response in VOLLEY
我已经把这种依赖关系用于排球并从受人尊敬的URl获取JSON数据:-
**compile 'me.neavo:volley:2014.12.09'**
在放置此Not Getting StringRequest响应后:-
如果我使用其他URL,则可以正常工作,但是此URL没有给我响应。
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
public class VolleyTest extends Activity {
//MY URL where i had made Json data
public static final String MainUrl = "http://mycricket.net23.net/abcd.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Log.d("method", "GETDATA");
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.GET, MainUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// SHow LOG of JSON data
Log.d("ResponseVolley",response);
}
},new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//.. Adding Request
requestQueue.add(request);
}
}
您正在方法上下文中创建一个新的RequestQueue
,但是一旦方法返回,就有可能对请求队列进行垃圾回收。
尝试使RequestQueue
对象成为您的活动的成员,并仅在它仍然为null时创建它。
您的php的响应是JSON。 它以{开头,而不是以[开头,因此它是一个JSONObject而不是JSONArray,所以...
只需遵循Singleton模式,创建一个类似的类:
public class VolleySingleton {
private static VolleySingleton sInstance = null;
private RequestQueue mRequestQue;
private VolleySingleton(){
mRequestQue = Volley.newRequestQueue(MyApplication.getAppContext());
}
public static VolleySingleton getInstance() {
if (sInstance == null) {
sInstance = new VolleySingleton();
}
return sInstance;
}
public RequestQueue getRequestQueue() {
return mRequestQue;
}
}
然后在Fragment / Activity中创建您的下载方法,您只需在其中提出Volley请求并将其添加到队列中:
public void downloadMethod(String urlService, Response.Listener<JSONObject> successListener, Response.ErrorListener errorListener) {
//final List<Event> myEventsList = eventsList;
RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue(); // this is where we use the Singleton
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, urlService, null, successListener, errorListener);
requestQueue.add(jsonObjectRequest);
Log.d("VolleyDebug", "REQUEST QUE ADDED SUCCESSFULLY");
}
现在,创建2个响应侦听器,您将在其中实际处理响应:
private Response.Listener<JSONObject > createMyReqSuccessListener() {
return new Response.Listener<JSONObject >() {
@Override
public void onResponse(JSONObject response) {
//handle JSON here
}
};
}
private Response.ErrorListener createMyReqErrorListener() {
return new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MyApplication.getAppContext(), "ERROR:" + error.getMessage(), Toast.LENGTH_SHORT).show();
}
};
}
现在使用该方法:
downloadMethod(yourURL, createMyReqSuccessListener(), createMyReqErrorListener());
编辑:
我忘了告诉您有关MyApplication类的信息,它在获取应用程序周围的上下文时非常有用,因此您也可以创建它:
public class MyApplication extends Application {
private static Context context;
//private static MyApplication sInstance;
public void onCreate() {
super.onCreate();
MyApplication.context = getApplicationContext();
}
public static Context getAppContext() {
return MyApplication.context;
}
}
但是,您还必须在清单<application>
标记android:name=".MyApplication"
声明此内容
看起来像这样:
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@drawable/app_ico"
android:label="@string/app_name"
可能有概率。 与您的网址。
请检查其他网址,然后重试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.