java - URL Encoding with Volley -


does android volley url encoding?

i trying request , on server side, seems url being sent through not encoded correctly.

i have been fiddling customrequest class not able working, if encoding.

customrequest.java:

package com.bidorbuy.app.network;  import com.android.volley.authfailureerror; import com.android.volley.networkresponse; import com.android.volley.parseerror; import com.android.volley.request;  import com.android.volley.response; import com.android.volley.toolbox.httpheaderparser;  import org.json.jsonexception; import org.json.jsonobject;  import java.io.unsupportedencodingexception; import java.net.urlencoder; import java.util.map;   public class customrequest extends request<jsonobject> {  private response.listener<jsonobject> listener; private map<string, string> params; private map<string, string> headers; priority priority;  public customrequest(string url,                      map<string, string> params,                      map<string, string> headers,                      response.listener<jsonobject> reponselistener,                      response.errorlistener errorlistener,                      priority priority) {     super(method.get, url, errorlistener);     this.listener = reponselistener;     this.params = params;     this.headers = headers;     this.priority = priority; }  public customrequest(int method,                      string url,                      map<string, string> params,                      map<string, string> headers,                      response.listener<jsonobject> reponselistener,                      response.errorlistener errorlistener,                      priority priority) {     super(method, url, errorlistener);     this.listener = reponselistener;     this.params = params;     this.headers = headers;     this.priority = priority; }  protected map<string, string> getparams()         throws com.android.volley.authfailureerror {     return params; };  @override public map<string, string> getheaders() throws authfailureerror {     return headers; }  @override public byte[] getbody() throws authfailureerror {     map<string, string> params = getparams();     if(params!=null && params.size() > 0){         return encodeparameters(params, getparamsencoding());     }     return null; }  private byte[] encodeparameters(map<string, string> params, string paramsencoding) {     stringbuilder encodedparams = new stringbuilder();     try {         (map.entry<string, string> entry : params.entryset()) {             encodedparams.append(urlencoder.encode(entry.getkey(), paramsencoding));             encodedparams.append('=');             encodedparams.append(urlencoder.encode(entry.getvalue(), paramsencoding));             encodedparams.append('&');         }         return encodedparams.tostring().getbytes(paramsencoding);     } catch (unsupportedencodingexception uee) {         throw new runtimeexception("encoding not supported: " + paramsencoding, uee);     } }  @override protected response<jsonobject> parsenetworkresponse(networkresponse response) {     try {         string jsonstring = new string(response.data,                 httpheaderparser.parsecharset(response.headers));         return response.success(new jsonobject(jsonstring),                 httpheaderparser.parsecacheheaders(response));     } catch (unsupportedencodingexception e) {         return response.error(new parseerror(e));     } catch (jsonexception je) {         return response.error(new parseerror(je));     } }  @override protected void deliverresponse(jsonobject response) {     listener.onresponse(response); }   } 

and use request elsewhere in app seen below:

    public void getrequest(string url, map<string, string> params, final string source) {      getheaders(headers);      customrequest jsobjrequest = new customrequest(request.method.get, url, params, headers, getreponse(source), createmyreqerrorlistener(), constants.priority_high);     queue.add(jsobjrequest);  } 

and code passing params through used in network call:

    private void forgotpasswordexecute() {     forgotpasswordbutton.setonclicklistener(new view.onclicklistener() {         @override         public void onclick(view v) {              map<string, string> params = new hashmap<string, string>();             localbroadcastmanager.getinstance(forgotpasswordactivity.this).registerreceiver(forgotpasswordreceiver, new intentfilter(constants.ws_forgot_password));             params.put("emailaddress", "some@email.com");               string url = constants.forgot_password_url;             request.getrequest(url, params, "forgotpassword");             system.out.println("url is: " + url);         }     }); } 

i need assistance encoding of url in volley. if volley not it, there way encode url , still use volley?

thanks

try , (may not best approach) direct code used in project. late answer after 1 year , may usefull future visitors..

public void yourmethodname() {      stringrequest request = new stringrequest(request.method.get, link , new response.listener<string>() {         @override         public void onresponse(string result) {             try {                 jsonobject jsonobj = new jsonobject(result);                 jsonarray jsonarray = jsonobj.getjsonarray("data");                  // here              } catch (exception e) {                 e.printstacktrace();                 toast.maketext(getcontext(),"check connection , or try again later",toast.length_short).show();             }         }     }, new response.errorlistener() {         @override         public void onerrorresponse(volleyerror volleyerror) {          }     }){         @override         protected response<string> parsenetworkresponse(networkresponse response) {             try { //                    string utf8string = new string(response.data, "utf-8");                 string data = urlencoder.encode("param", "utf-8") +     "=" + urlencoder.encode("param", "utf-8");                 url url = new url(link);                 urlconnection conn = url.openconnection();                 conn.setdooutput(true);                 outputstreamwriter wr = new outputstreamwriter(conn.getoutputstream());                 wr.write(data);                 wr.flush();                 bufferedreader br = new bufferedreader(new inputstreamreader(conn.getinputstream()));                 stringbuffer sb = new stringbuffer("");                 string line;                 while ((line = br.readline()) != null)                     sb.append(line);                 br.close();                 return response.success(new string(sb.tostring()), httpheaderparser.parsecacheheaders(response));             } catch (unsupportedencodingexception e) {                 return response.error(new parseerror(e));             } catch (exception e) {                 return response.error(new parseerror(e));             }         }     };       queue.add(request);  } 

Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -