Question

Today I started developing a new Android app which is based on the Windows Azure Mobile Services. I've tried to use the SDK provided by Microsoft for the whole day, without any success. Every time the app performs a web request, this one returns a 400, BAD REQUEST code.

I go to a lower level, with the following code.

URL url = new URL("https://xxxx.azure-mobile.net/api/contents"); //host obscured
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();

conn.setRequestMethod("GET");
conn.setDoInput(true);

//I obscure the authentication and app key, but they are correct in the program
conn.addRequestProperty("AUTHORIZATION", "Basic xxxxxxxxxxxxxxx");
conn.addRequestProperty("X-ZUMO-APPLICATION", "xxxxxxxxxxxxxxxx");
conn.addRequestProperty("ACCEPT", "application/json");

//connect
conn.connect();
int code = conn.getResponseCode(); //400 in the emulator, 200 in a standard java code
String message = conn.getResponseMessage();
InputStream is = conn.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String read = null;
StringBuilder sb = new StringBuilder();
do {
//Read the content
    read = br.readLine();
    sb.append(read);
} while(read != null);

//print the content
System.out.println(sb.toString());

//Close the buffers
br.close();
isr.close();
is.close();

System.out.println("Status Code: "+code);
System.out.println(message);


conn.disconnect();

It works perfectly in a standard java application, but in the emulator I still get 400 BAD REQUEST. I really do not understand why! Maybe an issue of the emulator? I was running an Android Emulator with API 10.

I'd like to try the code on a real device, but the mac does not recognize it... And the device worked until yesterday since I debugged an application with it. If you know what I am doing wrong, please help me.

EDIT 20-03-2014:

I made some other attempts. I tried with the HttpClient API. No luck.

I'm still getting 400 as response code and Bad Request as response Message. The thrown Exception is UnknownHostException... But the InetAddress.getByName(); successfully get an IP address for my web service. The app still work with Android version greater than 4, I have not tried with Android 3. I'm very sad and frustrated...

Thank you for your time,

Rik.

Was it helpful?

Solution

SOLVED

Oddly, the problem was in the AUTHENTICATION header. I computed the header with the following code:

public setHeader(String code, String username)
{
    StringBuilder sb = new StringBuilder(username);
    sb.append(":");
    sb.append(code);
    sb = new StringBuilder(Base64.encodeToString(sb.toString().getBytes(), Base64.DEFAULT));
    sb.insert(0, "Basic ");
    auth = sb.toString();
}

The Base64.encodeToString() method adds a \n at the end of the line. .__.

By adding a trim() in the last line, I threw away the new line and everything went fine in both 2.3.3 and 4 Android versions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top