I'm pretty new in the Android world and maybe my question is very simple..
I have an android app where I use HttpGet to connect to a server and collect data.
However the server sometimes sets some cookies that are not remembered by my code.
I found a post where its using a custom cookie policy and is accepting everything.. just what I need.But I cant implement it.As I understand my version of java httpclient is old and does not have the functions I need.
Here's my code:
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(link);
get.getParams().setParameter(
ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);
HttpResponse responseGet = client.execute(get,ctx);
status_code = responseGet.getStatusLine().getStatusCode();
HttpEntity resEntityGet = responseGet.getEntity();
if (resEntityGet != null) {
http_response = EntityUtils.toString(resEntityGet);
}
}
And the code I need to implement:
CookieStore cookieStore = new BasicCookieStore();
httpclient.setCookieStore(cookieStore);
CookieSpecFactory csf = new CookieSpecFactory() {
public CookieSpec newInstance(HttpParams params) {
return new BrowserCompatSpec() {
#Override
public void validate(Cookie cookie, CookieOrigin origin)
throws MalformedCookieException {
log.debug("allow all cookies");
}
};
}
};
httpclient.getCookieSpecs().register("easy", csf);
httpclient.getParams().setParameter(
ClientPNames.COOKIE_POLICY, "easy");
All I need is to set this csf policy to my client.
However it seems that I dont have these two functions in the library : setCookieStore and getCookieSpecs().register()
What are my options to run it ?!
Related
i am new in Android development and now i am developing small application of google map. i have integrated google map,but now i want to show google places on map therefore i want to call google api web service to get locations, but i don't know how to call web service in android.enter code here
enter code here
public void getLocation()
{
HttpClient httpClient = new HttpClient()
AsyncHttpClient client =new AsyncHttpClient();
}
You can call Url's with HttpConnection.
For Example:
public String getValuefromUrl(String url)
{
try
{
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
ResponseHandler<String> resHandler = new BasicResponseHandler();
String page = httpClient.execute(httpGet, resHandler);
Log.v("PAGE",page);
return page;
}
catch(Exception ex)
{
ex.printStackTrace();
return "zero";
}
}
I used httppost to login a bbs.
The httpclient can save cookie and other information automatically.
I want to get cookie for httpclient and save it.
So next time I can give the cookie to httpclient and I can visit the bbs again.
So my question is how to get cookie from httpclient.
and how to save the cookie.
and how to set httpclient used the cookie.
Thank you.
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientContext context = HttpClientContext.create();
BasicCookieStore cookieStore = new BasicCookieStore();
context.setCookieStore(cookieStore);
HttpGet httpget = new HttpGet("https://host/stuff");
CloseableHttpResponse response = httpclient.execute(httpget);
try {
List<Cookie> cookies = cookieStore.getCookies();
if (cookies.isEmpty()) {
System.out.println("None");
} else {
for (int i = 0; i < cookies.size(); i++) {
System.out.println("- " + cookies.get(i).toString());
}
}
EntityUtils.consume(response.getEntity());
} finally {
response.close();
}
Please note you will need to use the official Apache HttpClient port to Android
Do it like this:
Header[] headers = null;
HttpResponse response = null;
HttpClient httpclient = new DefaultHttpClient(params);
HttpPost post = new HttpPost(URI.create(this._strBaseUrl));
response = httpclient.execute(post);
After request returned, extract cookie via:
headers = response.getHeaders("Set-Cookie");
Then You can iterate through cookie values ( if necessary).
I have the following problem:
My application needs to inform a website of a change as fast as possible
This can happen so fast that the previous webrequest hasn't been completely dealt with
The application should always send at least the last webrequest (it doesn't matter if previous ones are lost).
I'm not sure how to do this optimally. My current method is below, but gives me a warning ("Invalid use of SingleClientConnManager: connection still allocated."). I suspect I can reuse this connection, but have no clue how. Using threadSafeConnManager doesn't seem to be the solution, since I only need one connection (I think :) ).
How should I optimize my code for my needs?
The runnable in the code below is in a thread (webThread) and webrequest is a global variable that gets set to a certain url. After setting the variable, webThread.run() is fired.
private Runnable mSyncInternet = new Runnable() {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
public void run() {
HttpGet httpGet = new HttpGet(webrequest);
try {
HttpResponse response = httpClient.execute(httpGet, localContext);
} catch (ClientProtocolException e) {
sendMessageToUI(MSG_NO_INTERNET, 1);
} catch (IOException e) {
sendMessageToUI(MSG_NO_INTERNET, 1);
}
}
};
Thanks so much in advance!
Just today I found a blog with the solution to the SingleClientConnManager issue:
public static DefaultHttpClient getThreadSafeClient() {
DefaultHttpClient client = new DefaultHttpClient();
ClientConnectionManager mgr = client.getConnectionManager();
HttpParams params = client.getParams();
client = new DefaultHttpClient(new ThreadSafeClientConnManager(params,
mgr.getSchemeRegistry()), params);
return client;
}
I tried it, and it worked perfectly!
I am trying to send data to my server using HttpPost via the following code.
private boolean FacebookLogin(String url) {
boolean isDataSend = false;
try {
HttpClient client = new DefaultHttpClient();
HttpPost request = new HttpPost(url);
List<NameValuePair> value = new ArrayList<NameValuePair>();
value.add(new BasicNameValuePair("data", FacebookData()));
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(value);
request.setEntity(entity);
HttpResponse res = client.execute(request);
if (res.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
String bufstring = EntityUtils.toString(res.getEntity(),
"UTF-8");
isDataSend = true;
}
} catch (Exception e) {
}
return isDataSend;
}
Is there any way i can have a look at how the $_POST looks on the server end. so that it will be easier for me to code the server part.
You can write the received $_POST on a file. Sometimes I do that. It's not the most elegant solution, but it works fine.
Try using a http proxy (e.g. Fiddler) for debugging, it helps a lot in these cases. You can set up an emulator to use this proxy for network communications, so you can inspect the messages sent and received. Check out the emulator docs on how to configure it to use a proxy.
Need to send a POST request to a Service provider using HTTPS protocol, response from the service provider will be an xml file, need to read that also.
You could start by taking a look at AndroidHttpClient and at HttpPost.
Something like this should work:
final AndroidHttpClient httpClient = AndroidHttpClient.newInstance(this.getClass().getSimpleName());
HttpResponse httpresponse = null;
HttpEntity httpentity = null;
HttpUriRequest httprequest = new HttpPost("https://...");
byte[] xmlByteArray = null;
if ((httpresponse = httpClient.execute(httprequest)) != null) {
if ((httpentity = httpresponse.getEntity()) != null) {
xmlByteArray = EntityUtils.toByteArray(httpentity);
}
}
Also, my RestClient on github might be useful.
Note: I use GET to retrieve the data, so YMMV.