Use curl and libjson for networking and data parsing

Source: Internet
Author: User

Address: http://my.oschina.net/cocosgame/blog/71181

Compiling and using libjson-3. libjson C interface API

Http://blog.csdn.net/laogong5i0/article/details/8225936

The project is connected to the Internet. I chose to use the curl library to connect to the Internet. I checked the curl on the Internet. It would be quite simple to simply connect to the offline network and test data or something, but to use the game, further encapsulation is necessary.

Since C ++ is not very familiar with it, it is necessary to estimate the key time for completing the work.

------------------------------------------------------------------------------

Because there is still some work to be done in the previous project, the time is delayed.

The new project needs to solve the problem of network connection and data parsing. Currently, curl is used to solve the problem of network connection. The transmitted data is in JSON format, and the client uses libjson for data parsing.

First, we can solve the networking problem. cocos2dx comes with the curl library, which is easy to use. You can directly import the header file curl. h on Win32 (note the path). In Android, you can also add the curl library in the Android. mk file. For details, refer to the tests example.

Next, how can I use curl to connect to the background?

Specific can look at this link: http://blog.csdn.net/jgood/article/details/4787670, (Chinese)

There is the curl built-in documentation: http://curl.haxx.se/libcurl/c/libcurl-tutorial.html (English)

Since I started using curl, I am not very familiar with many usage, but I simply use the get and post methods to interact with the background.

After a few minutes, I finally got the data from the background, but when I transplanted it from Win32 to Android, the first problem was that when I added the curl library, I didn't know how to add it. Later I remembered that it was useful to curl in tests, so I copied the method. however, you still cannot get the data and need to open the permission for Android networking. Data can be obtained smoothly on Android.

After the network is connected, the following is data parsing.

First, search for information related to cocos2dx C ++ JSON on the Internet. You can see this link: Response
And copy the source folder to the class directory of the cocos2dx project. note some Macros in jsonoptions. h. One is: # define json_library, which specifies that libjson uses C language. After commenting on it, it becomes C ++. For details, I like it. There are other macros that indicate whether the version is debug or nodebug. Let's take a look at the source file. According to the libjson tutorial, The JSON string is successfully parsed, and the time spent parsing the JSON array is discarded. You need to traverse each array element and store it in a C ++ array. By now, Win32 is available for offline networking and data parsing. Some problems also occur when porting libjson to Android. The first is the compilation problem. When compiling libjson with ndk, you need to capture C ++ exceptions and add local_cppflags to the Android. mk file.
+ =-Fexceptions, this question has been entangled for a while.

Well, now both Win32 and android can connect to the Internet and parse data.

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.