我正在使用volley
库进行网络通话。对于这样一个电话,我得到了:
com.android.volley.NoConnectionError:java.io.EOFException
以下是我的代码:
public void loadSearchList(final String mName){
String url;
int mainval;
if(product.getMain()==1){
mainval=2;
}
else{
mainval=1;
}
URL = Constants.productListUrlStr +"" +" _supplier" +" =&#34 + + globalProvider.contract._supplier"&安培; &#34 +#34;主&#34 +#34; =&#34 + mainval +"&安培;&#34 +#34;名称&#34 +#34; =&#34 + MNAME;
Map<String,String> params=new HashMap<>();
params.put("_supplier",globalProvider.contract._supplier);
params.put("name",mName);
params.put("main",String.valueOf(mainval));
Log.d("checksearchurl",url);
Utf8JsonRequest utf8JsonRequest=new Utf8JsonRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("checkresponse",response);
parseSearchList(response,mName);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("checeee",error.toString());
}
}
)
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Authorization", Constants.getToken(ProductDetailActivity.this));
headers.put("language","english");
// headers=globalProvider.addHeaderToken(ProductDetailActivity.this);
// Log.d("checklangheader",headers.get("Language"));
//headers=globalProvider.addHeaderToken(getActivity());
return headers;
}
@Override
public Request.Priority getPriority() {
Request.Priority mPriority = Request.Priority.HIGH;
return mPriority;
}
};
globalProvider.addRequest(utf8JsonRequest);
}
This is my Utf8JsonRequest:
public class Utf8JsonRequest extends StringRequest {
private Priority priority;
private Response.Listener<String> listener;
public Utf8JsonRequest(int method, String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(method, url,listener, errorListener);
this.listener=listener;
}
public Utf8JsonRequest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
super(url, listener, errorListener);
this.listener=listener;
}
@Override
protected Response<String> parseNetworkResponse (NetworkResponse response) {
try {
String utf8String = new String(response.data, "UTF-8");
return Response.success(new String(utf8String), HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
// log error
return Response.error(new ParseError(e));
}
}
@Override
protected void deliverResponse(String response) {
// TODO Auto-generated method stub
listener.onResponse(response);
}
public void setPriority(Priority priority) {
this.priority = priority;
}
@Override
public Priority getPriority() {
return priority;
}
}
我必须构建url:
this:url=Constants.productListUrlStr+"?"+"_supplier"+"="+
globalProvider.contract._supplier+"&"+"main"+"="+mainval+"&"+"name"+"="+mName
如果我通过params而不是代码400 error
。
我正在忏悔选择凌空而非改装的决定。
答案 0 :(得分:0)
经过几个小时的挣扎,发现了这个问题。 问题是与url.If我想搜索“紫色甘薯”,它抛出了上述错误。 所以我构建了这样的网址:
URL = Constants.productListUrlStr + “?” + “_供应商” + “=” + globalProvider.contract._supplier + “&安培;” + “主” + “=” + mainval + “&安培;” + “名称” + “=” + MNAME
String requestURL = String.format(url +“&amp;”+“name =%s”,Uri.encode(mName)); 现在它工作正常。