I'm developing for Nokia Asha and using Tantalum library to make my HTTP requests through the HTTPPoster class.
When the task is executed I try to get the value from the X-Session-Id header from the rest of the response headers, these are returned as a HashTable, the value in this header I need to convert it to a SHA256 hash.
But I am unable to convert the value I get from the HashTable into String.
My code looks like this:
final HttpPoster httpPoster = new HttpPoster(Task.FASTLANE_PRIORITY, completeURL);
httpPoster.setRequestProperty("Content-Type", "application/json");
httpPoster.setPostData(new byte[]{});
httpPoster.chain(new Task(Task.FASTLANE_PRIORITY){
protected Object exec(Object in) throws CancellationException,
TimeoutException, InterruptedException {
// TODO Auto-generated method stub
System.out.println("Task is executing");
String tmpSessionId = new String(httpPoster.getResponseHeaders().get("X-Session-Id").toString().getBytes());
byte[] input = null;
try {
input = new String("X-Session-Sig" + tmpSessionId + "test").getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
System.out.println("Unsupported encoding exception");
e.printStackTrace();
}
byte[] key = new byte[4];
int tmpInt = new Random().nextInt();
key[0] = (byte)(tmpInt >> 24);
key[1] = (byte)(tmpInt >> 16);
key[2] = (byte)(tmpInt >> 8);
key[3] = (byte)(tmpInt);
Digest digest = new SHA256Digest();
HMac hmac = new HMac(digest);
hmac.init(new KeyParameter(key));
hmac.update(input, 0, input.length);
byte[] output = new byte[digest.getDigestSize()];
hmac.doFinal(output, 0);
sessionId = tmpSessionId;
sessionSig = new String(Hex.encode(output));
System.out.println("session id " + sessionId + " session signature " + sessionSig);
return in;
}
protected void onCanceled(){
System.out.println("Request was cancelled");
}
});
httpPoster.fork();
As you can see, in the line String tmpSessionId = new String(httpPoster.getResponseHeaders().get("X-Session-Id").toString().getBytes());
I try to convert to String and then get the bytes but this returns [Ljava.lang.String;@6f7159d, not the actual value I'm looking for, even calling the method toString() without performing the getBytes method returns the same value.
How can I get the true value of this element in the HashTable? As far as I can see, J2ME doesn't support ObjectOutputStream class. What are the alternatives?
I found that the [Ljava.lang.String;@6f7159d part meant that the object inside the HashTable was a String array.
After realizing that I just need to cast to String[]:
String[] stringArray = (String[]) dotREZApi.getCurrentRequest().getResponseHeaders().get("X-Session-Id");
String tmpSessionId = stringArray[0];
This returned to me the value I was expecting.