Android截击错误D/截击:[486]基本网络.logSlowRequests:
我正在尝试使用Volley获取JSON对象以创建动态自动完成,但在使用Volley请求时,我在日志cat中看到: D/Volley:[486]BasicNetwork.logSlowRequests:HTTP响应 请求=[lifetime=5648],[size=363753],[rc=200], [retryCount=0] 这些并没有解决我的问题 下面是初始化请求队列的Singleton类:Android截击错误D/截击:[486]基本网络.logSlowRequests:,android,android-volley,Android,Android Volley,我正在尝试使用Volley获取JSON对象以创建动态自动完成,但在使用Volley请求时,我在日志cat中看到: D/Volley:[486]BasicNetwork.logSlowRequests:HTTP响应 请求=[lifetime=5648],[size=363753],[rc=200], [retryCount=0] 这些并没有解决我的问题 下面是初始化请求队列的Singleton类: import android.content.Context; import com.androi
import android.content.Context;
import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
public class SingleTon {
private Context context;
private RequestQueue requestQueue;
public static SingleTon singleTon;
public SingleTon(Context context){
this.context=context;
requestQueue=getRequestQueue();
}
private RequestQueue getRequestQueue(){
if(requestQueue==null){
requestQueue= Volley.newRequestQueue(context);
}
return requestQueue;
}
public synchronized static SingleTon getInstance(Context context){
if(singleTon==null){
singleTon=new SingleTon(context.getApplicationContext());
}
return singleTon;
}
public void addToRequestQueue(Request request){
requestQueue.add(request).setRetryPolicy(new DefaultRetryPolicy([I have tried all possible combinations here]));
}
}
发出请求的方法:
public Filter getFilter() {
Filter filter=new Filter() {
@Override
protected FilterResults performFiltering(CharSequence charSequence) {
FilterResults filterResults=new FilterResults();
if(charSequence!=null){
String partialName=charSequence.toString();
String url="https://api.railwayapi.com/v2/suggest-train/train/"+partialName+"/apikey/xnlo6zq3yj/";
HTTPConnector httpConnector=new HTTPConnector(url,context);
JSONObject response=httpConnector.getJsonResponse();
try {
JSONArray trains=response.getJSONArray("trains");
for(int i=0;i<trains.length();i++){
trainName.add(new Train(trains.getJSONObject(i).getString("number")));
}
} catch (JSONException e) {
e.printStackTrace();
}
filterResults.values=trainName;
filterResults.count=trainName.size();
}
return filterResults;
}
公共过滤器getFilter(){
过滤器过滤器=新过滤器(){
@凌驾
受保护过滤器结果执行过滤(CharSequence CharSequence){
FilterResults FilterResults=新的FilterResults();
if(charSequence!=null){
String partialName=charSequence.toString();
字符串url=”https://api.railwayapi.com/v2/suggest-train/train/“+partialName+”/apikey/xnlo6zq3yj/”;
HTTPConnector HTTPConnector=新的HTTPConnector(url,上下文);
JSONObject response=httpConnector.getJsonResponse();
试一试{
JSONArray trains=response.getJSONArray(“trains”);
对于(inti=0;i这是一种奇怪的使用Volley的方式。Volley会发出异步HTTP请求。您的这两行代码不会每次都给出结果
HTTPConnector httpConnector=new HTTPConnector(url,context);
JSONObject response=httpConnector.getJsonResponse();
您正在发送async..request并立即调用getJsonResponse
函数?在调用该函数时,您不能确定响应是否可用。当收到响应时,即OnResponse
方法中的JsonObjectRequest
因此,您应该正确地实现HTTPConnector
类和包含getFilter
方法的类之间的通信
您可以为此使用接口。在HTTPConnector
类中声明接口,并在活动中实现它
public class HTTPConnector {
String url;
JSONObject jsonResponse;
Context context;
IMyInterface mListener;
public interface IMyInterface
{
void sendResponse(JsonObject obj);
}
public HTTPConnector(String url,Context context,IMyInterface pListener){
this.url=url;
this.context=context;
mListener=pListener
}
public void makeQuery(){
JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
jsonResponse=response;
if(mListener!=null)
{
mListener.sendResponse(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context,"ERROR",Toast.LENGTH_SHORT).show();
Log.d("ERROR: ", error.toString());
if(mListener!=null)
{
mListener.sendResponse(null);
}
}
});
SingleTon.getInstance(context.getApplicationContext()).addToRequestQueue(request);
}
}
你有没有试过在网页浏览器中点击url,需要多少时间才能得到结果?我已经提到过它没有帮助。@duggu是的,我试过了。没花多少时间。谢谢。它有帮助。很高兴帮助你
public class HTTPConnector {
String url;
JSONObject jsonResponse;
Context context;
IMyInterface mListener;
public interface IMyInterface
{
void sendResponse(JsonObject obj);
}
public HTTPConnector(String url,Context context,IMyInterface pListener){
this.url=url;
this.context=context;
mListener=pListener
}
public void makeQuery(){
JsonObjectRequest request=new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
jsonResponse=response;
if(mListener!=null)
{
mListener.sendResponse(response);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context,"ERROR",Toast.LENGTH_SHORT).show();
Log.d("ERROR: ", error.toString());
if(mListener!=null)
{
mListener.sendResponse(null);
}
}
});
SingleTon.getInstance(context.getApplicationContext()).addToRequestQueue(request);
}
}
public class MainActivity extends AppCompatActivity implements HTTPConnector.IMyInterface
{
...........
...........
// Your other code of activity
...........
..........
@Override
public void sendResponse(JsonObject response)
{
try {
JSONArray trains=response.getJSONArray("trains");
for(int i=0;i<trains.length();i++){
trainName.add(new Train(trains.getJSONObject(i).getString("number")));
}
//Now you have response and filled the list, can call getFilter
} catch (JSONException e) {
e.printStackTrace();
}
}
}
String url="https://api.railwayapi.com/v2/suggest-train/train/"+partialName+"/apikey/xnlo6zq3yj/";
HTTPConnector httpConnector=new HTTPConnector(url,context,this); // this will act as your listener
httpConnector.makeQuery();