Reputation: 17185
I am trying to send a call to a remote server with user credentials, and get back a true/false for whether the persons credentials match. But my code is returning the credentials right back to me in JSON lol
try
{
String query = String.format("login=%s&password=%s",
URLEncoder.encode(myEmail, charset),
URLEncoder.encode(myPassword, charset));
final URL url = new URL( myUrl + "?" + query );
final HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
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)
{
Log.d( "Exception: " , "Yup");
e.printStackTrace();
}
Would anyone know how I can get the output from the server instead of this stuff that I get back now?
Thanks!
Upvotes: 0
Views: 317
Reputation: 306
Another solution would be to work with the HTTPClient
a possible scenario would be like that:
HttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(yoururlhere);
HttpResponse response = httpClient.execute(request);
respondEntity = EntityUtils.toString(response.getEntity());
This will also work with HttpGet
just as you desire. Perhaps this will give you some clarity of what is "really" coming from the server.
Upvotes: 1
Reputation: 3824
First of all, you're sending the username and password both as URL-encoded parameters http://.../?username=abc&password=test
and as HTTP request headers.
Check the documentation of your service to see where the username and password is expected.
Could it be that you service uses HTTP Basic authentication? If so, take a look at the Android documentation at http://developer.android.com/reference/java/net/HttpURLConnection.html, under HTTP Authentication.
PS: remember to disconnect() your connection after use so the resources can be released.
Upvotes: 1
Reputation: 7659
Surprisingly the code you have posted works on my standalone java client as well as in my android app. It seems something is wrong on the server part. Can you post any additional output from server??
On server I have a servlet which receives user and passcode in JSON format and returns validation result in json format back.
Upvotes: 1