cocos2dx获取网络时间(二):浅析CCHttpClient(2)
来源:未知 责任编辑:责任编辑 发表时间:2015-03-01 01:33 点击:次
p>26 {
p>27 CCHttpClient::getInstance()->release();
p>28 if (!response)
p>29 return;
p>30 if (0 != strlen(response->getHttpRequest()->getTag()))
p>31 {
p>32 CCLog("%s completed", response->getHttpRequest()->getTag());
p>33 }
p>34 int statusCode = response->getResponseCode();
p>35 char statusString[64] = {};
p>36 sprintf(statusString, "HTTP Status: %d, tag = %s", statusCode, response->getHttpRequest()->getTag());
p>37 CCLog("%s", statusString);
p>38 if (!response->isSucceed())
p>39 {
p>40 CCLog("response failed");
p>41 CCLog("error buffer:%s", response->getErrorBuffer());
p>42 return;
p>43 }
p>44 std::vector<char> * buffer = response->getResponseData();
p>45 std::string str;
p>46 for (unsigned i = 0; i < buffer->size(); i++)
p>47 {
p>48 char a = (*buffer)[i];
p>49 str.append(1, a);
p>50 }
p>51 CCLog("%s", str.c_str());
p>52 readJson(str);
p>53 }
p>54
p>55 void NetTime::readJson(std::string jsonStr)
p>56 {
p>57 Document doc;
p>58 doc.Parse<0>(jsonStr.c_str());
p>59 if (!doc.IsObject())
p>60 return;
p>61 if (doc.HasMember("Year") && doc.HasMember("Month") && doc.HasMember("Day") && doc.HasMember("Hour") && doc.HasMember("Minute") && doc.HasMember("Second"))
p>62 {
p>63 _year = doc["Year"].GetInt();
p>64 _month = doc["Month"].GetInt();
p>65 _day = doc["Day"].GetInt();
p>66 _hour = doc["Hour"].GetInt();
p>67 _minute = doc["Minute"].GetInt();
p>68 _second = doc["Second"].GetInt();
p>69 }
p>70 }
p>
p> 看到这里 ,读者可能会忍不住要说了,Leandro真是一个大忽悠,如此简单的类封装都要单独拿出来写一篇!
p>
p> 各位客官莫着急,请接着向下看:
p>
p> 我们回到HelloWorldScene.cpp的init方法,注释掉前面发起网络请求的代码,接着使用我们的NetTime类:
p>
p>
p>
p>
p>
p> 1 /* CCHttpRequest * request = new CCHttpRequest();
p> 2 request->setUrl("http://localhost:23244/NetTime.svc/Time");
相关新闻>>
最新推荐更多>>>
- 发表评论
-
- 最新评论 更多>>