Firstly , this is a java program but not Android.
I write a very simple API to get the DB data in Php like below code:
<?php
$query = "Select * from staff order by staff_name";
$reuslt = $mysqli->query($query);
echo json_encode($reuslt->fetch_object());
Suppose that i can get the JSON object in the following URL easily in Android or Php:
http://localhost/Testing/simpleWebService.php
But how can i get it in Java??
I have searched it in Google but the result seems not what i actually want and useful to me
(Reason is i am quite Junior to Java ,Lack of knowledge in Web Service, not accurate keywords inputted........).
I found sth like Jersey, JAX-RS but i don't think this help and the way to get the JSON from the Web Service is complex like that.
Can anyone help ? Thank you very much!!!!!!
The crux of the matter is that you have to replace the behaviour implemented by PHP's json_encode function in Java ie. you have got to find a way to convert the recordset representation of your data to JSON.
1 approach would be to manually convert your recordset to a collection of POJO's then use Jackson to generate the JSON.
However, there are loads of frameworks that make this easier for you to achieve.
What Java framework are you using as that will constrain your future decisions to some degree? How are you reading from your DB - using JDBC API or using an ORM or other library?
Related
I am trying to use the REST API of vSphere to get information about our virtual machines. I already connected to the server using the provided REST API and
I got a JSON Result from a REST request. The vSphere JSON result looks like this:
{"value":[{
"hot_add_increment_size_MiB": 8,
"size_MiB": 1,
"hot_add_enabled": false,
"hot_add_limit_MiB": 51258
}]
}
Now I want to convert this JSON String to an appropriate Class.
The problem, I am actually facing is, that it can be a very complex JSON result.
My question is: Is there an API from VMWare, I can use to solve my problem?
Every hint is appreciated.
Many thanks in advance.
The answer is no. There is schema published by VMware. It might not exist.
You may be able to get what you want with this API (disclaimer: I am the author):
https://github.com/DoubleCloudInc/vim-rest-api/
The schema is the same as vSphere API, so you can use the vijava data objects as your Java objects. http://vijava.sf.net. Again, I am the author.
I am looking to use this JSON interface:
https://translate.yandex.net/api/v1.5/tr.json/translate ?
key=<API key>
& text=<text to translate>
& lang=<translation direction>
& [format=<text format>]
& [options=<translation options>]
& [callback=<name of the callback function>]
More info
It returns a JSON object. How do I get that JSON object in Java?
I know there already is an implementation for that exact API, but it's old and not working anymore.
I've had good results using google's gson library. Really this depends on what you are doing with the JSON data - is it a rest payload, JMS message or what? A lot of stuff understands JSON natively now so don't reinvent the wheel.
Yandex.Translate API documentation can be found here:
With the API, you can access the online machine translation service Yandex. It supports more than 60 languages and can translate single words and whole texts. This API allows you to embed Yandex.Translate in a mobile application or web service for end users. Or, to translate large volumes of text - such as technical documentation.
There's a lot of info out there for a newbie like myself when it comes to webservices, I acknowledge that.
However, most of the Google results I've seen tend to be focusing on a specific format or strategy, and all of them different to each other.
As a newbie, I am looking to get more of an overview of the various options open to me, their pros & cons... before I start committing to a specific one.
For example, I have an existing webservice created from a SQL Server source via ASP.net and this by default comes out as a DiffGram. So there are articles that tell me how to parse the diffgram data coming into my Android app, but I still do not know if the diffgram is the best option I should be using in the first place.
Should I, for example be changing my webservice to output JSON ?
Does anyone know of a 101 level tutorial or explanation out there ?
Many Thanks
DiffGram is just another type of XML format. In order to parse the XML data in Android, simply use the XMLFullParse class.
FYI: http://developer.android.com/training/basics/network-ops/xml.html
However if you just want to create a simple data exchange WebService, Json is definitely a better choice. Json is simple, lightweight, easier-to-parse and surely easier to use.
Example:
JsonObject json = new JsonObject(jsonString);
json.put("name", "Michael");
json.put("age", "18");
String name = json.opt("name");
I want to have some data stored in Device in order my application to work offline. They are plain java objects that are returned from a JSON web service. I use retrofit + GSON . The issue is how to store them.
WHAT I have tried
Tried to use Serializable interface at my java classes and during my sync() method when I fetch them from webservice I store them at a file. Performance is a bit poor at slow devices during sync. Not much but it is an issue.
WHAT I attempted
I attemted to store JSON from Response Retrofit. However it seems a bit of pain. Because it doesnt return as string the result but as InputStream. Then I have to convert back again.
WHAT I didnt do
I didnt try SQLite and any ORM. Seems overkill to me for 7 simple list of readonly List of objects. And I guess that performance is going to suffer more.
In IOS core data is easy to achieve this out of the box. Is there a solution for this in android ?
Best approach to this is to write the JSON out to a file using one of the near infinite number of free libraries or code samples, such as: http://www.mkyong.com/java/json-simple-example-read-and-write-json.
JSON stands for JavaScript Object Notation. But how come languages like php, java, c etc can also communication each other with json.
What I want to know is that, am i correct to say that json is not limited to js only, but served as a protocol for applications to communicate with each other over the network, which is the same purpose as XML?
JSON cannot handle complex data hierarchies like XML can (attributes, namespaces, etc.), but on the other hand you don't get the same overhead with JSON as you get with XML (if you don't need the complex data structures).
Since JSON is plain text with a special notation for JS to interpret, it's an easy protocol to adopt in other languages.
It is easy for a JS script to parse JSON, since it can be done using 'eval' in which the JS enginge can use its full power.
On the other hand, it is more complicated to generate JSON from within JS. Usually one uses the JSON package from www.json.org in which an object can easily be serialised using JSON.stringify, but it is implemented in JS so its not running with optimal performance.
So serialising JSON is about the same complexity using JS as when using Java, PHP or any other server side language.
Therefore, in my opinion, JSON is best suited when there is asymmetry between produce/consumer e.g. a web server that generates a lot of data that is consumed by the web application. Not the other way around.
But! When one choses JSON as data format it should be used in both directions, not XML<>JSON. Except for when simple get requests are used to retrieve JSON data.
yes, JSON is also wildly used as a data exchange protocol much like XML.
Typically a program (not written in JavaScript) needs a JSON library to parse and create JSON objects (although you can probably create them even without one).
Your right - it's a light weight data interchange format -- more details at: http://www.json.org
You are completely correct. JSON definition of how data should be formatted. It is more light weight than XML and therefore well suited to things like AJAX where you want to send data back and forth to the server quickly.