JavaFX III learns to deal with servers (long Play Monsters upgrade)

Source: Internet
Author: User

In the previous issue we learned the usage of some basic control of JavaFX and the trigger event handling of the button, this period we learn how to interact with the server to do the data, with this opportunity I also good to consolidate what I learned, hot iron. Look at the dismal ratings for the first two episodes, and, after all, JavaFX has few people I write my, you see you, basically no one gave me encouragement, did not make me feel a positive momentum. Oh, no nonsense, return to the topic

Using FX to construct a diagram, write the relevant control processing, we have to access the server. Do related data interaction, such as user login verification, file download transmission, version update maintenance and so on, this need to understand the HTTP access protocol some basic knowledge, here I do not do more explanation, I am interested to ask degrees Niang, study room on their own

To do data interaction, there are generally two ways:

1. Direct and DB handover, do data processing, in JavaFX itself encapsulation jdbc and corresponding db to connect

2. Interface with server (backend) for background data processing


The first kind: I do not explain, there is a bit of Java basic know the operation of JDBC, as JavaFX client, we also do not want to see any of the client can at any time do not make a sound of your hometown database access, Unless you write your own program. Otherwise, do not choose this way of data interaction, security is not a general danger, in short, with caution

We explain the second, I use SSH to do the background architecture, provide the corresponding service interface to FX access, the use of HTTPS data interaction, do not know what HTTPS is, or the old saying, ask Niang, to learn the initiative to learn.

Here is my own way of encapsulating the HTTPS method works for everyone reference

Package Util;import Java.net.urlencoder;import Org.apache.http.httpresponse;import Org.apache.http.client.methods.httppost;import Org.apache.http.entity.stringentity;import Org.apache.http.impl.client.closeablehttpclient;import Org.apache.http.message.basicheader;import Org.apache.http.util.entityutils;public class Httpconnectionutil {private static final String Application_json = "Appl    Ication/json ";    private static final String Content_type_text_json = "Text/json";        public static string Dopostbyjson (string URL, string json) throws Exception {string result = NULL;        Closeablehttpclient httpclient = Closeablehttpclientutil.getinstance ();        HttpPost HttpPost = new HttpPost (URL);        Httppost.setheader ("Content-type", "application/json;charset=utf-8");        Httppost.addheader ("Content-type", Application_json);         Stringentity se = new stringentity (JSON, "UTF-8");        Here must deal with the code UTF-8, otherwise it will appear Chinese garbled se.setcontenttype (Content_type_text_json); SE.setcontentencoding (New Basicheader ("Content-type", Application_json));        Httppost.setentity (SE);            try {HttpResponse httpresp = Httpclient.execute (HttpPost);            int statusCode = Httpresp.getstatusline (). Getstatuscode ();            if (StatusCode = =) {result = Entityutils.tostring (httpresp.getentity ());        }} catch (Exception e) {e.printstacktrace ();        } finally {httppost.releaseconnection (); } return NULL = = result?    "": result; }}

The above encapsulates a post method, which consists of two parameters, the first is the URL is the path of https you want to access, the second JSON is used to interact with the server data, in JSON format, the returned parameters are also (service-side processing) and in JSON format feedback to the FX client.

This issue is basically finished, do not understand the students can leave a message to me at any time, I will answer in a timely manner, to read more than 300 times, I will update the four said .... Hey... Update fast and slow it's up to you to decide OH. 88


JavaFX III learns to deal with servers (long Play Monsters upgrade)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.