Build a restapi in android from JSON - android

Suppose I have this json file
[{"item_id":"1000","item_name":"PEn","item_quantity":"2.66","item_rate":"3.69","item_purchase_date":"2020-05-13T00:00:00Z"},
{"item_id":"1004","item_name":"box","item_quantity":"63","item_rate":"20.5","item_purchase_date":"2020-06-12T00:00:00Z"}]
I want to make a rest api using this json file in Android. This json file should be locally available in my android device. When I send a certain request (GET/DELETE/etc.) to a certain URL (localhost:3000/posts), I want to perform requested operation.
Please show me how can I achieve this.
UPDATE 1: I don't need NanoHTTPD. (Its better if I can find something like golang with postgres)

To serve an api on an android device, you would need a webserver. The easiest way would be to extend a Java HTTP server and add your own logic.
Try NanoHTTPD, you can easily use it in android.

Related

How to load data in my Android app from online resource where I can add/update content easily?

I want to update content/news in my app based on any online resource that's possible, a website, database or something similar where I can manually update/add content. The underlying purpose is to connect my app to that online resource, and reflect changes in my app content, whenever I update that online resource.
Right now I don't know what should be that online resource and how should I connect it with my app.
I would appreciate anyone coming up with any form of suggestions/solution.
Updating App Content means retrieving data which is stored on server in some database.
We can store and retrieve data or any content with the use of Web Services.
With the use of Web services you can parse the data and display it in your application.
You can also develop an Admin Panel for managing all your resources which will be then reflect to in your app.
These above things are possible using Networking libraries like OkHTTP, Volly, Retrofit etc. which gives you response in form of JSON (JavaScriptObjectNotation).
For Refrence Please visit :
OKHTTP > https://square.github.io/okhttp/
VOLLEY > https://developer.android.com/training/volley
RETROFIT > https://square.github.io/retrofit/
Lookup OkHttp library by Square. Super easy way to connect to an API. Then you can check out rapidapi website for a collection of APIs
If you have an Api endpoint you can use Retrofit to get data. However if you dont then i belive you will need to make server using php and then connect to that server using Volley/OkHttp3 in order to get data.

How does android retrieve data from a website?

I'm new to android, I'm trying to build an android app that is a front for a web portal. For example, Airbnb. They have a website, but they also have an android app that, using it's own layout, will show listings from their website.
There are many websites that teach how to or even directly convert your website to android apps. However, this will result in an app that loads too slowly and is unresponsive due to CPU usage.
Could anyone share any tutorial/guide to learn how to do this myself?
Million thanks.
To actually load data from a web server you're gonna need and API which usually delivers the proper date using JSON or XML format so that you can properly parse and display that data. Building this API is in it self a complete course on its own.
But connecting to and requesting data from the API is usually done using some networking libraries. These are some of the better know libraries for this purpose.
OkHttp: A complete library with a set of tools for handling network connections and HTTP requests.
RetroFit:Type-safe HTTP client for Android and Java by Square, Inc. which is built on top of OkHttp.
Async-Http-Client:
The Async Http Client library's purpose is to allow Java applications
to easily execute HTTP requests and asynchronously process the HTTP
responses. The library also supports the WebSocket Protocol. The Async
HTTP Client library is simple to use.
There tons of other good libraries.
its called webservices
Through android you get data in form of json from a web server and then return in custom view as you want.
Follow this link hope it will help
Step by Step Method to Access Webservice from Android
you would have to write an API/Web service or use if already exits to fetch data from web server. Basically the concept is that, the website itself must be pulling data from some database, so write an API which would fetch the data from same API and return JSON data and consume the API from your android app.
If you know PHP refere to this for the help :http://www.codeproject.com/Articles/267023/Send-and-receive-json-between-android-and-php
You can write WebService, in programming it generally refers to a web page(ex. Airbnb), that can be called from your android application which can pass in data to it, or receive data from it.
WebService is basically like a 'method' or 'function' in a normal programming language; except you're calling it over the internet.
The first thing is you have to create a Web Service. The Web Service will be your "bridge" to consume the data from other Website like airbnb or others and return the data to your android through json format for example.
You can create a Web Service using many languages like C#, Java, PHP, etc. I would like to recommend you to use the language that you know the most.
You can try to google this
Cheers

How to get some simple data by Internet in Android

I need to get some simple data (actual currency values) to my android application. I'm not sure what is the best way to achieve it. Do I have to make some database and put it on server and than update it manually or there is some better way?
You can use an API Service and access to this service from you android device. You donot need to do a server implementation for this.
e.g. for exchange rates check https://openexchangerates.org/
Yes.
Please make a server and a PHP which going to print out the values in JSON. You can validate your json with http://jsonlint.com/. After it you can download and parse the json with yourself or a 3rd part library (like retrofit).
You can use this website to generate POJO, so retrofit can parse the json for you by it`s own.
http://www.jsonschema2pojo.org/
select: JSON instead of scheme and select GSON instead of JSON, if you are using retrofit.
sources:
https://en.wikipedia.org/wiki/Gson
https://github.com/square/retrofit
http://blog.robinchutaux.com/blog/a-smart-way-to-use-retrofit/

Android web app

What are the best ways to connect site and show it's data on an android application ? Also does I have to create anything on server where the site is for using JSON ? I am new to programming web android application's, though I searched a lot I didn't find anything which would explain me straight to the point.
You're on the solid ground starting out using JSON as the interchange between the two.
Alot of popular mobile apps like Twitter and Foursquare have restful APIs set up to interact with their mobile clients by exchanging HTTP requests that contain data formatted as JSON. Most of the communication between the two can be accomplished with HTTP requests using the standard GET and POST methods.
A good place to start would be setting up some server endpoints that output this data and then setting up your android app to request and parse this data just like a browser would. You just need to set the appropriate mimetypes on your server end (application/json).
Most modern server-side languages have implemented modules/functions that can take their native data structures and approximate them in serialized JSON (PHP's json_encode(), python's json.dumps() etc) These can be used to output data from within the app or database to your mobile client where it can be interpreted and used in the Java environment there.
To pass back JSON you need to set the mime type (http://stackoverflow.com/questions/477816/the-right-json-content-type), which is application/json.
If you are passing back JSON or XML then the client just needs to make the appropriate http call, most likely GET, perhaps POST, to actually retrieve the information.
You can use something like this as a starting point:
http://lukencode.com/2010/04/27/calling-web-services-in-android-using-httpclient/

How have to be a Webservice for use it with Android?? (limitations, paramters, return... etc)

i have a android APP that needs a remote Database, to do INSERT's and SELECT's.
I will use a webservice to comunicate with the remote database, the webservice will be installed in the server side, with the remote database, and webservice will do the SELECTS and the INSERTS and will return me XML data. But i dont know anything about webservices, then a friend will do it.
But my friend need's to known if there is any limitations making webservices for Android. I search on google for days but all i am finding is very complex and i can't find the answer to these three questions:
Webservice needs to be created with a special way?
Webservice remote functions can have parameters? (i need to give to the webservice parameters to do the select's or the inserts of data)
Webservice can return results in XML normally or needs a special way to do it?
Also, if i have to know something more, please tell me
thanks
Regarding HTTP networking Android can basically do what plain Java can do. There are a few limitations:
XML parsing/generating is pain. AFAIK, there is no automatic XML-object mapping library (JAXB) for Android. For this reason it's best to go with REST/JSON. Jackson does ok on Android.
Yes they can.
Any XML is ok, but look at 1.
My suggestion: go with Restlet - they have clients for all Java platforms (servlet, android, gwt, j2se).

Categories

Resources