Java Codenstuff API

Guides, Help & Support, Tutorials and Examples on how to access and use the Site API.
4 posts Page 1 of 1
Contributors
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Java Codenstuff API
Shim
I wrote this when the new api was introduced but I didn't use json here to parse because I didn't find any good and didn't have time.
Code: Select all
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

class Codenstuff {
/* 
Author : Mohamed Shimran A.K.A mshimranpro

 */
    public static void main(String args[]) {

        String username;
        String apicode;

        try {
            Scanner in = new Scanner(System. in );
            System.out.println("Enter your Codenstuff username :");
            username = in .nextLine();
            System.out.println("Enter your Codenstuff API code :");
            apicode = in .nextLine();

            URL url = new URL("http://codenstuff.com/forum/api/LiveAPIJsonXML.php?mode=SelfProfile&username=" + username + "&skey=" + apicode);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Accept", "application/json");

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

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

            String output;
            while ((output = br.readLine()) != null) {
                System.out.println(output);
            }

            conn.disconnect();

        } catch (MalformedURLException e) {

            e.printStackTrace();

        } catch (IOException e) {

            e.printStackTrace();

        }

    }
}
Find my programs on Softpedia
User avatar
comathi
Coding God
Coding God
Posts: 1242
Joined: Fri Mar 26, 2010 1:59 pm

Re: Java Codenstuff API
comathi
So... what does this do? Does it just retrieve the response from the API, or does it actually parse it (I know you're not doing JSON, but what about XML)?
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Re: Java Codenstuff API
Shim
#comathi it just retrieve the response from API and as you can see the response is json so only with json it can be parsed
Find my programs on Softpedia
User avatar
CodenStuff
Site Admin
Site Admin
Posts: 4389
Joined: Tue Aug 04, 2009 1:47 am

Re: Java Codenstuff API
CodenStuff
Good stuff. All these little bits of code will come in handy for anyone wishing to use the API in the future so keep them coming cooll;
Welcome to CodenStuff.com Learn Code, Love Code. Thank you for being a member of the community.
4 posts Page 1 of 1
Return to “Codenstuff API”