Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to make a post request in java.Maven with http post to an Elasticsearch server. This is my code:

package com.server.java.Webping;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class ReadAndWrite extends Server {
    public static void main(String[] args) throws IOException {
    try {
        URL url = new URL("http://192.168.1.126:9200/shakespeare/_bulk?pretty' --data-binary @shakespeare.json");
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/json");
        File file = new File("shakespeare.json");
        FileReader reader = new FileReader(file);
        @SuppressWarnings("resource")
        BufferedReader br  = new BufferedReader(reader);
        String input = null;
        StringBuilder builder = new StringBuilder();
        while(br.readLine( ) != null) 
        {
            String txt = br.readLine( );                                     
            builder.append(txt);
        }                                
        input = builder.toString();

        OutputStream os = (OutputStream) conn.getOutputStream();
        os.write(input.getBytes());
        os.flush();

        if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
            throw new RuntimeException("Failed : HTTP error code : "
                + conn.getResponseCode());
        }

        BufferedReader br1 = new BufferedReader(new InputStreamReader(
                (conn.getInputStream())));

        String output;
        System.out.println("Output from Server .... \n");
        while ((output = br1.readLine()) != null) {
            System.out.println(output);
        }

        conn.disconnect();

    } catch (MalformedURLException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    }
}
However, now I want to make a graph representation of my data being inserted by the above code. Can anyone help me out on how should I code further for getting it graphed?


What I have tried:

package com.server.java.Webping;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class ReadAndWrite extends Server {
public static void main(String[] args) throws IOException {
try {
URL url = new URL("http://192.168.1.126:9200/shakespeare/_bulk?pretty' --data-binary @shakespeare.json");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/json");
File file = new File("shakespeare.json");
FileReader reader = new FileReader(file);
@SuppressWarnings("resource")
BufferedReader br = new BufferedReader(reader);
String input = null;
StringBuilder builder = new StringBuilder();
while(br.readLine( ) != null)
{
String txt = br.readLine( );
builder.append(txt);
}
input = builder.toString();

OutputStream os = (OutputStream) conn.getOutputStream();
os.write(input.getBytes());
os.flush();

if (conn.getResponseCode() != HttpURLConnection.HTTP_CREATED) {
throw new RuntimeException("Failed : HTTP error code : "
+ conn.getResponseCode());
}

BufferedReader br1 = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));

String output;
System.out.println("Output from Server .... \n");
while ((output = br1.readLine()) != null) {
System.out.println(output);
}

conn.disconnect();

} catch (MalformedURLException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}
}
Posted
Comments
Richard MacCutchan 13-May-17 4:35am    
What is the question?
Member 13197418 14-May-17 23:39pm    
However, now I want to make a graph representation of my data being inserted by the above code. So can u suggest me how shall i do it ?
Richard MacCutchan 15-May-17 2:51am    
You do it by writing some code that plots the points on a screen or printer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900