0

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?

Luke Vo
  • 17,859
  • 21
  • 105
  • 181
  • why don't you leave the ajax call in javascript ? – njzk2 Nov 18 '13 at 14:24
  • see http://stackoverflow.com/questions/11224454/android-share-session-between-webview-and-httpclient – njzk2 Nov 18 '13 at 14:25
  • @njzk2 Thank you, I am checking the reference. Because I want to update the number, it's a Android's textview, so Javascript cannot do that. – Luke Vo Nov 18 '13 at 14:37
  • @njzk2 Ah thank you for your hint, I got a way around. I will just call an Android Javascript when the Ajax finishes. Please post both your solution as answer. – Luke Vo Nov 18 '13 at 14:50

0 Answers0