Skip to content Skip to sidebar Skip to footer

Android Setrequestproperty In A Url.openconnection()

I have an Android app that need to set a requestproperty in a connection. Here is my code: URL url = new URL(sUrl); HttpURLConnection connection = (HttpURLConnection) url.openCo

Solution 1:

Here url.openCOnnection() will open new connection to the resource referred to by this URL. Here you again opening a connection by calling url.connect() method. So remove that

Check this.. for the sample example...

Solution 2:

You could try to use the CookieManager mentioned in http://developer.android.com/reference/java/net/HttpURLConnection.html

Set your cookie to CookieManager

CookieManagercookieManager=newCookieManager();
    CookieHandler.setDefault(cookieManager);

    HttpCookiecookie=newHttpCookie("lang", "fr");
    cookie.setDomain("twitter.com");
    cookie.setPath("/");
    cookie.setVersion(0);
    cookieManager.getCookieStore().add(newURI("http://twitter.com/"), cookie);

Source: http://developer.android.com/reference/java/net/HttpURLConnection.html

Use url.openConnection() after you set your cookie.

Post a Comment for "Android Setrequestproperty In A Url.openconnection()"