Java 在截击中未获得StringRequest响应

Java 在截击中未获得StringRequest响应,java,android,json,android-volley,Java,Android,Json,Android Volley,我把这种依赖性 要使用截取并从该受尊重的URl获取JSON数据,请执行以下操作:- **compile 'me.neavo:volley:2014.12.09'** 放了这个之后 未获取StringRequest响应:- 如果我使用其他URL,它可以正常工作,但是这个URL不会给我响应 import android.app.Activity; import android.os.Bundle; import android.util.Log; import com.android.volley

我把这种依赖性 要使用截取并从该受尊重的URl获取JSON数据,请执行以下操作:-

**compile 'me.neavo:volley:2014.12.09'**
放了这个之后 未获取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);
        }
}
导入android.app.Activity;
导入android.os.Bundle;
导入android.util.Log;
导入com.android.volley.Request;
导入com.android.volley.RequestQueue;
导入com.android.volley.Response;
导入com.android.volley.VolleyError;
导入com.android.volley.toolbox.StringRequest;
导入com.android.volley.toolbox.volley;
公开课截击测试扩展活动{
//我制作Json数据的URL
公共静态最终字符串MainUrl=”http://mycricket.net23.net/abcd.php";
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Log.d(“方法”,“获取数据”);
RequestQueue RequestQueue=Volley.newRequestQueue(this);
StringRequest=newStringRequest(request.Method.GET、MainUrl、new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
//显示JSON数据的日志
Log.d(“ResponseVolley”,response);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
});
//…添加请求
添加(请求);
}
}

您正在方法上下文中创建一个新的
RequestQueue
,但一旦方法返回,请求队列可能会被垃圾收集


尝试使
RequestQueue
对象成为活动的成员,并仅在它仍然为null时创建它。

您正在方法上下文中创建一个新的
RequestQueue
,但一旦方法返回,请求队列可能会被垃圾收集


尝试将
RequestQueue
对象作为活动的成员,并仅在它仍然为空时创建它。

php的响应是JSON。它以{not with[开始,所以它是一个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");


}
编辑:

我忘了告诉您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;
}

 }
但是,您还必须在清单
标记
android:name=“.MyApplication”

看起来像这样:

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@drawable/app_ico"
    android:label="@string/app_name"

php的响应是JSON。它以{not with[开头,所以它是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");


}
编辑:

我忘了告诉您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;
}

 }
但是,您还必须在清单
标记
android:name=“.MyApplication”

看起来像这样:

<application
    android:name=".MyApplication"
    android:allowBackup="true"
    android:icon="@drawable/app_ico"
    android:label="@string/app_name"

您的URL可能有问题


请检查另一个URL并重试。

您的URL可能有问题



请检查另一个URL,然后重试。

尝试将响应转换为JsonObject并打印。可能会起作用吗?您还没有告诉我们如果您这样做会出现什么错误?如果它与其他链接一起工作,那么我们应该查看您的php文件……此外,您为什么不使用谷歌存储库中的截图?git clone获得此版本,请转到Android Studio>新建>导入模块并给出下载文件夹的路径。然后在app build.gradle中添加编译项目(“:volley”)对于Dependencies,我没有收到响应,…如果我放置其他URL,一切都很好。尝试将响应转换为JsonObject并打印。可能会起作用。如果您这样做,您还没有告诉我们您会遇到什么错误?如果它与其他链接一起工作,那么我们应该查看您的php文件…另外,您为什么不使用google reposit中的截击呢ory?git clone获取此版本,转到Android Studio>新建>导入模块并给出下载文件夹的路径。然后在app build.gradle中添加编译项目(“:volley”)对于Dependencies,我没有收到响应,…如果我放置其他URL,一切都会正常工作。很抱歉,我没有收到您的响应。我必须更改什么?移动此行:“RequestQueue RequestQueue=Volley.newRequestQueue(此);'位于声明MainUrl所在的行下,即使requestQueue成为活动的成员,而不是方法中的变量。很抱歉,我没有得到您的消息。我必须更改什么?移动此行:'requestQueue requestQueue=Volley.newRequestQueue(此)像你以前一样,我也尝试过处理JSON,但有时在获得响应时会出现延迟。使用这些单独的响应侦听器我没有问题。现在如果你对JSON不熟悉,尽管在我为VolleySingleton创建新类时非常容易,但请阅读一些关于如何处理JSON的内容…mRequestQue=Volley.newRequestQueue(MyApplication.getAppContext());get error here“MyApplication.getAppContext()”很抱歉,忘记了这个类,看看我的答案,我添加了infoIt,它与您的实现有点不同,但Singleton模式是一个很好的实践,您创建的响应侦听器与