How to upload a file in android using AsyncTask - java

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

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

Android HttpURLConnection connection refused after few minutes in service when ui is not visible

I am working on small chat application which is working fine when the app is visible to user. A service class which get and send data using post every 5 second when aap is visible to user and works fine. But when app.is closed and service works in background for few minutes fine. After few minutes i.e. apprx 3 minutes it get conmection refused error and never fetch data from server until.the app is again visible to user. I cant find any solution. please help.me
I uses HttpURLConnection for posting data, and a Thread and timer for regular posting.
Android suspend applications when they get into background, to make resources available for the foreground application. If you need to sync your data on the background you should use a Background Service or SyncAdapter.
Take a look at Best Practices for Background Jobs and Transferring Data Using Sync Adapters
Some tips:
Do not use manual timed HTTP requests for chats messages, its a very bad practice,
But, if you want to keep this, use some new http request library like "Volley" or "OkHttp" (this is my favorite),
If you want more professional and highly "best practice" stuff, use the Google's Firebase Cloud Messaging for chats apps, its use native google services for send messages to others apps, highly recommended.
Connection Refused can be your client (service) sending wrong data/values, wrong URL, wrong ports and wrong query, please post a piece of your code.

How to call a method in java web service?

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.

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.

What is the established way to request data from an android device

I currently have an small application that I have been using to learn java/android programming. Right now I have a setup were the app on one phone sends a request (via sms) to another phone running the same app. The remote phone receives the request and sends back some info. Next I would like to try this from the web. Is there an established "best" way to to this?
I was thinking I would have a web server send requests to the device via google cloud messaging and then have the device return the data directly to the web server. (Not that I really know how to do any of that just yet).
I see that there is a google cloud messaging return path (send messages from the device to the google cloud server, but it seems very new, do I need something like that? The main thing I want is to be able to ask the phone to do something when I want, not have it poll to see if there is a request, or just periodically update some status.
UPDATE:
Thanks to the answers below for confirming to me that I was on the right track.
I now have some basic functionality.
I started out using this gcm android demo code
https://code.google.com/p/gcm/source/browse/#git%2Fgcm-client%2Fsrc%2Fcom%2Fgoogle%2Fandroid%2Fgcm%2Fdemo%2Fapp%253Fstate%253Dclosed
and this ruby gem
https://github.com/spacialdb/gcm/blob/master/README.md
between the above two I was able to send a message to my phone pretty easily.
To get the round trip working, I setup a very simple rails app on heroku.
I included a modified version of the sample code in the gcm gem in a controller and then used
HttpPatch (needed for rails 4) to send a post/patch from my phone to my web app, the controller then echoes the message back to my phone.
I guess it would be nice to get the two way gcm stuff to work, but I am not sure there are any gems that handle that, and I am not qualified to handle a task like that :)
I would say it's the right call: Google Cloud Messaging for Android
From the site Android Developer:
This could be a lightweight message telling your app there is new data
to be fetched from the server (for instance, a movie uploaded by a
friend), or it could be a message containing up to 4kb of payload data
(so apps like instant messaging can consume the message directly).
In this case you don't want to fetch data from the server but you want to send them.
You can send them in different ways. I would suggest, since you are learning, to try a RESTful solution using one of the implementation of JAX-RS.
As a short and direct answer for beginner : GCM (Google Cloud Messaging) would solve your issue. However, if your app turned out to be something bigger, other more technical and complicated solutions are present too.
see this link.

Categories