Server - Client connection between android app and a server - java

I'm developing a Java Server Aplication and an Android Aplication and my android app need to send and receive data from/to server (Bidirectional), for example my Android App need to Login to the server and the server need to know who is logged in.
Wich protocol do you recommend me to do this kind of program?

Usually in this situation you can use HTTP protocol for several reason. First of all you can reach your server even if it is behind a firewall or something like that.
Second using HTTP you can send XML or JSON data widely used in android.
The only limitation you have is the HTTP protocol is a synchronous protocol so you send and wait for the answer.
Using HTTP you can use your existing server architecture and you can wrap your business layer with a Webservices so that you can expose your services.
If you need that server can contact your app you can use you can use Google Cloud Mesaging.

Use Http request (get or post request) to communicate with a server.
You have to use a thread or an AsyncTask to perform your request or the execution fails from Api 11+.
I attach an example of http request that receives an xml:
import org.apache.http.*;
[..]
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
Log.d("XMLParser-getXmlFromUrl", "UnsupportedEncodingException");
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.d("XMLParser-getXmlFromUrl", "ClientProtocolException");
e.printStackTrace();
} catch (IOException e) {
Log.d("XMLParser-getXmlFromUrl", "IOException");
e.printStackTrace();
}
// return XML
return xml;
}

Related

Consuming service with SSO authentication in java?

I'm developing a web service that consumes another one. To authenticate with this other service, I have to use the SAML V2 protocol. How to programmatically consume this service in Java ?
My code without SSO:
HttpClient client = builder.build();
URIBuilder uriBuilder = new URIBuilder();
uriBuilder.setScheme(test.getScheme());
uriBuilder.setHost(test.getHost());
uriBuilder.setPort(test.getPort());
uriBuilder.setPath("Path");
uriBuilder.addParameter("id","theID");
uriBuilder.addParameter("param", "param");
try {
HttpUriRequest request = new HttpGet(uriBuilder.build());
HttpResponse response = client.execute(request);
return EntityUtils.toString(response.getEntity());
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
You'll need to build a SAML response that meets the requirements of the target service and include it in the call. Your best option is OpenSAML or another "product" such as CXF that builds on top of it. See How to create a valid SAML 2.0 Assertion with OpenSAML library in Java for an OpenSAML example; CXF ships with good docs and examples.

why Http Post working when send to ip but not when send to url?

I am using org.apache.http.client.methods.HttpPost to get a line of info from my server.
When I use the IP address of the server (the below is an example only) I get the info as expected:
HttpPost("http://127.0.0.1/api/fetchInfo")
However, when I use the URL the server is not getting a request and the client getting a runtime error: org.apache.http.client.HttpResponseException: Not Found.
HttpPost("http://subdomain.domain.com/api/fetchInfo")
The URL includes a subdomain.
the server i am trying to reach is a website provider and i'm able to interact with the website through the domain, so the domain exists.
the client is in my local computer and the server is on amazon ec2.
How to make it work with the url?
My code:
String responseString = "";
try {
MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
multipartEntityBuilder.setCharset(Charset.forName("UTF-8")).setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
HttpEntity entity = multipartEntityBuilder.build();
HttpPost request = new HttpPost("http://subdomain.domain.com/api/fetchInfo");
request.setEntity(entity);
HttpClient client = HttpClientBuilder.create().build();
HttpResponse response = client.execute(request);
responseString = new BasicResponseHandler().handleResponse(response);
} catch (IOException ex) {
log(ex)
}
return responseString;
If you are using the apache server then you need to configure the subdomain.conf file with the server name subdomain.domain.com and in the /etc/hosts file onto the machine you need to specify the mapping of subdomain.domain.com name with the ip 127.0.0.1.
I hope this helps.

Passing ArrayList from Java to PHP webservice

I have a java program that contains a username and passwords (strings) and an ArrayList of objects with 4 attributes (long, int, int int) and I want to pass these 3 things to a WebService (that I have yet to make). My host is Bluehost and it's a shared server so I won't have Java available server side it will need to be in PHP.
What is the best way of connecting to the webservice and passing this into php?
EDIT.
OK so I now have something like this:
public void upload(ArrayList<MyObject> myList) throws Exception{
//HTTP POST Service
try{
HttpClient httpclient = HttpClientBuilder.create().build();
URI uri = new URIBuilder()
.setScheme("http")
.setHost("www.myHost.com")
.setPath("/myWebservice.php")
.setUserInfo(userID, password)
.build();
HttpPost httppost = new HttpPost(uri);
httpclient.execute(httppost);
}catch (Exception e) {
e.printStackTrace();
}
}
But I'm still not sure how I can pass the ArrayList in a way that I'll be able to receive and split it into it's components on the PHP side?
You can use an HTTP client e.g. this one.
http://hc.apache.org/httpcomponents-client-ga/tutorial/html/index.html
and send a GET/POST request to your WebService.

Large data is not sending in android through webservice

I am using the following code in android to send data to a server through a web service
call.When i am sending small amount of data it is hitting the server.When i am sending large data it is not hitting the server.Simply it is httpClient.execute(httpPost); .But i am not getting any result.What might be the problem
HttpPost httpPost = new HttpPost(url+data);
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");
if (rsopnse != null)
System.out.println(httpPost.getMethod());
try
{
httpResponse= httpClient.execute(httpPost);
}catch(Exception e)
{
e.printStackTrace();
}
Thanks in advance...
You need to create a List<NameValuePair> for all the parameters that you want to pass in the Request. You should not append your parameters to the URL, which is more of the GET style of making a call.
The examples for HTTP Post are covered in the post here.

Web Service request is not working in android browser and code; but it is working in pc browser

My PC is connected to client's network via tunneling and I am calling a webservice which is in client's network. This works fine in PC Browsers, where as when I try to access it via android application I am getting ClientProtocolException. I tried to hit the web service ip via android browser I was able to see the IIS server response in browser but couldn't access the webservice. Not an https url, just normal http url. It's not working in any version of android emulator.
Code :
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(url);
HttpResponse response;
try {
response = httpclient.execute(httpget);
Log.i("Debug",response.getStatusLine().toString());
HttpEntity entity = response.getEntity();
if (entity != null) {
....
}
} catch (Exception e) {
e.printStackTrace();
}
It could be a firewall application (there is none built-in to my knowledge) on your Android device that may be blocking the access to the webservice.
Actually there is no response headers in the webservice. That is why I got the exception. Now I opened a socket to get the response, but still a request without proper response header is not good.
Have you tried setting up a Web proxy?
httpclient.getHostConfiguration().setProxy("ip-address","port")
or
System.setProperty("http.proxyHost", "ip-address");
System.setPropery("http.proxyPort", "port");

Categories

Resources