I've been pulling my hair trying to figure this out: I'm making an HttpsURLConnection and using java.net.cookiemanager to manage my cookies (there's no way of using android.webkit.cookiemanager to HttpUrlConnection/HttpsUrlConnection as I have understood?). I need to save my longtime cookie to later connections.
Sadly I can't use http://loopj.com/android-async-http/ and it's PersistentCookieStore because I need to allow an untrusted certificate (using http://abhinavasblog.blogspot.se/2011/07/allow-untrusted-certificate-for-https.html). I've tried using their PersistentCookieStore alone but they are using apache cookies and I'm using java.net cookies...
This is what I've tried:
cManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);
private void setSharedPreferences(){
List<HttpCookie> cookies = cManager.getCookieStore().getCookies();
if (cookies.isEmpty()) {
Log.d(tag,"no cookies received");
} else {
for (int i = 0; i < cookies.size(); i++) {
if(cookies.get(i).getName().equals("rememberMe")) {
editor.putString(
"rememberMe", cookies.get(i).toString());
editor.commit();
}
}
}
}
And when I'm retrieving the cookie on next app launch:
SharedPreferences sharedPreferences = context.getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
editor = sharedPreferences.edit();
String rememberString = sharedPreferences.getString("rememberMe", "none");
if (!rememberString.equals("none")) {
Log.d("rememberME är inte", "none!");
URI uriToCookie = null;
try {
uriToCookie = new URI("https://myservername.com");
} catch (URISyntaxException e) {
e.printStackTrace();
}
List<HttpCookie> cookieList = HttpCookie.parse(rememberString);
cManager.getCookieStore().add(uriToCookie, cookieList.get(0));
}
The cookie is added to cManager but is not recognized by the server.. I believe there is some sort of parse problem. Anyone got the solution?
I used this part:
cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies();
Log.v("Cookie:", cookies.toString());
if (cookies.isEmpty()) {
} else {
for (int i = 0; i < cookies.size(); i++) {
if(cookies.get(i).getName().contentEquals("PHPSESSID")) {
PHPSESSID = cookies.get(i).getValue();
}
}
}
Just use the contentEquals to get the domain name and let it match with urs and store it. Oh and i used PHPSESSID as a string which i dumped in my sharedprefs for later
After looking at your code, I code be wrong but you are simply not storing the entire cookie:
for (int i = 0; i < cookies.size(); i++) {
if(cookies.get(i).getName().equals("rememberMe")){
editor.putString("rememberMe", cookies.get(i).toString());
editor.commit();
}
}
You get the List length of cookies with cookies.size() and loop and get all the cookies but you commit the save key-value "rememberMe" instead of appending or storing in separate keys. So basically, you are simply overwriting what you stored over and over.
Related
I am overriding this method for my WebView. And I am using the proper domain. When debugging using an Android simulator I only see a partial list of all of the cookies. The one in particular I'm requiring the value of isn't listed. Any suggestions? Source code below.
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
_cookieManager.Flush();
string customerId = null;
var cookies = _cookieManager.GetCookie(Constants.DCH_DOMAIN);
Log.Info(TAG, string.Format("Cookies retrieved as {0}.", cookies));
if (cookies != null)
{
string[] tempList = cookies.Split(';');
foreach (var pair in tempList)
{
if (pair.Contains("wishlist_customer_id"))
{
string[] tempPair = pair.Split('=');
customerId = tempPair[1];
if (customerId != null)
{
Log.Info(TAG, string.Format("Customer ID retrieved as {0}.", customerId));
PCLStorage(customerId);
}
}
}
}
UPDATE There is another domain that stores cookies as part of this web session. No different in path, HTML, same site, secure, etc. between the four cookies. I'll attach what Chrome on my workstation looks like, showing these four cookies. Then I'll attach what my Android simulator's device log looks like, where I output the cookie collection based on the subclassed WebViewClient. Only two of the four cookies appear to be there. I inserted a SystemClock.Sleep(5000) before retrieving the cookies, in order to give them a chance to fully populate as well.
Chrome session
WebViewClient session
you should do cookied synchronization before load url to ensure complete cookie data like :
_cookieManager.flush ();
webView.loadUrl(url);
then get cookies int the OnPageFinished method :
public override void OnPageFinished(WebView view, string url)
{
base.OnPageFinished(view, url);
string customerId = null;
var cookies = _cookieManager.GetCookie(url);
Log.Info(TAG, string.Format("Cookies retrieved as {0}.", cookies));
if (cookies != null)
{
string[] tempList = cookies.Split(';');
foreach (var pair in tempList)
{
if (pair.Contains("wishlist_customer_id"))
{
string[] tempPair = pair.Split('=');
customerId = tempPair[1];
if (customerId != null)
{
Log.Info(TAG, string.Format("Customer ID retrieved as {0}.", customerId));
PCLStorage(customerId);
}
}
}
}
Thanks to Leo Zhu the issue has been resolved. Apparently before I load the WebView URL I needed to flush the CookieManager instance. This resulted in all of the cookies for this particular resource to appear and be available for querying. Hope that this might help anyone else who runs into a similar challenge!
I have successfully uploaded 6 image one by one on Cloudinary. Now I want to get all images URL. How can I get in android mobile client side?
I am using code below.
List<String> imageList = new ArrayList<>();
Map config = ObjectUtils.asMap(
"cloud_name", "shank",
"api_key", "644617911542992",
"api_secret", "oW3bQk8luOT9UlEkRsH21KoQkxY");
Cloudinary cloudinary = new Cloudinary(config);
Api api = cloudinary.api();
JSONObject outerObject = null;
String jsonNext = null;
boolean ifWeHaveMoreResources = true;
while (ifWeHaveMoreResources) {
try {
outerObject = new JSONObject(api.resources(ObjectUtils.asMap("max_results", 10, "next_cursor", jsonNext)));
if (outerObject.has("next_cursor")) {
jsonNext = outerObject.get("next_cursor").toString();
ifWeHaveMoreResources = true;
} else {
ifWeHaveMoreResources = false;
}
JSONArray jsonArray = outerObject.getJSONArray("resources");
for (int i = 0, size = jsonArray.length(); i < size; i++) {
JSONObject objectInArray = jsonArray.getJSONObject(i);
String public_id = objectInArray.get("public_id").toString();
String url = objectInArray.get("secure_url").toString();
imageList.add(url);
}
} catch (Exception e) {
e.printStackTrace();
}
}
But it gives the exception.
java.lang.Exception: Administration API is not supported for mobile applications
Please help me to resolve this issue.
java.lang.Exception: Administration API is not supported for mobile applications
Due to security reasons, Admin API is not available with client SDK like Android.
You should use some kind of proxy server to make this call to Cloudinary (you can use Cloud Functions for Firebase for example)
Hi guys i have already made login and register using volley library and data gets saved successfully as shown in the snap 1 below. Now my question is how to retrieve particular or specific row data for the particular user when he/she logins?? For example if user admin gets logged in how to fetch her entire row alone
saved data in server side
<?php
$conn = mysql_connect("localhost", "root", "");
if(isset($conn))
{
mysql_select_db('your_db', $conn);
}
else
{
echo 'Sorry,can not connect to database' ;
}
$userid = isset($_GET['id']) ? mysql_real_escape_string($_GET['id']) : "";
$qur = mysql_query("select usename,other_fields from `your_tbl` where userid= $userid");
$result =array();
while($r = mysql_fetch_array($qur)){
extract($r);
$result[] = array("usename" => $usename,"other_fields" => $other_fields);
}
$json =array("data"=>$result);
mysql_close($conn);
/* Output header */
header('Content-type: application/json');
echo json_encode($json);
You will have to use server side scripting. Webservice that will fetch that particular row of data.
The script should take your input parameters and give you the out put in Json Array or Object.
You then hit this script using volley and the response you receive can then be utilized for the next step.
//call volley get method and get data in success method
public void onSuccess(string response)
{
try {
String s = new String(response);
JSONObject jsonObject = new JSONObject(s);
if (jsonObject.has("data")) {
JSONArray country =jsonObject.getJSONArray("data");
for (int i = 0; i < country.length(); i++) {
JSONObject cnt = country.getJSONObject(i);
String res=cnt.getString("username"));
//now you can use res as per your requirement
}
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
In my android application I have a webview. It loads URLs from multiple domains. I need to delete all cookies from a specific domain. I want to keep cookies from other domains. But I need to delete all cookies from one domain. I'm open to all other solutions that handles my request. (note that domain uses both http and https)
But when I try to use CookieManager.setCookie, all available cookies for that domain didn't deleted. Multiple cookie keys appeear when I try to write to that keys.
I attach my code below. You can find results in comment lines. At the end of story I get this cookie. Note for multiple values:
"userid=12%34; token=12ased; remember_check=0; userid='-1'; token='-1'; remember_check='-1';"
My helper function that splits cookie string to get cookie keys:
public static Vector<String> getCookieAllKeysByCookieString(String pCookies) {
if (TextUtils.isEmpty(pCookies)) {
return null;
}
String[] cookieField = pCookies.split(";");
int len = cookieField.length;
for (int i = 0; i < len; i++) {
cookieField[i] = cookieField[i].trim();
}
Vector<String> allCookieField = new Vector<String>();
for (int i = 0; i < len; i++) {
if (TextUtils.isEmpty(cookieField[i])) {
continue;
}
if (!cookieField[i].contains("=")) {
continue;
}
String[] singleCookieField = cookieField[i].split("=");
allCookieField.add(singleCookieField[0]);
}
if (allCookieField.isEmpty()) {
return null;
}
return allCookieField;
}
I get present cookies:
// I take cookie string for specific URL
mCookieManager = CookieManager.getInstance();
String url2="https://mysite.com";
String cookieString = mCookieManager.getCookie(url2);
Toast.makeText(mContext, "cookie string:\n"+cookieString, Toast.LENGTH_SHORT).show();
// result is: userid=12%34; token=12ased; remember_check=0;
Then I call to replace old cookies.
Vector<String> cookie = CookieUtil.getCookieAllKeysByCookieString(cookieString);
if (cookie == null || cookie.isEmpty()) {
Toast.makeText(mContext, "cookie null", Toast.LENGTH_SHORT).show();
}
if (cookie != null) {
int len = cookie.size();
Toast.makeText(mContext, "cookie number: "+len, Toast.LENGTH_SHORT).show();
// result is, cookie number: 3
String cookieNames="";
for (int i = 0; i < len; i++) {
cookieNames += "\n"+cookie.get(i) ;
mCookieManager.setCookie(url2, cookie.get(i) + "='-1';");
}
Toast.makeText(mContext, "cookieNames:\n"+cookieNames, Toast.LENGTH_SHORT).show();
// result is: "cookienames: userid token remember_check"
mCookieSyncManager.sync();
cookieString = mCookieManager.getCookie(url2);
Toast.makeText(mContext, "cookie string:\n"+cookieString, Toast.LENGTH_SHORT).show();
mCookieSyncManager.sync();
// result is: "userid=12%34; token=12ased; remember_check=0; userid='-1'; token='-1'; remember_check='-1';"
}
Edit:
I also tried setCookie like this:
mCookieManager.setCookie(url2, cookie.get(i) + "=-1;");
mCookieManager.setCookie(url2, cookie.get(i) + "=-1");
Edit2: setCookie's signature is like this:
/**
* Sets a cookie for the given URL. Any existing cookie with the same host,
* path and name will be replaced with the new cookie. The cookie being set
* must not have expired and must not be a session cookie, otherwise it
* will be ignored.
*
* #param url the URL for which the cookie is set
* #param value the cookie as a string, using the format of the 'Set-Cookie'
* HTTP response header
*/
public void setCookie(String url, String value) {
throw new MustOverrideException();
}
Although I get same keys inside cookie string ("userid=12%34; token=12ased; remember_check=0; userid='-1'; token='-1'; remember_check='-1';") will they have different host or path ?
I've had a similar experience with the CookieManager in Android. Setting the same cookie will indeed add it as a new cookie.
Please try to implement this solution. It will enable you to flush the cookies you want to remove and then you'll be able to set the again as you desire.
Good luck!
First, we can delete the cookie via CookieManager's interfaces:
setCookie(URL, 'COOKIE_KEY=;');
Then, we need to find out the correct URL, considering both Domain and Path attributes of the cookie.
For example, the following cookie
document.cookie = 'COOKIE_NAME=COOKIE_VAL; path=/; domain=.example.com;'
can be deleted by
setCookie('.example.com', 'COOKIE_NAME=;')
and cannot be deleted by
setCookie('www.example.com/info.html', 'COOKIE_NAME=;')
Finally, here is an example to remove a cookie.
String[] kvPairs = CookieManager.getInstance().getCookie(url).split(" ");
for (String kvPair : kvPairs) {
String newPair = kvPair.replaceAll("=.*", "=;");
// Delete the cookie asynchronously.
CookieManager.getInstance().setCookie(url, newPair);
}
I have native application in Andriod and Iphone with ASP.NET as a back-end. I am sending request to my ASP.NET server API by appending Session_Id cookie using HttpClient classes which works great. But I also need to send the same session in WebView. The problem is that some times WebView send the Session_Id cookie and sometimes doesn't. It strange for me. I am not able to find why sometimes webview send the cookie and sometimes doesn't. Here is my code,
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myapp = ((....) getApplicationContext());
setTimeout(true);
try {
getActionBarHelper().setupHomeActivity();
} catch (Exception e) {
e.printStackTrace();
}
cm = (ConnectivityManager) this.getSystemService(Activity.CONNECTIVITY_SERVICE);
webView = new CustomWebView(this, this);
webView.getWebView().addJavascriptInterface(new MyJavaScriptInterface(), "android");
getTimeDiff();
if(MyApplication.INSTANCE.isLoggedin()){
Cookie sessionCookie = MyApplication.INSTANCE.getCookie();
List<Cookie> cookies = MyApplication.INSTANCE.getClient().getCookieStore().getCookies();
for (int i = 0; i < cookies.size(); i++) {
sessionCookie = cookies.get(i);
}
try{
if(sessionCookie!=null){
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
if (sessionCookie != null) {
cookieManager.removeSessionCookie();
String cookieString = sessionCookie.getName() + "="
+ sessionCookie.getValue() + "; domain="
+ sessionCookie.getDomain();
if (MyApplication.isDebug())
cookieManager.setCookie(sessionCookie.getDomain(),(sessionCookie.getName() + "=" + sessionCookie.getValue()));
CookieSyncManager.getInstance().sync();
}else{
if (MyApplication.isDebug())
Log.d("WebView", " Cookie is null: " );
}
}else{
if (MyApplication.isDebug())
Log.d("WebView", " Cookie is null: " );
}
}catch (Exception e) {
e.printStackTrace();
}
Log.e("WenView", "============================ ");
}
}
Your for loop appears to only include the single line that retrieves the cookie... so your logic that's setting the cookie below only gets called once, with whatever the last cookie happened to be. Since the cookies aren't ordered in the list, this probably explains why sometimes it works and sometimes it doesn't; it's working when your cookie is last, and failing otherwise.
The answer is to just expand your for loop to include ALL the logic underneath as well. Your copying code looks fine, and I expect it is if you can get it to work sometimes.