How to perform a simple HTTP request in Kotlin/Android? - android

I am quite new to Android app development and would like to perform a simple API request via HTTP to a web server. URL("http://www.myapi.com").readText() seems to be a good way to start with, but this leads to a NetworkOnMainThreadException. So it must run asynchronously or in a coroutine. However, I'm not familiar enough with this and other examples I have found so far don't work for me.
Thanks a lot already for your help!

if you are using mvvm pattern you can do the following in your ViewModel class
viewModelScope.launch {val response = withContext(Dispatchers.IO) { your request }}

HTTP requests are no longer allowed by default on Android 9 and above.
Consider using HTTPS, or modify the AndroidManifest.xml, see this answer on StackOverflow.

Related

http request with Fuel

I am trying to do HTTP request with Fuel library in Android kotlin and I want to request Synchronously. But I can't get data with the below code. Does anyone know why? or Fuel library don't have sync functionality?
val hoge = "https://xxxxx.com/id/1".httpGet().response()
println(String(hoge.third.component1()))
Fuel can perform a request in synchronized way. Your code is perfectly fine and it works for me.
Make sure your server works fine and in case you are on android that you have added necessary permission to AndroidManifest.xml.
<uses-permission android:name="android.permission.INTERNET" />
Apart from this, your code can be improved. Notice that response() method of Request class will get you ByteArray. If you need string you should use:
"https://xxxxx.com/id/1".httpGet().responseString()
println(hoge.third.component1())
The newest version of Fuel library allows you to do it in even better way using kotlin coroutines. You can mark your function as suspend and call awaitString(). The thread will be blocked until you get the response.
"https://xxxxx.com/id/1".httpGet().awaitString()
println(hoge)

Tasker App: Why my HTTP POST is not sending parameters?

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.

How to test volley POST?

I want to test POST request on android. I don't have a web host. There are some android-volley request example in here.
How can I find a free url to allow post the requestes?
There a few i can think of :
https://httpbin.org/post
http://requestb.in/
www.posttestserver.co
I am sure there are more.
These are good for general experiments, however I would use something more customizable and locally installed like https://nodered.org/ and for actual tests something that actually runs on the device with the tests like:
MockWebserver
RESTMock
wiremock - example
Custom solution mocking the HttpStack(the last point before flushing the data to the wire)

Android + Espresso + async HTTP request -> how to test?

i'm trying to test Android app using Espresso FW and I stucked on the problem with doing async HTTP request.
How can i test the case that app (activity) is waiting for processing request and after the response is received is displayed next activity with result or error message?
I tried to find any solution how to do it in the Espresso with testing against the data from the test server, but without luck.
Many thanks for any advice, example or link.
I struggled with this for a few days. If your app is using retrofit to handle HTTP requests, you can add this one line:
.setExecutors(AsyncTask.THREAD_POOL_EXECUTOR, new MainThreadExecutor())
to your RestAdaptor.Builder. This moves all of the HTTP requests into the AsyncTasks pool which is then handled by espresso.
I found the original answer here: http://www.michaelevans.org/blog/2015/08/03/using-espresso-for-easy-ui-testing/
I think the problem is in app architecture.
You should use some Dependency Injection library to inject mock service with mock data in your application in test mode.
When you inject "test" implementation ( mocks) when you testing. U will avoid problems with not network availability or you can simulate network availability by your test implementation.

Android Rest client to Delphi XE2 Datasnap Rest server

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...

Categories

Resources