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
Related Q & A:
- How to pass multiple parameters in a single Ajax function?Best solution by stackoverflow.com
- How to attach components programmatically to GroupBox?Best solution by Stack Overflow
- How to disable Request information in twitter4j?Best solution by Stack Overflow
- How to attach the mast to the windsurfer board?Best solution by Yahoo! Answers
- How do you request a friend on Yahoo Messenger?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
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.