Sending HTTP request from Android app - android

Currently in cURL I can run a basic request like this:
curl -u username:token https://api.mywebsite.com -H "Accept: application/custom-header"
It authenticates, it picks up the custom header and everything is great.
However, I want to do this from an Android app. I had a look at HttpClient but it was deprecated at API level 22. So now I'm trying to find something else to do the job.
What is the current/best way to do this from an Android app?
Thanks

You can use Volley Library of google for the Network call check this Volley site

Related

ibm watson speech to text

anyone know how to turn this code into an api key for watson speech to text?
<!-- STT default credentials -->
<string name="STTdefaultUsername">yyyyyyyy</string>

 <string name="STTdefaultPassword">xxxxxxxx</string>

 <string name="STTdefaultTokenFactory">https://stream.watsonplatform.net/speech-to-text/api</string>
<!-- TTS default credentials -->
<string name="TTSdefaultUsername">yyyyyyyy</string>
<string name="TTSdefaultPassword">xxxxxxx</string>
<string name="TTSdefaultTokenFactory">https://stream.watsonplatform.net/text-to-speech/api</string>
it is then called below
private boolean initSTT() {
// initialize the connection to the Watson STT service
String username = getString(R.string.STTdefaultUsername);
String password = getString(R.string.STTdefaultPassword);
String tokenFactoryURL = getString(R.string.STTdefaultTokenFactory);
String serviceURL = "wss://stream.watsonplatform.net/speech-to-text/api";
SpeechConfiguration sConfig = new SpeechConfiguration(SpeechConfiguration.AUDIO_FORMAT_OGGOPUS);
SpeechToText.sharedInstance().initWithContext(this.getHost(serviceURL), getActivity().getApplicationContext(), sConfig);
// Basic Authentication
SpeechToText.sharedInstance().setCredentials(username, password);
SpeechToText.sharedInstance().setModel(getString(R.string.modelDefault));
SpeechToText.sharedInstance().setDelegate(this);
return true;
}
The Android SDK is built to work with the Java SDK primarily. The Java SDK handles most of the authentication and HTTP logic while the Android SDK just adds things on to get it to work on mobile devices. A deprecated link for it was posted above, so for reference, this is where you can find the Android SDK.
The Java SDK README is where you can find most information about getting started. For this case, you can find help in this section.
To put everything here, if you have your API key in your resources, you can do the following:
SpeechToText service = new SpeechToText();
IamOptions options = new IamOptions.Builder()
.apiKey(R.string.stt_api_key) // this is your API key
.build();
service.setIamCredentials(options);
Again, you'll need to bring in the Java SDK as a dependency for this. The latest version to add to your Gradle config is:
compile 'com.ibm.watson.developer_cloud:java-sdk:6.14.0'
The SDK will handle making the correct API calls in the backend and you should now be able to make authenticated API calls using that service object.
This might help you to authenticate yourself with IBM watson websocket handshake.
To get the authentication-token you need to run the following cURL command. This can be included in your program prior to the connection (websocket handshake).
curl -k -X POST --header "Content-Type: application/x-www-form-urlencoded" --header "Accept: application/json" --data-urlencode "grant_type=urn:ibm:params:oauth:grant-type:apikey" --data-urlencode "apikey={your apikey}" "https://iam.bluemix.net/identity/token"
You will get the token as response.
And use this token for your authentication at handshake.
Below is given how I used it for my Project in C++ using boost library.
ws_.async_handshake_ex(host_, "/speech-to-text/api/v1/recognize",[](request_type& reqHead){reqHead.insert(http::field::authorization,"Bearer {my_token}");},std::bind( &session::on_handshake, shared_from_this(), std::placeholders::_1));
Try this instead of your apikey. Don't forget to add "Bearer"
Follow this link for more details - https://console.bluemix.net/docs/services/watson/getting-started-iam.html
Youu may try doing the same in your language.

Android HTTP Methods Rest API

I am working on a project on android and I want to implement the functionality of my application on an API written in Node JS and use it with HTTP requests.
I am searching for an (open source) HTTP API (CRUD System) that I can use in my android application in order to make HTTP requests to my API (GET, POST, PUT, DELETE).
What are you suggesting me? Which are the best solutions?
Thank you
I recommend OSS "Fuel".
https://github.com/kittinunf/Fuel
I created a sample application of API request using Fuel.
https://github.com/y-okudera/FuelSampleApp
I hope this will be of some help.

Jhipster Jwt Authentication for Mobile API

I have a jHipster project with Jwt Authentication but I can't get it to work outside de webapp. I'm currently developping a Android application and the authentication process get harder than I expected.
Basically I'm sending the parameters of the LoginDTO, to UserJwtController#authorize ('/api/authenticate'). At first I was getting Unauthorized, both on Android or Postman (I'm using it to test the requests).
If I change the '/api' to permitAll, I'm getting 405, Request method 'POST' not supported.
EDIT
It was a wrong typo on Android :/
It works fine for me against /api/authenticate, so either you use wrong URL (e.g. /api/authorize) or your JSON payload is wrong. The only issue you could have is with CORS. You should consider testing with curl as it is easier than PostMan for reporting here what you really do and also the curl options are already built for you in JHipster swagger page.

Is it possible to work with Dropbox REST API without any libraries, using only GET&POST requests?

Also it is preferably no to start a web-browser for user logging-in. For instance, the ES File Manager shows this form within its own fragment.
I need my application to be very small, while most of the libraries are of a few megabytes.
You can definitely access the API without a library, but all authentication requires OAuth (which requires opening a browser). All calls to the Core API are just HTTP with the header Authorization: Bearer <token>, so any HTTP library will do. For example, this curl command will write a file called hello.txt:
curl -X PUT https://api-content.dropbox.com/1/files_put/auto/hello.txt?overwrite=false \
-H 'Authorization: Bearer <YOUR_TOKEN>' \
-H 'Content-Type: text/plain' \
-d 'Hello, World!'
See my blog posts about how to do call the Core API from the command-line with curl and how to do it from a variety of languages without using an OAuth or Dropbox library.
Yes, but Dropbox OAuth steps require signing in via the HTML representation. Source.
Note: This is the only step that requires an endpoint on www.dropbox.com. All other API requests are done via api.dropbox.com or api-content.dropbox.com.
After the OAuth process, you will receive the token and the secret. With these, you can make calls to the REST API using HTTP requests.
Here is an SO question and a nice answer discussing the Dropbox authentication with JavaScript (though using a library) :Transfer files to dropbox from node js without browser based oauth authentication

Android SOAP NTLM example?

I've tried to google around for a working example. I've tried KSOAP2 and JCIFS examples but no success. The web service i'm trying to connect is 3rd parties (Microsoft Dynamics NAV) and can't be modified. It uses SOAP and either SPNEGO or NTLM authentication. And I guess it's already NTLMv2 but I'm not sure on that.
Can anybody please suggest me a full code for the following SOAP request?
$<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:item="urn:microsoft-dynamics-schemas/page/items">
$ <soapenv:Header/>
$ <soapenv:Body>
$ <item:ReadMultiple>
$ <!--1 or more repetitions:-->
$ <item:filter>
$ <item:Field>No</item:Field>
$ <item:Criteria>1000</item:Criteria>
$ </item:filter>
$ <item:setSize>500</item:setSize>
$ </item:ReadMultiple>
$ </soapenv:Body>
$</soapenv:Envelope>
I've validated this request with soapUI and it works fine. But when I tried to run it with KSOAP2 and JCIFS I get an error message "Connection refused".
Important to note I am a beginner in Java and Android.
I have had limited experience with NAV and SOAP, however I had a similar issue with Authentication.
What I needed to do is actually run the request twice. For some reason the first request would never go through (maybe as part of a handshake?), but the second would return a valid response. The best debugging tool was a simple HTTP Proxy (such as Burpsuite) which let me see what exactly was being sent (in terms of headers etc) and what the response was.
Secondly, as I was manually creating this SOAP Request, I had missed out the SOAPAction header for NAV. I'm not sure if this is standard, but I needed to use the HTTP Proxy with a .NET Web Service connection (The official way of talking to NAV through SOAP) and discover how it was talking.

Categories

Resources