Skip to content

Instantly share code, notes, and snippets.

@qi7chen
Created December 10, 2014 07:44
Show Gist options
  • Save qi7chen/e29589b70c31eaec967e to your computer and use it in GitHub Desktop.
Save qi7chen/e29589b70c31eaec967e to your computer and use it in GitHub Desktop.
poco http for jni
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class tutorial_myfirstapp_MainActivity */
#ifndef _Included_tutorial_myfirstapp_MainActivity
#define _Included_tutorial_myfirstapp_MainActivity
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: tutorial_myfirstapp_MainActivity
* Method: getHttpResponse
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_tutorial_myfirstapp_MainActivity_getHttpResponse
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
//#include "tutorial_myfirstapp_MainActivity.h"
#include <iostream>
#include <sstream>
#include "Poco/Net/HTTPClientSession.h"
#include "Poco/Net/HTTPRequest.h"
#include "Poco/Net/HTTPResponse.h"
#include "Poco/StreamCopier.h"
using namespace std;
using namespace Poco;
using namespace Poco::Net;
std::string getHttpResponse(const char* url, int port = 80)
{
HTTPClientSession s(url, 80);
HTTPRequest request(HTTPRequest::HTTP_GET, "/index.html");
s.sendRequest(request);
HTTPResponse response;
std::istream& rs = s.receiveResponse(response);
std::ostringstream ostr;
StreamCopier::copyStream(rs, ostr);
return ostr.str();
}
JNIEXPORT jstring JNICALL Java_tutorial_myfirstapp_MainActivity_getHttpResponse(
JNIEnv* env, jobject thisObj, jstring jurl)
{
const char* url = env->GetStringUTFChars(jurl, NULL);
std::string response = getHttpResponse(url);
env->ReleaseStringUTFChars(jurl, url);
return env->NewStringUTF(response.c_str());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment