I am porting my website to Android using Webview. In my Android app, there is an indicator that shows the number of items in the cart. In Javascript, I call a function named refreshShoppingBadge
, that request an AJAX request to update the number. So I write the Javascript Interface for Webview like this:
@JavascriptInterface
public void refreshShoppingBadge(final String pUrl) {
if (pUrl == null) {
return;
}
final DefaultHttpClient request = new DefaultHttpClient();
try {
final HttpResponse response = request.execute(new HttpGet(
this.rootContext + pUrl));
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
final ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
Utils.changeTabIndicator(this.tabCartIndex,
String.format(this.tabCartContent, out.toString()));
} else {
Utils.alertNetworkProblem(this.context);
}
} catch (ClientProtocolException e) {
e.printStackTrace();
Log.e("Shopping", e.getMessage());
} catch (IOException e) {
e.printStackTrace();
Log.e("Shopping", e.getMessage());
}
}
However, the result is always 0, because HttpClient send the request with a new session, so there is nothing in the cart.
Can I "only get result" from the webview, or somehow get its session to request?