I have to send an image to a server (I think the best option is using HttpURLConnection) and recieve from it a String answer. In the different docuemnts and web sites that I have read, I have investigated that the best way to do this is using MultipartEntity.
1_ Is it the best solution to do it?
2_ I had an error which says that Android Studio cannot resolve the symbol 'MultipartEntity'. I read that to solve it I have to download external libraries. Which are them and how can I download them?
3_ For this I want to run this process in backGround, but I have a mistake writing the AsyncTask like AsyncTask<String, Void, String> because I want to recieve the parameters like in the answer from this question (Sending files using POST with HttpURLConnection): String urlString, MultipartEntity reqEntity. How can I do to resolve it?
I think the best option is using HttpURLConnection
Is it the best solution to do it?
Probably not. It's too low-level for multi-part requests.
I had an error which says that Android Studio cannot resolve the symbol 'MultipartEntity'. I read that to solve it I have to download external libraries. Which are them and how can I download them?
Options include, but not limited to
Apache HTTP (which is where MultipartEntity comes from)
OkHttp with a multipart example
VolleyPlus
You can get either using Gradle. For Apache see: Android - MultipartEntity and dependencies, and Okhttp just read their documentation where it says "Gradle"
For this I want to run this process in Background
Can't remember about Apache, but OkHttp already can handle that without an AsyncTask.
Related
I'm making a simple HTTP POST request using Tasker app, it's working fine but somehow it's not sending my arguments. HTTP GET is working fine. What am I doing wrong?
The only thing the server does is JSON stringify all received parameters and return them like: Text: { parameters }
HTTP GET is working:
HTTP POST works, it returns the response "text: {}" but doesn't return any parameter:
Now sometimes it shows "Bad Request Error 400"
I've tested it with another server and this issue persists...
Here's a fiddle to send get and post requests to the same server and both work.
I've exported my task as xml for you to test: You can download it here
Thanks in advance guys!
I have done a little testing with what you have supplied. It appears that your post parameters are making it to the server but %HTTPR is not getting populated. The Tasker documentation says that this variable will be populated if the response is "text-based", but this does not seem to be the case. I may not know what Tasker considers to be "text-based", but I have tried "text/plain" and "text/html" with no luck.
The work-around that I have come up with is to put the response into a file using the "Output File" field of the post task. (Leave "Content Type" blank or this won't work.) You can then read the file into a variable and do what you need to.
This is either a defect in Tasker, incorrect documentation, or just a misunderstanding of what it takes to make it work. This work-around, however, will get you what you need.
Here is a link to a GitHub repository with the updated task export. You may need to change the output file name/location to work with your device.
Update:
Since I posted the above solution, I have run across a Tasker plugin called "RESTask for Tasker". Evidently, Tasker has enough issues with HTML requests that a separate plugin was needed. I have tested this plugin with POST and it does work, so this is another way to go. The plugin is available on the Google Play store.
Hy guys!
I am working on an android project(java) with another guy working on the server-side(php). In my application I need to call POST and GET methods in order to upload files to server, download files, send Strings, byte[] array etc.
My question is: What is the best library to use in my case?(I think my files will not exceed 3mb)
I am new in android so I tried so far:
1.Android Asynchronous Http Client(com.loopj.android:android-async-http:x.x.x)
-we gave up to this because it is not from a "trusted" source
2.AsyncTask+HttpClient+HttpPost
-we gave up to this too
3.Volley library
-best so far(for strings, image request), but it needs additional libraries to send images to server(org.apache.httpcomponents:httpmime:4.5)
-I followed so examples from here but I got exceptions, error, libraries error(duplicates) and never managed to solve one without other showing up.
-so I gave up on this too
My question posted for volley library here
4. Now I am thinking about using Retrofit, but dont know it fits my needs:
-send strings and all types of primitive data
-send image/images to server(together with an Api key)
-download image/images from server
Tell me if I am wrong somewhere or if I missed something working with the libraries specified above. I managed to send simple data with all of these, but I didnt managed to send Files(excepting loopj library).
Do you think should I go back to Volley, or starting reading about Retrofit? Volley seems to be the most flexible one, but not for uploading files.
Any reference or advice is welcome! Thanks in advance!
Update:
I found a possible solution for my problem:
-I convert my file/image to a byte array and encode it to a base64 string
-I send the string to server as basic StringRequest with HashMap<String,String>(Using Volley library from Google developers)
-The server decode the string a save the file
I think a very good fit for you would be AndroidAsync.
You can find more about it on their GitHub repository here: https://github.com/koush/AndroidAsync
As an example for you on how to upload files to server:
AsyncHttpPost post = new AsyncHttpPost("http://myservercom/postform.html");
MultipartFormDataBody body = new MultipartFormDataBody();
body.addFilePart("my-file", new File("/path/to/file.txt");
body.addStringPart("foo", "bar");
post.setBody(body);
AsyncHttpClient.getDefaultInstance().execute(post, new StringCallback() {
#Override
public void onCompleted(Exception e, AsyncHttpResponse source, String result) {
if (e != null) {
ex.printStackTrace();
return;
}
System.out.println("Server says: " + result);
}
});
There is also NanoHTTPD which you can find here: https://github.com/NanoHttpd/nanohttpd
I hope this will help you.
You should try HttpURLConnection its really easy to send data to a server.
https://developer.android.com/training/basics/network-ops/connecting.html
I am making a POST call to a tomcat server running Struts2 using the retrofit library on an Android Galaxy S3 (/Nexus 7) device. The POST call fails. The tomcat log shows Socket timeout exception.
The same POST using the exact same headers done via curl does not have any issues. I verified that the data on the wire matches using charles proxy.
Any tips/ideas on debugging this issue.
The post call is as follows
#POST(Constants.URL_GET_ORDER_LIST_BASE)
void getCardOrderList(#Body GetOrderListRequest getOrderListRequest, Callback<GetOrderListResponse> cbGetOrderListResponse);
Please let me know if I need to add more information to explain this better.
Adding Square's OKHTTP library into the libs folder fixed the issue.
I was having SocketTimeoutExceptions too. Pay attention to always add the final slash to your POST call.
Example:
BAD
#POST("/customers")
GOOD
#POST("/customers/")
My mistake was just this :)
I found many examples on Stack Overflow and I just don't know where to start since I have a particular prerequisite:
My Delphi Datasnap Rest server is using ZLibCompression and RSA encryption (in TTransportFilterCollection of DSHTTPWebDispatcher). Is there an Android restful API that can handle those? Any sample that already implements that?
I found this thread Android REST client, Sample? as a good starting point, both lightweight and well-formed clients are presented but is it enough for me?
Thanks in advance for any hint, sample or library that you could point me to.
you can use the Android Rest-Client for sending data to a webservice
see my answer in the post Adding body of call to POST using HttpURLConnection
I just used the proxy generator from Danasnap server TDSProxyGenerator component (http://yourserver:port/proxy/java_android.zip) and added this code to my android project...
I had to search for the DSRESTSSLFactory.java file that is not generated by the proxy (but required !!), i found it in a demo from embarcadero, and from that point the communication is entirely handled, all accessible procedures are publicated, and really really simple to use...
This is probably a stupid question; however, I am out of options and would appreciate help. I am trying to mock file downloads using Robolectric. I am referring to the class HttpTest.java, and the Robolectric blog post.
My problem: Maven is unable to find the Http class referred to in the HttpTest.java code. I could manually bring it over as a class in the same package, but I ended up getting a NullPtrException in one of the methods of class Http.
I have searched on the net for a solution. I have also verified the modules configuration in the pom file. I do have com.pivotlabs present there.
Has anyone gotten this working? Please let me know what I am missing here.
I got this working.
Http is a class internal to Robolectric's sample code. It can be used outside; but you might not want to do that if your code is gonna be built non-locally.
The clarification/mention in documentation that would have gone long way in helping me is: "Robolectric will give you the response you create using addPendingHttpResponse() for your HTTP request, no matter how you do the request. Robolectric will NOT do an actual HTTP request." Once I understood this, rest was easy.
I used Apache's DefaultHttpClient for my request, and verified received response against the already created response.
Hope this helps; I'd be happy to clarify further.
Thanks for the help I received here.
[I had asked this question on Robolectric's google group as well.]