没有得到在VOLLEY
StringRequest回应,我已经把这个依赖 使用凌空并获得JSON数据从一个尊重网址: - 把后没有得到在VOLLEY
**compile 'me.neavo:volley:2014.12.09'**
这 没有得到StringRequest响应: -
如果我使用其他URLS其工作正常,但与此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
对象成为您的活动的成员,并仅在其仍然为空时创建该对象。
对不起,我没有得到你。我必须改变什么? –
一切正常,如果我把其他网址。 –
移动此行:'RequestQueue requestQueue = Volley.newRequestQueue(this);'在您声明MainUrl的行的下面,即使requestQueue成为该活动的成员,而不是该方法中的变量。 –
您的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;
}
}
然后在片段/活动创建下载方法,你只让凌空请求,并将其添加到队列:
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>
标签声明本
是这样的:
<application
android:name=".MyApplication"
android:allowBackup="true"
android:icon="@drawable/app_ico"
android:label="@string/app_name"
ohk让我试试 –
它与你的实现有点不同,但Singleton模式是一个好习惯,你创建的响应监听器可以让代码更清晰,避免出现问题。我的回复处理方式与以前一样,但有时会出现延迟,无法获得回复。使用这些单独的回应监听器,我没有任何问题。现在,如果您是JSON的新手,请阅读关于如何处理JSON的一些信息,虽然这很容易 – iBobb
当我为VolleySingleton创建新类时... mRequestQue = Volley.newRequestQueue(MyApplication.getAppContext()); 在这里得到错误“MyApplication.getAppContext()” –
可能出现概率。与您的网址。
请检查另一个URL并重试。
尝试将响应转换为JsonObject和print.may是否可以工作 –
如果您完全不知道您还没有告诉我们您会得到什么错误?如果它与其他链接一起工作,那么我们应该看看你的PHP文件...另外,为什么你不使用谷歌存储库中的Volley? git clone https://android.googlesource.com/platform/frameworks/volley获取此版本,请转至Android Studio>新建>导入模块,并为下载的文件夹提供路径。然后在应用程序build.gradle添加编译项目(':volley')的依赖 – iBobb
我没有得到回应,..多数民众赞成它 –