Skip to content
Snippets Groups Projects
Commit b42a6344 authored by Bilal Talhaoui's avatar Bilal Talhaoui
Browse files

Upload the json on the web server

parent 40400e70
No related branches found
No related tags found
No related merge requests found
......@@ -41,6 +41,11 @@
<artifactId>jackson-databind</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.4</version>
</dependency>
</dependencies>
<properties>
......
......@@ -39,7 +39,14 @@ public class Main {
// Convert object to JSON string and pretty print
String json_string = mapper.writerWithDefaultPrettyPrinter()
.writeValueAsString(analyze_result);
System.out.println(json_string);
Uploader up = new Uploader(json_string);
up.initialize();
try {
up.post();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (JsonGenerationException e) {
e.printStackTrace();
......
package rucd.monitoring.client;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
public class Uploader {
private String json;
private HttpClient httpclient;
private HttpPost httppost ;
private String url = "http://192.168.0.70/Laravel/public/api/sensors";
public Uploader(String json) {
// TODO Auto-generated constructor stub
this.json = json;
}
public void initialize() {
httpclient = HttpClients.createDefault();
httppost = new HttpPost(url);
}
public void post() throws Exception {
List<NameValuePair> params = new ArrayList<NameValuePair>(1);
params.add(new BasicNameValuePair("content", json));
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
try {
System.out.println(instream.toString());
} finally {
instream.close();
}
}
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment