1

I've been researching on how to send and receive information to a url, via json for the last 3 days. I have found a lot of documentation and code examples on how to do it, I just can't comprehend what they're saying. I've imported god knows how many .jar files into my eclipse package. Does anyone have a good example on how to connect to a url, send/receive information (even login), parse it, and send more information? I understand that I'm asking for a lot. I don't need all the answers, good documentation and some good examples would make me soooo happy.

Mike G
  • 4,232
  • 9
  • 40
  • 66
Trae Moore
  • 1,759
  • 3
  • 17
  • 32
  • 1
    SO isn't a sample repository ;) try google like [this](https://www.google.com/#hl=en&sclient=psy-ab&q=json+java+example&oq=json+java+example&gs_l=hp.3..0j0i30l3.1588.4478.0.4618.17.13.0.4.4.0.236.2033.0j11j2.13.0...0.0...1c.V4w7MapD_C4&pbx=1&bav=on.2,or.r_gc.r_pw.r_cp.r_qf.,cf.osb&fp=45f67fe1507830ba&biw=1366&bih=614) – Francisco Spaeth Jul 17 '12 at 17:37
  • im building a bot, i already built the guts, im just trying to send and receive from the site... and im banging my head on a wall... – Trae Moore Jul 17 '12 at 17:44
  • i've tried all kind of things, scrapers, inputstream, outputstream, json parsers, builders, im looking in to repository @francisco, thank you – Trae Moore Jul 17 '12 at 17:50
  • by the by... I've been writing java for a week. that's why i suck – Trae Moore Jul 17 '12 at 18:06
  • http://stackoverflow.com/questions/7181534/http-post-using-json-in-java – Lucky Jul 22 '15 at 14:03

2 Answers2

3

Start with http://hc.apache.org/ Then look at http://code.google.com/p/google-gson/ or: http://wiki.fasterxml.com/JacksonHome

That should be all you need.

Rick Mangi
  • 3,761
  • 1
  • 14
  • 17
  • Rick, thanks for your help. I just don't think I can grasp it at this point. It all looks like greek to me. thanks for your help. – Trae Moore Jul 17 '12 at 18:25
  • +1 for Google's GSON library; I have used it in many projects to simplify working with JSON. – Jesse Webb Jul 17 '12 at 18:42
  • 1
    @TraeMoore - if the examples in these libraries look like Greek to you, I would suggest trying to become more proficient with Java in general before trying to do a larger project like you describe. Have you tried making a Hello World app? Do you have an understanding of Object-oriented programming languages? etc... – Jesse Webb Jul 17 '12 at 18:44
  • Its nice to come back to a question i asked a long time ago, and be like.. gahhh i was stupid.. – Trae Moore Jan 29 '14 at 18:25
0

Found a really solid example here on this blog http://www.gnovus.com/blog/programming/making-http-post-request-json-using-apaches-httpclient

Pasted below if for some reason the link doesnt work.

public class SimpleHTTPPOSTRequester {

private String apiusername;
private String apipassword;
private String apiURL;

public SimpleHTTPPOSTRequester(String apiusername, String apipassword, String apiURL) {        
    this.apiURL = apiURL;
    this.apiusername = apiusername;
    this.apipassword = apipassword;
}

public void makeHTTPPOSTRequest() {
    try {
        HttpClient c = new DefaultHttpClient();        
        HttpPost p = new HttpPost(this.apiURL);        

        p.setEntity(new StringEntity("{\"username\":\"" + this.apiusername + "\",\"password\":\"" + this.apipassword + "\"}", 
                         ContentType.create("application/json")));

        HttpResponse r = c.execute(p);

        BufferedReader rd = new BufferedReader(new InputStreamReader(r.getEntity().getContent()));
        String line = "";
        while ((line = rd.readLine()) != null) {
           //Parse our JSON response               
           JSONParser j = new JSONParser();
           JSONObject o = (JSONObject)j.parse(line);
           Map response = (Map)o.get("response");

           System.out.println(response.get("somevalue"));
        }
    }
    catch(ParseException e) {
        System.out.println(e);
    }
    catch(IOException e) {
        System.out.println(e);
    }                        
}    
}
plosco
  • 891
  • 1
  • 10
  • 18