0

I do not know what I am doing wrong, but I cannot access a REST API using the POST method in Java with Json parameters.

Each time I run the program, I receive Java.net.SocketException - Connection Reset.

I tried to access the API from PHP, and it worked.

Code: https://github.com/BobTheProgrammer/Question/blob/master/POST

1
  • 1
    Questions seeking debugging help must include the shortest code necessary to reproduce it in the question itself. Commented Oct 27, 2015 at 13:05

2 Answers 2

1

try this one

    public static void HttpPost() {
    try {

        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost(
                "https://api.website.net/auth/token");
        StringEntity input = new StringEntity("{\"auth\":{\"user\":{\"id\":\"bla\",\"password\":\"bla\"}\"method\":\"user\",\"website\":\"http://website.net/\"}}");
        post.setEntity(input);
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {
            System.out.println(line);
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
Sign up to request clarification or add additional context in comments.

1 Comment

What could it be? Anyway. I need to implement the API in a android app. Since I can access it using php code,how can I get the response from the server into a text file?
1

There is a problem with the JSON syntax in your source code in line number 55, you have missed a comma after the user object.

Replace this line

String urlParameters = "{\"auth\":{\"user\":{\"id\":\"bla\",\"password\":\"bla\"}\"method\":\"user\",\"website\":\"http://website.net/\"}}";

with this one(added a comma after the user object before method)

String urlParameters = "{\"auth\":{\"user\":{\"id\":\"bla\",\"password\":\"bla\"},\"method\":\"user\",\"website\":\"http://website.net/\"}}";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.