在VOLLEY

时间:2016-03-08 11:37:18

标签: java android json android-volley

我有这种依赖性 使用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);
        }
}

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

}

然后在你的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"

答案 2 :(得分:0)

可能有问题。用你的网址。

请检查其他网址,然后重试。