How to call a method in java web service? - java

I'm a learner of both java and android programming. I made a web service of hello world. Created a class called HelloWorld. There is only one method in it like
sayHello(){
System.out.println("hello world")
}
Now how should I write a android main activity to invoke this method using http request. what are the prerequisites to use http request. What are the libraries to import. Should I download any library from any where? I googled for it. Many irrelevant results are coming. I'm not able to comprehend from them.So seeking help here which seemed to be the only option.
Kind answer is appreciated.

The Android platform comes with everything you need to make HTTP requests. You should take a look at the URLHttpConnection. Bear in mind, that making an HTTP request is a bit harder then just using this class. You should think about doing it on a worker thread, so you don't block the UI. You should also think about the data transfer protocol you are going to use for your client - server communication and how to process the received responses.
I think this can get you started: http://developer.android.com/training/basics/network-ops/connecting.html
When you feel more comfortable with the concept, you can try and use a library for your HTTP request. Here is a useful link: https://developer.android.com/training/volley/index.html
Only one thing - seems like your web service is not sending any data back to clients, it's just printing to the server standard output, so it is unlikely you see any information sent to your Android app.

Related

Track outgoing REST call in Java Function App inside Application Insights

From what I understand Azure Application Insights is able to automatically track and log incoming HTTP requests to an application.
However, my usecase is I need to track an outgoing HTTP request (that I make programmatically).
I have a Java Function App (with an HTTP trigger) running in Azure with App Insights attached to it. It logs logs the incoming HTTP request to the function app. Once triggered, my app makes a REST call to a 3rd party REST API. I would like to log this call either as a request or as a dependency under the function app in App Insights
Pointers on how to do this are appreciated.
The documentation seems vague. I looked at a similar question that is answered for C# as well as this, but my question is JAVA and Function App specific and I want the request to be logged and correlated correctly to the above Function APP
If you look at https://learn.microsoft.com/en-us/azure/azure-monitor/app/java-in-process-agent#instrumentation-libraries , it lists a bunch of autocollected dependencies
You just have to use one of the autocollected HTTP libraries (e.g OkHttp), and the outgoing http calls should be automatically tracked.
After some back and forth with Microsoft about this, it turns out Azure functions has a specific set of configuration parameters that needs to be put in. I am posting the windows one as thats what I used, but the linux one is in the link as well.
(Windows):
XDT_MicrosoftApplicationInsights_Java -> 1
ApplicationInsightsAgent_EXTENSION_VERSION -> ~2
https://learn.microsoft.com/en-us/azure/azure-monitor/app/monitor-functions#how-to-enable-distributed-tracing-for-java-function-apps

communication between android app (java) with windows app (vb.net) by web service

I have application Android to control computer (restart,shutdown,...) by connecting Android application with a Windows application (vb.net) through the Web Service
I do not know where to start and how to contact Anyone who has any idea on the subject ?
Since this is a broad question and you asked for ideas let me pour some thoughts.
Where to start? Define a protocol for the web service that both parties understand. For example define an URI that the android client can push messages to (with PUT or POST) and the vb.net client can poll for updates (with GET). Since we use one URI here the payload sent to the URI must define what to do. The android client could for example post the message '{command:restart}" then the vb.net client should know that he has to restart the machine if the message is received.
There are a few caveats here though. First the vb.net client needs to poll the URI to get the command. So between sending the command and executing it there is some delay. Even better, right now you don't even know if the command was executed or not. Also since there is no authentication everybody can issue commands to the web service. Sounds like tons of fun. For other parties at least. Lastly you need to clear the command from the URI if it was executed or updated. Else the machine would, in this example, go into a restart loop.
So the first problem may be solved with and extra URI where the android client can poll for some king of system state that the vb.net client provides. For example the vb.net client may send the message '{state:restarting}' to the URI before restarting. Of course the vb.net client needs to update that URI every time some event happens. The security issue? Security is hard. You may want to create a certificate and sign the requests sent so the server with it. Then the server can check if it was really you who sent this request. Or somebody that has your cert, replayed the request or found some way of breaking the whole stuff. So you additionally might want to use TLS with some king of certificate pinning. You might already know this but I'm going to repeat it anyway: do not send credentials over an insecure network. Also base64 (used in basic authentication) is not some kind of encryption. The last problem may be solved with a nonce that can be included in the command. Or the vb.net client clears the command from the URI (with DELETE) when it received the command. It should update the status URI though so that the android client does not send the request again.
Or you drop the whole web service stuff use technologies like Web Sockets. Then the android client can send a command to the socket. The server receives to command and sends it to the vb.net socket. When the command is received by the vb.net socket it sends out some kind of delivery receipt to the server and executes the command. The server receives the receipt and sends a confirmation the the android client so that it known that the command is going to be executed.
As you see there is a lot do do when implementing such a solution so let me summarize:
define you use cases. What operations must the system support and how are the participating parties involved in any case.
find corner cases for each use case. For example the authentication problem that needs to be solved.
do some research on technologies that support your idea at the best possible way. Maybe a web service is not the best solution? What is a web service anyway? (Seems to be some king of buzzword anyone uses). Maybe Web Sockets, XML RPC or even SOAP are better candidates?
define a protocol for the components
implement it
One last note. I know this is a rather long answer? that might spawn a lot of questions in the readers head. But, do not get discouraged! You don't have to do anything that I wrote here. Well except the security thing maybe.

How to upload a file in android using AsyncTask

I am just a beginner at Android so this question might be stupid. PLease help me out of this.
I am working on an App which requires data to be uploaded on the server. The data could be text, image or a video. I tried to use the following link
http://developer.android.com/reference/java/net/HttpURLConnection.html
and also
http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106
However I ended up being even more confused. How do I send the data into the server using the ASyncTask doinbackgound method.
Thanks.
I suggest you use a REST Client for your networking needs. They allow easy use of Async Http Requests and even easer exception handling.
These are some the most notable REST Clients available out there:
Retrofit
ION
Android-Query

Sending and receiving data from an Android application to a web hosted Java application

I've been looking around all morning, and can't seem to figure this one out. I know it's not as complicated as I think it is, and all I need is just some pointers to the right direction.
I have an Android application that takes some user input, sends it to a Java application over the web, and then receives some output based on the input.
My Android and Java applications are ready, but how do I go about sending and receiving the data over the web? I understand that I'll be sending and receiving HTTP requests, but my problem is where I'll be sending them and receiving them from. Do I host my Java application on a Servlet like Tomcat, or do I use something like OpenShift to host my application for me?
I have no trouble with sending some HTTP request from my Android application to the web and receiving some output back, but my problem is that my web service needs to use a Java library to process the input and generate the output. I'm just not sure how I'm supposed to get this data to and from this Java library that needs to be hosted on the web.
I hope my question makes any sense.
EDIT: Perhaps I wasn't clear with what exactly I need help with. I do realize that the architecture I use doesn't really make any difference, but the problem I have is with how I'm supposed to use this architecture.
Let's say I have a registered domain name that I can easily send to and receive data from using my Android application - no trouble with that. But how am I supposed to integrate my Java library with this server? Do I just create an applet and put it on my server? Does my web host even allow Java applets to run?
I guess my question is how I'm supposed to get through the "barrier" between my HTTP request / transport layer protocol and my Java application.
Server architecture usually does not matter. You can use Java, NodeJs, Rails, Python, .NET, etc. You just need an endpoint that accepts a HTTP POST/PUT/GET/DELETE/PATCH verb.
This is more a question of "how do I set up a server to accept input" and its a very large topic in itself. I would advise you look at PAAS solutions like:
Parse.com
Kinvey.com
You can use their tools to build a solution fairly quickly. If you need something custom you'll need to build your backend in the language of choice and host it online via AWS, Google Cloud, Heroku, a VPS or something similar.

Java Client/Server Implementation

I have to make an university project that involves a client/server architecture.
There should be a server where a client can login and search or save some stuff.
What's the best way to implement a stuff like that?
I think it can be done using RMI or ServerSockets or even WebServices, but what's the easiest way to implement this project?
Using Web Sevrvices i think it can be troublesome the authentication/session handling, using ServerSockets i have done some tests where i pass some custom serialized objects, but It doesnt seem to me a good way to go.
Any help is appreciated
Since this is a project for university I will not post an solution, but give you an good direction.
The most basic Way (what may be a good thing for a university project, and for understanding th whole matter...) would be with the Server listening in his Mainthread on a ServerSocket for Requests to connect to the Server and then for every (correct) Request (you need to specify somehow what is correct in this case) starting a new Thread with a Socket connected to the Client. This Threads should be hosted in some sort of List or whatever in the Mainthread of the Server...
Update:
So if this Server provides different functionalities to its clients, which are of course methods in our Server Code, you can specify the Objects which are crated when a new Client connects (I'm calling these "ClientServerConnection" from now on, and which run in its own Thread) in the Way that the Server Object is passed to it, so if one of the "ClientServerConnection"s get a Request for whatever they can call the matching method on the Server-Object and give an according response to the client...
Here some pseudo-code:
in Server:
//request for Connection came in
ClientServerConnection csc = new ClientServerConnection(this, "and everything you need, at least client IP for connecting the socket");
csc.run(); //running in its own thread, of cause ClientServerConnection should extend Thread
connectionList.add(csc); //a list of the connections the Server holds
in ClientServerConnection:
//A request to the use a functionality of the Server come in, in the easiest way you are sending a String, and than trying to match it here
if(recievedString=="doWhatever"){
Server server.doWhatever(); //calling the according method on the Server Object you passed by creation of the ClientServerConnection Object
//now return something to the client, according to whatever the Method did
}else if(recievedString=="doSomethingElse"){
//same again, according to whatever the now requested method does
}else{
//the client requested something you do not provide, need some sort of handling here
}
Hope I got you right and this helps...
'Easy' is a subjective thing, depending on what you already have experience on.
If you have experience in Java related technologies, you could pick a tech stack like Servlets, JSP and JQuery, and use GAE to keep things simple from the 'troublesome' aspects you mentioned. GAE is a platform as a service so you woudnt have to worry about those things, as google takes care of the authentication, scaling etc. You can use GAE with PHP too, if you are into that.
I think RMI is the easiest solution since you define all your interfaces and don't have to care about the protocol used to communicate.
You can also use web service with SOAP which is also a RPC (remote procedure call ) interface.
But by using Socket and ServerSocket you will learn how to build a server / client software from scratch, which is very important to know (because this is the basics).

Categories