【问题标题】:Not Getting StringRequest Response in VOLLEY在 VOLLEY 中没有得到 StringRequest 响应
【发布时间】:2016-06-22 08:03:45
【问题描述】:

我已经把这个依赖 使用 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);
        }
}

【问题讨论】:

  • 尝试将响应转换为 JsonObject 和 print。也许它会起作用
  • 您还没有告诉我们如果您这样做了会出现什么错误?如果它与其他链接一起工作,那么我们应该看看你的 php 文件......另外,你为什么不使用谷歌存储库中的 Volley 呢? git clone android.googlesource.com/platform/frameworks/volley 获取此版本,转到 Android Studio>New>Import 模块并给出下载文件夹的路径。然后在 app build.gradle 中添加 compile project(':volley') 到依赖项
  • 我没有得到回应,..就是这样
  • 如果我输入其他 URL,一切正常。

标签: java android json android-volley


【解决方案1】:

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

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

【讨论】:

  • 对不起,我没有得到你。我必须改变什么?
  • 移动这一行:'RequestQueue requestQueue = Volley.newRequestQueue(this);'在您声明 MainUrl 的行下,即,使 requestQueue 成为活动的成员,而不是方法中的变量。
【解决方案2】:

您的 php 的响应是 JSON。它以 { 开头而不是 [ 所以它是 JSONObject 而不是 JSONArray 所以...

只要按照单例模式,创建一个这样的类:

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;
}

}

然后在您的片段/活动中创建您的下载方法,您只需在其中发出 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;
}

 }

但是,您还必须在清单中声明这一点&lt;application&gt; 标签android:name=".MyApplication"

看起来像这样:

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

【讨论】:

  • 这与您的实现有点不同,但单例模式是一种很好的做法,您创建的响应侦听器可以使代码更清晰,并避免出现问题。我像以前一样处理了响应,但有时在获得响应时会出现延迟。使用那些单独的响应侦听器我没有问题。现在,如果您是 JSON 新手,请阅读一下如何处理 JSON,尽管它非常简单
  • 当我为 VolleySingleton 创建新类时... mRequestQue = Volley.newRequestQueue(MyApplication.getAppContext());在这里得到错误“MyApplication.getAppContext()”
  • 不好意思,忘了这门课,看我的回答,我加了信息
【解决方案3】:

可能有问题。使用您的网址。

请检查另一个网址并重试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 2018-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-12
    • 1970-01-01
    相关资源
    最近更新 更多