http request with Fuel - android

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)

Related

How to perform a simple HTTP request in Kotlin/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.

Verify http requests made by android app while running espresso

While running UI Espesso tests I would like to also verify if the app is making correct http requests (e.g triggered by a button press). Currently using OkHttpClient client to make the requests.
Did you try something? I like to use Wiremock to mock http requests and check that the app have done the correct request. You must use the standalone version or you will get some issues with HttpClient.
For more information see http://wiremock.org/index.html

Android Loopj AndroidAsyncHttp mocking

is there are any way to mock Loopj AndroidAsyncHttp responses?
I am developing application which depends on REST API, but API is not ready yet and I want to develop application independently from API. Retrofit has a lot of mock implementations, but I was not able to find any solutions for AndroidAsyncHttp. Is there are any way?
I tried to send success message to handler on request creation handler.sendSuccessMessage(200, new Header[1], new byte[1]); , but onSuccess or something else is not called.
As example, you may create on any hosting php file with responsing json.
Or you can use my template for this cases http://so.five-dots.ru/api-test.php
Make responsing on this address and implements your logic. Then RESTfull API backend service will be ready, you can replace adresses on real and getting responseBody will reqirement data.

Android Retrofit Causes Socket Timeout Exception

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 :)

Restlet createQuery fails with connection error (and after workaround NoSuchMethodError)

I followed the Android restlet tutorial and tried to consume a sample OData webservice (http://services.odata.org/OData/OData.svc/). When I create a query with myService.createProductQuery("/Products"); logcat tells me that
ERROR/org.restlet(700): Can't get the metadata for ...
A problem similar to this question. I did try and register the Apache client as suggested by Jerome Louvel:
Engine.getInstance().getRegisteredClients().clear();
Engine.getInstance().getRegisteredClients().add(new HttpClientHelper(null));
Unfortunately I get a new error. On Query initialization I get a NoSuchMethodError exception. The Query class seems to want to call MyService.getMetadatada(). From the docs I see this is a protected method on org.restlet.ext.odata.Service
Happens on version 2.0.11 and 2.1rc3 of Restlet.
Additionally I also tried a home-made .NET 4.0 WCF webservice, with the same results. Any help would be appreciated.
For future readers that come upon this question: just make double sure your Manifest file sets permission on your application to access the internet. (For shame). Obviously it needs access to the network to "call" the webservice.
..
<uses-permission android:name="android.permission.INTERNET"></uses-permission>

Categories

Resources