0

I am trying to make a java program that could I would give her a string, it would paste it to Hastebin(www.hastebin.com) and print the paste's URL to the console and this is the code I have: Main.java(Main class):

public class Main {
    Hastebin hastebin;
    static String randomString = "HELLO";

    public static void main(String args[]) {
        System.out.println(Hastebin.paste((randomString)));
    }
}

My Hastebin.java class:

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;

public class Hastebin {

public static String paste(String content){
    final HttpClient client = HttpClient.newHttpClient();
    final HttpRequest request = HttpRequest.newBuilder(
            URI.create("https://hastebin.com/documents")
    ).POST(HttpRequest.BodyPublisher.fromString(content).build());

    final HttpResponse<String> response = client.send(request, 
    HttpResponse.BodyHandler.asString());
    final String responseContent = response.body();
    final JSONObject responseJson = new JSONObject(responseContent);
    final String key = responseJson.getString("key");
    return "https://hastebin.com/" + key;
    }
}

My errors:

Error:(14, 48) java: cannot find symbol
symbol:   method fromString(java.lang.String)
location: interface java.net.http.HttpRequest.BodyPublisher

Error:(17, 92) java: cannot find symbol
symbol:   method asString()
location: interface java.net.http.HttpResponse.BodyHandler

Error:(19, 15) java: cannot find symbol
symbol:   class JSONObject
location: class Hastebin

Error:(19, 45) java: cannot find symbol
symbol:   class JSONObject
location: class Hastebin

I would really appreciate getting help.

Tal Moshel
  • 232
  • 3
  • 18
  • That should be `ofString`, not `fromString` or `asString`. You're also missing `JSONObject` imports. – Joe C Dec 01 '18 at 10:35

1 Answers1

1

Given that you're working with JDK-11 (the presence of non-complaining java.net.http package), the APIs you're using has changed to BodyPublishers.ofString and BodyHandlers.ofString. You can update them in your code as:

final HttpRequest request = HttpRequest.newBuilder(URI.create("https://hastebin.com/documents"))
            .POST(HttpRequest.BodyPublishers.ofString(content)).build();

final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

Note: The APIs were changed to what they were initially designed as during the incubator module in Java9, hence you might find documentation around with the previous syntaxes.

Additionally: Here is a sample for POST request from openjdk.


Further, as mentioned in the GET JSON sample, if you want to read the response as a custom object, you can use a custom object mapper as:

class UncheckedObjectMapper extends com.fasterxml.jackson.databind.ObjectMapper {
    Map<String, String> readValue(String content) {
        try {
            return this.readValue(content, new com.fasterxml.jackson.core.type.TypeReference() {
            });
        } catch (IOException ioe) {
            throw new CompletionException(ioe);
        }
    }
}

and then read your response as

final HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
Map<String, String> mappedResponse = new UncheckedObjectMapper().readValue(response.body());

For the above fully qualified class names to resolve, you would need dependencies over jackson-databind and jackson-core artifacts.

Note: You can make use of the readValue in a more generic implementation to return custom object type.

Naman
  • 27,789
  • 26
  • 218
  • 353
  • ... and for `JSONObject` you would have to make sure that **Jackson library** is added to your project dependency. – Naman Dec 01 '18 at 10:42
  • I don't know where should I download Jackson from, do you have a link for me? – Tal Moshel Dec 01 '18 at 13:33
  • @TalMoshel One possible link could be maven central - https://search.maven.org/artifact/com.fasterxml.jackson.core/jackson-databind/2.9.7/bundle – Naman Dec 01 '18 at 13:46
  • What is it? I checked the website but I don't understand anything. – Tal Moshel Dec 01 '18 at 13:55
  • @TalMoshel Its a repository of dependencies. You can check the download option adn download the `jar` and include that as an external dependency in your project. – Naman Dec 01 '18 at 14:01
  • I have downloaded it and setten it as an external dependency but I still cannot import JSONObject. any help? – Tal Moshel Dec 01 '18 at 14:06
  • @TalMoshel have updated the answer with the code to convert the string body to a `Map` for example and provided links to dependencies required for the code. – Naman Dec 01 '18 at 14:52