0

Hi i have no idea about java, but for testing purpose i need some code to do http post request with json parameters in java. I have collected few examples and written the code below but its not working.

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;

import com.google.gson.Gson;

public class pojo1
{
 String name=abc;
 String age=18;
 //generate setter and getters
}

 public class SimpleURL
{
 String postUrl="www.site.com";// put in your url
 Gson gson= new Gson();
 HttpPost post = new HttpPost(postUrl);
 StringEntity  postingString =new StringEntity(gson.toJson(pojo1)); //convert to json
 post.setEntity(postingString);
 post.setHeader("Content-type","application/json");
 HttpResponse  response = httpClient.execute(post);
} 

File name: SimpleURL.java compilation in linux: javac SimpleURL.java Error:

SimpleURL.java:22: <identifier> expected
post.setEntity(postingString);
          ^
SimpleURL.java:22: <identifier> expected
post.setEntity(postingString);
                        ^
SimpleURL.java:23: <identifier> expected
post.setHeader("Content-type","application/json");
          ^
SimpleURL.java:23: illegal start of type
post.setHeader("Content-type","application/json");
           ^
SimpleURL.java:23: illegal start of type
post.setHeader("Content-type","application/json");
                          ^
1
  • explain more what you wanted to do Commented Mar 31, 2014 at 12:46

2 Answers 2

1

Your code doesn't compile. In order to compile, you just need to put the code from SimpleURL in a main method as following :

public class SimpleURL{
    public static void main(String[] args) {
        String postUrl="www.site.com";// put in your url
        Gson gson= new Gson();
        HttpPost post = new HttpPost(postUrl);
        StringEntity  postingString =new StringEntity(gson.toJson(pojo1)); //convert to json
        post.setEntity(postingString);
        post.setHeader("Content-type","application/json");
        HttpResponse  response = httpClient.execute(post);
    }
}

You also need to change www.site.com to be the target website. pojo1 should not be declared public. You can keep it in the same file though.

Sign up to request clarification or add additional context in comments.

Comments

0

your code does not compile, probably because you've defined 2 public class in the same file.

public class pojo1
public class SimpleURL

2 Comments

i have done necessary changes still i get the error, pastebin.com/pmEggF94
you should have two java files one to declare yout pojo1 class in a file named pojo1.java, and the other for SimpleURL in in a file named SimpleURL.java

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.