I have the following site http://www.freewebservicesx.com/GoldSpotPrice.aspx which provides a webservice api. As i am extremely new to soap and rest i have absolutely no clue about how to call this service and use it in android. Could someone please tell me how to do it.
You can make HTTP requests using either HttpURLConnection or HttpClient. Example of using HttpClient for a RESTful web service:
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.mywebsite.com/webservice");
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
ByteArrayOutputStream out = new ByteArrayOutputStream();
entity.writeTo(out);
out.close();
String responseStr = out.toString();
// process response
} else {
// handle bad response
}
Otherwise, if you're working with a SOAP web service, I would recommend using ksoap2
Use the following url to refer:::
Using ksoap2 for android, and parsing output data...
ksoap2-android - HowToUse.wiki.....
ksoap2-android
Use this LINK it has a sample the suits your requirement.
also LINK
Related
How to save and retrieve data from database. Can somebody give me a little help how to connect web service with database and in app, what would be the first step to start. Thanks.
Build a web server, which will be sending the JSon objects to you
Then use http calls to get this Json, like this:
public String httpGet(String url) throws IOException{
HttpGet get = new HttpGet(url);
DefaultHttpClient() _httpClient = new DefaultHttpClient();
BasicHttpContext httpContext = new BasicHttpContext();
HttpResponse response=null;
response = httpClient .execute(get, httpContext);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
return EntityUtils.toString(response.getEntity());
}
For more information, leave me a message, please.
To communicate with a remote database: database on server for example you need to communicate with an API. They are several ways to build an API: PHP, ruby.
Communicating with a remote db is forbidden in most of hosts for security purpose.
I am accessing a REST web service via Mozilla client via Mozilla browser. (In web service
back end spring security has configured and credentials for each
request is validated). So I am setting basic authentication (username and password) and
Request header "application/json" which is fine.
Now I want to connect to this web service via a android application as below. How do I set
credentials of the user (username and password) to the request ?
#Override
protected Void doInBackground(Void... params) {
HttpClient client = new DefaultHttpClient();
WsUrl="http://192.168.0.15:8080/ServiceApp/categoryservice/category/001"; // RESTFUL URL RESOURCE
HttpGet getRequest = new HttpGet(WsUrl);
try {
HttpResponse response=client.execute(getRequest);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
if(statusCode != 200){
return null;
}
InputStream jsonStream = response.getEntity().getContent();
ufferedReader reader = new BufferedReader(new InputStreamReader(jsonStream));
check the link for answer, I don't what kind of authentication you are using, but this link shows how to windows basic authentications
HTTP requests with basic authentication
I have no idea about how to call a web service from Android application . I also want to use trulia web service to get data from it .
So please help me.
I dont know what trulia is, If you want to use a SOAP webservice, you should use KSOAP2. Google it and you will find alot of tutorials how to use it.
There are alot of pepol explain it good.
Are KSOAP2 not for you?
Check out HTTPClient for android. here is a good one for that http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/
Now, google and u find ur answer!
GL !
You can use HttpClient to connect with the webservice/webpage/remote-data.
Here is a sample code using that you can connect webservice from Android application
try {
HttpClient client = new DefaultHttpClient();
HttpGet method = new HttpGet(Url);
HttpResponse response = client.execute(method);
StatusLine status = response.getStatusLine();
if (status.getStatusCode() == HttpStatus.SC_OK) {
ResponseHandler<String> responseHandler = new BasicResponseHandler();
String responseBody = responseHandler.handleResponse(response);
return responseBody;
}
return null;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
I am new to android networking and would like to find the best solution/source that would help me learn the same. I have a working android application but i want to include network services that can get and send data to the webserver. While i was searching for the same i found link ( http://www.helloandroid.com/tutorials/connecting-mysql-database ) which dont produce any results. I also found that SOAP or REST (with android) are probably recommended methods, if so please give complete tutorials to learn the same ( i have no prior knowledge on webservices). In my application I would be required to send data to the server and receive data from the servers sql
Thank you
This is for post data on server,
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs,HTTP.UTF_8));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
// get response entity
HttpEntity entity = response.getEntity();
// convert entity response to string
if (entity != null)
{
InputStream is = entity.getContent();
// convert stream to string
result = convertStreamToString(is);
result = result.replace("\n", "");
}
or see how to post data to remote server in android app [closed], Post the data to Server
In my Android application, i'm using ksoap2 library for consuming a soap ( & .net) webservice. It works correctly but it's very slow (for parsing, controls, loops and Base64ToBinary processes). I want to parse it more fast. Is it possible to parse it without using ksoap2? Any ideas?
Thanks for your recommendations.
What do you mean slow? It does a http request and then parses the response. You have to do this in an async task. Memory usage will depend on the response you get. Maybe you are requesting way too much. See the wiki on how to debug!
I have another problem with KSoap2. Unfortunately, ksoap2 library is not working with my webservices. So at last, I have done with default http post.
I hope this will help for someone in future.
private String makeHttpRequest(){
try{
String request = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ns1=\"http://tempuri.org/\">"
+"<SOAP-ENV:Body>"
+ "<ns1:Connect>"
+ "<ns1:lstr_Login>xxxxx</ns1:lstr_Login>"
+"</ns1:Connect>"
+"</SOAP-ENV:Body>"
+"</SOAP-ENV:Envelope>";
String soapAction = "http://tempuri.org/Connect"; //this would be your soapAction from wsdl
StringEntity se = new StringEntity(request, HTTP.UTF_8);
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(new URI("http://xxxxxxxx.com/Storefront.asmx"));
httpPost.addHeader("Content-Type", "text/xml; charset=utf-8");
httpPost.addHeader("SOAPAction", soapAction);
httpPost.setEntity(se);
HttpResponse response = client.execute(httpPost);
int responseStatusCode = response.getStatusLine().getStatusCode();
Log.d(TAG, "HTTP Status code:"+responseStatusCode);
if(responseStatusCode>=200 && responseStatusCode<300){
//we got the success response from server. Now retrieve the value and go for usage.
String responseStr = EntityUtils.toString(response.getEntity());
//use this responseStr to parse with pullparsers or any
Log.d("Response", "Response:: "+ responseStr);
return responseStr;
}
}catch(Exception e){
//Write the proper catch blocks for exceptions
Log.e("Response Exception" , e.getMessage()+"",e);
}
return null;
}