I'm new in iOS development. I have a little experience in Android development and wanna to learn iOS development. In Android i use Retrofit library to access API.
And now i want to know kind of library for access API. I want to discuss about API library that have good performance, easy to use, and easy to understand. yeah of course i already try to find it and i get it :
https://medium.com/ios-os-x-development/restkit-tutorial-how-to-fetch-data-from-an-api-into-core-data-9326af750e10
But i need more idea about library for access API, can anyone help me?
Thank you.
you can Alamofire in ios.
Alamofire.request("https://httpbin.org/get").responseJSON { response in
print(response.request) // original URL request
print(response.response) // HTTP URL response
print(response.data) // server data
print(response.result) // result of response serialization
if let JSON = response.result.value {
print("JSON: \(JSON)")
}
}
Even though Alamofire might seem like a good choice for networking, the native URLSession along with the Codable protocol provide the same functionality without adding any dependencies to your project.
let task = URLSession.shared.dataTask(with: url) { data, response, error in
// Handle data, response, and error
}
task.resume()
In iOS, the only library that's ruling on networking is Aalmofire. It simplifies all your networking calls struggles. It provides simple methods to access data from the server. Alamofire is in swift. If you want to create a project in objective C, the same library available in Objective C as AFNetworking.
Below is the example of writing api calls:
let url = ""
let headers = [ "Content-Type" : "application/json"]
let para : Parameters = [ "data" : JSONObject]
Alamofire.request(url, method: .post, parameters: para, encoding: JSONEncoding.default, headers : headers)
.responseJSON { response in
print(response)
print(response.result)
}
Note: As you are a beginner I didn't tell about URLSessions(provided by Apple) which is the perfect way of writing API calls. But it's a very good choice in future.
URLSession built-in possibility
AFNetworking is an Objective-C networking library
Alamofire uses AFNetworking inside but it is written in Swift
SDWebImage image downloader with cache support
Related
I want to parse the graph Cool service in android. I am unable to parse the data.
here is the URL:
https://api.graph.cool/simple/v1/cj8dyjr0144dk33b7pz
have to parse this service
mutation {
updateLocation(
id:"cjck0maq9q7ovs54z",
lat:"16.11",
long:"81.11"
) {
id,lat,long
}
}
Please, any one has idea about that.Please help to resolve this issue.
please refer the complete document provided by Apollo graphql. it is a GraphQL compliant client that generates Java models from standard GraphQL queries.this link provides a sample application for native android.
click here
Looks like you use a tool to create classes that do the parsing: apollo-codegen
It was mentioned in the docs here: frontend quick start
So very similar to how you work with AIDL in Android.
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
Most of the backend stuff is in PHP which handle JSON request and response flow of data from Android app to backend.
I'd like to start writing Python code to handle the extra features I'm going to add in my app. How can I do that? Do I need to install Django or something like it in the backend? Our webhost does show "Python support". I'm guessing just a couple of Python classes and some helper library files would suffice.
But here's where I'm conceptually stuck:
In Android, on the app, in the user's side, suppose I send all my queries to backend with this function:
//Pseudo code on Android app
getServerResponse()
{
url = " ??? ";
jsondata = {somedata[a:b]};
response = sendData_andGetResponse(jsondata); // suppose this function sens json data and expects a server response.
showResults(response);
//Pseudo code on backend - BackendProcessing.py
def processRequest():
# some processing done here
response = "some_processed_data"
return response
My problem is, what and how do I integrate the backend Python code and the client side Android app code to communicate with each other. What should the URL be in my Android code to pass data from user to backend? How do I link them?
Do I need to specially setup some third party Python API to handle calls from the Android app at the backend? Or can I just do it with simple Python functions and classes with HTTP request and responses coming in for a particular URI?
You can include URL of the backend server in the android code. Define a variable for the URL of your backend server and use Httppost method for communication between backend and frontend.
Details here http://developer.android.com/reference/org/apache/http/client/methods/HttpPost.html
You can do it with simple Python functions and classes with HTTP request and responses coming in for a particular URI. A third party Python API is not necessary.
You can also use Python based web frameworks like Django for the backend.
I like to raise some questions regrading Yii framework and Android applications. I am going to build a mobile application in Android platform and implementing Yii framework as server side. I like to know how much Yii framework supports Android platform? Are Yii framework web services fully compatible with Android?
And, if can anyone suggest some tutorial or useful information, that will be very useful to me...
Check out this: Yii REST API
You can use JSON / XML to communicate your android application with the php yii framework by building a simple REST API.
In order to do use the REST API this, you need to send HTTP request (Get / Post) from Android application. Then perform your operation based upon the request and send again in JSON formate using php json_encode or XML if you preferred.
You can also perform it using google Gson. it will help you to create JSON from java object.
Refer this: Simple API using Yii
I have done similar recently,
Yii side
you need custom controller sending json array and you can refer Yii REST API
public function actionTest()
{
$commands = Command::model()->findAll();
$cmdlist = array();
if( $commands != '0'){
foreach( $commands as $item )
{
$object = array();
$object ['cmd'] = $item->command;
$object ['command'] = $item->getCommandOptions($item->command);
$object ['number'] = $item->number;
$object ['id'] = $item->id;
$cmdlist[] = $object;
}
$arr['command'] = $cmdlist;
}
header('Content-type: application/json');
echo json_encode($arr);
Yii::app()->end();
}
android side
You need a neat Async Http client with Json parser. I used android-async-http
I have an Android application that is on the market that I am switching to using the Yii framework. I use the http://www.yiiframework.com/extension/restfullyii/ extension. So far it's been great. I had a couple of issues with security and other things but overall the tool is what you will need. All you really care about for yii is having a restful API the rest of the logic should really be handled in your Android application. Also as a response type at this point I would only use json. I tried to implement a Android XML framework and decided to pull back because json did everything I needed to and was easy.
I don't know anything about Ruby, but I think what I'm trying to do is pretty simple. I have an app that needs to send a url query like this to a heroku database: http://dartmouth.heroku.com/dnd/search.json?query=sebastian, then receive the data that comes back and organize it for the user. How do I send and recieve a query like this?
EDIT: I downloaded Spring and added the rest template jar to my projects build path. I tried using this code:
String url = "http://dartmouth.heroku.com/dnd/" + dataBase + "json?query=" + searchContent;
RestTemplate rstTemplate = new RestTemplate();
PersonList pList = rstTemplate.getForObject(url, PersonList.class);
but "RestTemplate" is not recognized. Did I miss an installation step?
You need to start by making an HTTP request for the data and then parsing the results. I would suggest trying out the Spring Android library to accomplish this: http://www.springsource.org/spring-android
Check out the explanation here: http://mike.bailey.net.au/2011/02/json-with-ruby-and-rails/
You can use a plugin called HttpParty for sending the request. Ruby on rails will interpret the json response by using the json library. The example on the above mentioned page might make things clearer.