How can I attach some parameters with request?

How to attach request parameters to current Android HttpUrlConnection call

  • I am able to send a request from an Android program to a remote server, but it seems the request has no parameters. How do I attach parameters? Here is my current code: @Override protected String doInBackground(String... theParams) { String myUrl = theParams[0]; final String myEmail = theParams[1]; final String myPassword = theParams[2]; Authenticator.setDefault(new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( myEmail, myPassword.toCharArray()); } }); String response = null; try { final URL url = new URL(myUrl); final HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestProperty("login", myEmail); conn.setRequestProperty("password", myPassword); conn.setUseCaches(false); final InputStream is = conn.getInputStream(); final byte[] buffer = new byte[8196]; int readCount; final StringBuilder builder = new StringBuilder(); while ((readCount = is.read(buffer)) > -1) { builder.append(new String(buffer, 0, readCount)); } response = builder.toString(); Log.d( "After call, response: " , " " + response); } catch (Exception e) { } return response; } So now I am not sure how to make the Authenticator password/login get attached to the request and get sent to the server. Any idea how I can accomplish that? Thanks!

  • Answer:

    You simply do this final URL url = new URL(myUrl+"?login="+myEmail+"&password="+myPassword); And you do not need the setRequestProperty lines. Those are actually setting your Http Request's properties and not the query params.

Genadinik at Stack Overflow Visit the source

Was this solution helpful to you?

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.