how to call web service in android - android

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";
}
}

Related

Android HttpClient to accept all cookies

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 ?!

Android HTTP request failing on emulator

I am requesting a HTTP request from Android Emulator but its getting failed .. Is there any issue with the emulator?
I have also enabled internet connection permission from Manifest
HttpClient Client = new DefaultHttpClient();
// Create URL string
String URL = "http://androidexample.com/media/webservice/httpget.php?user="+loginValue+"&name="+fnameValue+"&email="+emailValue+"&pass="+passValue;
//Log.i("httpget", URL);
try
{
String SetServerString = "";
// Create Request to server and get response
HttpGet httpget = new HttpGet(URL);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
SetServerString = Client.execute(httpget, responseHandler);
// Show response on activity
content.setText(SetServerString);
}
catch(Exception ex)
{
content.setText("Fail!");
}
}
I am using below link to test on emulator
http://androidexample.com/How_To_Make_HTTP_Get_Request_To_Server_-_Android_Example/index.php?view=article_discription&aid=63&aaid=88

Android HttpClient SLOW

i am currently working on an android app which includes a HTML-code-download feauture. However, the execution via HttpClient is very slow and takes about 12 seconds for a page which loads in 1 or 2 seconds via phone browser. I already tried using HttpURLConnection, but it downloads the code in roughly the same time. What would you peoplerecommend me changing?
public class Downloader extends AsyncTask<String, Void, String>
{
protected String doInBackground(String... params)
{
String url = params[0];
String htmlcode = "FAIL";
try{
Log.i("METAMETER","DL: INITIALIZING");
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
HttpParams httpParameters = client.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 1000);
HttpConnectionParams.setSoTimeout(httpParameters, 1000);
Log.i("METAMETER","DL: EXECUTING");
HttpResponse response = client.execute(request);
//Apparently the step above takes about 12 seconds for a page which only takes 1-2 seconds via browser
Log.i("METAMETER","DL: WRITING");
htmlcode = EntityUtils.toString(response.getEntity());
}catch (Exception e){htmlcode = "EXCEPTION";}
Log.i("METAMETER","DL: DONE");
return htmlcode;
}
}
Instead of
HttpClient client = new DefaultHttpClient();
Try using
HttpClient client = AndroidHttpClient.newInstance("MyApp/1.0");

httpget android wcf error

HttpClient Client = new DefaultHttpClient();
String URL = "http://192.168.2.22:1099/Service1.svc/test";
try
{
String setServerString = "";
HttpGet httpget = new HttpGet(URL);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
setServerString = Client.execute(httpget, responseHandler);
lblStatus.setText(setServerString);
}
catch(Exception ex)
{
lblStatus.setText("Fail!");
}
when we call the url it returns a string and is set to lblStatus.
This code is working fine in v2.3.* but not working in v4.0.
i'm able to get the string in v2.3.* versions but not in v4.0.
Indeed you are getting the NetworkOnMainTreadException. This exception has been introduced with HoneyComb and it is raised when an attempt of run a network operation on the UI Thread is made.
If you want to overcame it you have to use an AsyncTask. Read the android painless threading guide

Execute a HTTP request within an app

I was wondering if I could make it open But NOT open the android browser, I just need it to visit: (pretend this is the ip) http;//91.91.91.91:2228?1, where it will trigger action on my arduino mega. I have tried to get it just to do this with this code
onclick(Intent websiteIntent = new Intent(Intent.ACTION_VIEW);
Uri uri = Uri.parse("http;//91.9.91.91:?1");
websiteIntent.setData(uri);
startActivity(websiteIntent);)
but I don't know how to get it to do so
A HttpClient will allow you to call an arbitrary URL within your app:
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http;//91.9.91.91:?1");
HttpResponse response = client.execute(request);
Don't forget to wrap in a try catch though.
edit:
new Thread(){
public void run(){
try{
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http;//91.9.91.91:?1");
HttpResponse response = client.execute(request);
}catch(Exception e){
// Handle the exception
e.printStackTrace();
}
}
};

Categories

Resources