我写了一个可以通过boost :: asio连接到服务器并以JSON数据形式获取响应的类,当我尝试获取其他JSON时,它可以响应相同的数据。该如何解决?
CargosAPI::CargosAPI(const std::string& address, const int& port) :
_address(address), _port(port), _sock(_service)
{
//// Class constructor for connection necessary set ip address anp port on which server deployed.
_sock.connect({ip::address::from_string(_address), _port});
}
void CargosAPI::substancesRequest(const int& offset, const int& limit, const std::string& application_key)
{
//// Method make request for server and give JSON response
//// A query string with the parameters offset, limit and flag, indicating whether to receive a reply with all data
/// or only with the name of the product
char stringRequest[88];
sprintf(stringRequest, "GET /api/substances?offset=%d&limit=%d HTTP/1.1\r\n\r\n", offset, limit);
std::string request(stringRequest);
//// Add request string for socket
_sock.send(buffer(request));
//// Piece record of the received data in a string
do {
char buf[1024];
size_t bytes_transferred = _sock.receive(buffer(buf), {}, _ec);
if (!_ec) _response.append(buf, buf + bytes_transferred);
} while (!_ec);
}
示例:
CargosAPI cargosAPI("127.0.0.1", 5000);
auto subs = cargosAPI.getSubstance("ACETICANHYDRIDE");
std::cout << subs.get("Density").toString() << std::endl;
auto subs1 = cargosAPI.getSubstance("ACETONE");
std::cout << subs1.get("Density").toString() << std::endl;
返回
1.08299994
1.08299994
必须返回
1.08299994
0.796999991
头文件
class CargosAPI
{
public:
CargosAPI(const std::string& address, const int& port);
CargosAPI();
std::vector<std::string> getVectorSubstances(const int& offset, const int& limit, const std::string& application_key = "");
void setConnection(const std::string& address, const int& port);
void substancesRequest(const int& offset, const int& limit, const
std::string& application_key = "");
private:
std::string _address;
int _port;
std::string _flag;
std::string _response;
boost::system::error_code _ec;
io_service _service;
ip::tcp::socket _sock;
pt::ptree _root;
std::vector<std::string> firstLineKey;
};
Cpp文件
void CargosAPI::substancesRequest(const int& offset, const int& limit, const std::string& application_key)
{
//// Method make request for server and give JSON response
//// A query string with the parameters offset, limit and flag, indicating whether to receive a reply with all data
/// or only with the name of the product
char stringRequest[88];
sprintf(stringRequest, "GET /api/substances?offset=%d&limit=%d HTTP/1.1\r\n\r\n", offset, limit);
std::string request(stringRequest);
//// Add request string for socket
_sock.send(buffer(request));
//// Piece record of the received data in a string
do {
char buf[1024];
size_t bytes_transferred = _sock.receive(buffer(buf), {}, _ec);
if (!_ec) _response.append(buf, buf + bytes_transferred);
} while (!_ec);
}
std::string CargosAPI::responseString()
{
//// Return response string if it is JSON, else error message
size_t pos = _response.find("Content-Type: application/json");
if (pos != std::string::npos) {
size_t pos_s = _response.find("[\n {");
std::string returnString;
if (pos != std::string::npos)
returnString = _response.substr(pos_s);
return returnString;
}
else
return "Error, No JSON response";
}
std::vector<std::string> CargosAPI::getVectorSubstances(const int& offset, const int& limit, const std::string& application_key)
{
substancesRequest(offset, limit, application_key);
//// Method Returns list all requested substances. For correctly work call CargosAPI::substancesRequest first
std::string jsonString(responseString());
std::vector<std::string> listSubstances;
if(jsonString !="Error, No JSON response")
{
std::stringstream jsonStringStream;
jsonStringStream << jsonString;
//// Function reading JSON required path to JSON file or stream.
pt::read_json(jsonStringStream, _root);
BOOST_FOREACH(pt::ptree::value_type const&v, _root)
{
BOOST_FOREACH(pt::ptree::value_type const&vi, v.second)
{
if(vi.first == "product_name")
listSubstances.push_back(vi.second.data());
//// v.first is the name of the child.
//// v.second is the child tree
}
}
return listSubstances;
}
}
主要
CargosAPI cargosAPI("127.0.0.1", 5000);
auto list = cargosAPI.getVectorSubstances(63, 10);
for(auto x: list)
{
std::cout << x << "\n";
}
auto list = cargosAPI.getVectorSubstances(0, 10);
std::cout << std::endl << "*------------------------------------------------------------------------*" << std::endl;
for(auto x: list)
{
std::cout << x << "\n";
}
输出
"ACETICANHYDRIDE"
"ACETONE"
"ACETONECYANOHYDRIN"
"ACETONITRILE"
"ACETOPHENONE"
"ACROLEIN"
"ACRYLAMIDESOLUTION"
"ACRYLICACID"
"ACRYLONITRILE"
"ACRYLONITRILE-STYRENECOPOLYMERDISPERSIONINPOLYETHERPOLYOL"
*------------------------------------------------------------------------*
"ACETICANHYDRIDE"
"ACETONE"
"ACETONECYANOHYDRIN"
"ACETONITRILE"
"ACETOPHENONE"
"ACROLEIN"
"ACRYLAMIDESOLUTION"
"ACRYLICACID"
"ACRYLONITRILE"
"ACRYLONITRILE-STYRENECOPOLYMERDISPERSIONINPOLYETHERPOLYOL"
输出应为
"ACETICANHYDRIDE"
"ACETONE"
"ACETONECYANOHYDRIN"
"ACETONITRILE"
"ACETOPHENONE"
"ACROLEIN"
"ACRYLAMIDESOLUTION"
"ACRYLICACID"
"ACRYLONITRILE"
"ACRYLONITRILE-STYRENECOPOLYMERDISPERSIONINPOLYETHERPOLYOL"
*------------------------------------------------------------------------*
"1-METHOXY-2-PROPYLACETATE"
"1-OCTADECENE"
"1-PHENYL-1-XYLYLETHANE"
"11-DICHLOROETHANE"
"111-TRICHLOROETHANE"
"112-TRICHLORO-122-TRIFLUOROETHANE"
"112-TRICHLOROETHANE"
"1122-TETRACHLOROETHANE"
"123-TRICHLOROPROPANE"
"1235-TETRAMETHYLBENZENESEETETRAMETHYLBENZENE"