Java Codenstuff API
Guides, Help & Support, Tutorials and Examples on how to access and use the Site API.
4 posts
Page 1 of 1
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
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)?
#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
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
Copyright Information
Copyright © Codenstuff.com 2020 - 2023