• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps ( Volley > sending GET Request with parameters ) How to flatten Map<String,String> parameters correc

yeln

Lurker
I got this custom volley request class that extends Request with the request parameters in Map<String,String> format, my question is how to flatten Map<String,String> parameters correctly into a query string and receive it with PHP $_GET?

public class GsonGetRequest_Exp5<T> extends Request<T> {

private Response.Listener<T> listener;
private Map<String, String> params;
private Type type;
private Gson gson;

public GsonGetRequest_Exp5(String url, Map<String, String> params, Type type, Gson gson,
Response.Listener<T> reponseListener, Response.ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.type = type;
this.gson = gson;
this.listener = reponseListener;
this.params = params;
}

public GsonGetRequest_Exp5(int method, String url, Map<String, String> params, Type type, Gson gson,
Response.Listener<T> reponseListener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.type = type;
this.gson = gson;
this.listener = reponseListener;
this.params = params;
}

@override
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
return params;
};

@override
protected void deliverResponse(T response) {
listener.onResponse(response);
}

@override
protected Response<T> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return (Response<T>) Response.success
(
gson.fromJson(jsonString, type),
HttpHeaderParser.parseCacheHeaders(response)
);
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JsonSyntaxException je) {
return Response.error(new ParseError(je));
}
}

}

public static GsonGetRequest_Exp5<ArrayList<FeedObject>> gsonGetRequest_exp6
(
Response.Listener<ArrayList<FeedObject>> listener,
Response.ErrorListener errorListener,
String str
)
{
//final String url = "http://example.com/php.php";

final Map<String, String> params = new HashMap<String, String>();
final Gson gson = new GsonBuilder()
.registerTypeAdapter(FeedObject.class, new FeedObjectDeserializer())
.create();


String test_url = "http://example.com/php.php";

params.put("SearchKey", str);




StringBuilder builder = new StringBuilder();

for (String key : params.keySet())
{
Object value = params.get(key);
if (value != null)
{
try
{
value = URLEncoder.encode(String.valueOf(value), HTTP.UTF_8);


if (builder.length() > 0)
builder.append("&");
builder.append(key).append("=").append(value);
}
catch (UnsupportedEncodingException e)
{
}
}
}

test_url += "?" + builder.toString();




return new GsonGetRequest_Exp5
(
test_url,
params,
new TypeToken<ArrayList<FeedObject>>() {}.getType(),
gson,
listener,
errorListener
);
}


my problem is the "params" I keep getting null, and nothing appended to the end of example.com
 
It appears that nothing is wrong with the above code, the cause of the problem is from somewhere else, so the above code is actually correct
 
Back
Top Bottom