2016-03-08 41 views
1

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); 
     } 
} 
+0

嘗試將響應轉換爲JsonObject和print.may是否可以工作 –

+0

如果您完全不知道您還沒有告訴我們您會得到什麼錯誤?如果它與其他鏈接一起工作,那麼我們應該看看你的PHP文件...另外,爲什麼你不使用谷歌存儲庫中的Volley? git clone https://android.googlesource.com/platform/frameworks/volley獲取此版本,請轉至Android Studio>新建>導入模塊,併爲下載的文件夾提供路徑。然後在應用程序build.gradle添加編譯項目(':volley')的依賴 – iBobb

+0

我沒有得到迴應,..多數民衆贊成它 –

回答

0

你正在創建的方法上下文中的新RequestQueue,但很有可能,一旦該方法返回,請求隊列被垃圾收集。

嘗試使RequestQueue對象成爲您的活動的成員,並僅在其仍然爲空時創建該對象。

+0

對不起,我沒有得到你。我必須改變什麼? –

+0

一切正常,如果我把其他網址。 –

+0

移動此行:'RequestQueue requestQueue = Volley.newRequestQueue(this);'在您聲明MainUrl的行的下面,即使requestQueue成爲該活動的成員,而不是該方法中的變量。 –

0

您的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" 
+0

ohk讓我試試 –

+0

它與你的實現有點不同,但Singleton模式是一個好習慣,你創建的響應監聽器可以讓代碼更清晰,避免出現問題。我的回覆處理方式與以前一樣,但有時會出現延遲,無法獲得回覆。使用這些單獨的迴應監聽器,我沒有任何問題。現在,如果您是JSON的新手,請閱讀關於如何處理JSON的一些信息,雖然這很容易 – iBobb

+0

當我爲VolleySingleton創建新類時... mRequestQue = Volley.newRequestQueue(MyApplication.getAppContext()); 在這裏得到錯誤「MyApplication.getAppContext()」 –

0

可能出現概率。與您的網址。

請檢查另一個URL並重試。