Java Example

The following code calls the Get Region Project Counts API operation that retrieves all regions and countries with their associated project counts.

The code example makes use of the Jakarta Commons HttpClientexternal link library which can be downloaded hereexternal link.

Sample Request

                  import java.io.ByteArrayOutputStream;
                  import java.io.IOException;
                  
                  import org.apache.commons.httpclient.HttpClient;
                  import org.apache.commons.httpclient.methods.GetMethod;
                  
                  public class SampleApiCall
                  {
                  
                     /**
                      * Test harness.
                      */
                     public static void main(String[] args)
                     {
                        String host = "https://api.globalgiving.org";
                        String operation = "/api/public/projectservice/regions/countries/projects/count";
                        String api_key = "YOUR_API_KEY";
                        String url = host + operation + "?api_key=" + api_key;
                        try
                        {
                           String response = getResponse(url);
                           System.out.println(response);
                        }
                        catch (IOException e)
                        {
                           e.printStackTrace();
                        }
                     }
                     
                     /*
                      * Retrieve response using commons HttpClient API.
                      */
                     private static String getResponse(String url) throws IOException {
                        GetMethod get = new GetMethod(url);
                        new HttpClient().executeMethod(get);
                        ByteArrayOutputStream outputStream = new ByteArrayOutputStream() ;
                        byte[] byteArray = new byte[1024];
                        int count = 0 ;
                        while((count = get.getResponseBodyAsStream().read(byteArray, 0, byteArray.length)) > 0)
                        {
                           outputStream.write(byteArray, 0, count) ;
                        }
                        return new String(outputStream.toByteArray(), "UTF-8");
                    }
                  
                  }      
               

Sample Response

                           
               <?xml version="1.0" encoding="utf-8" standalone="yes"?>
               <regions>
                  <region>
                     <countries>
                        <country>
                           <iso3166CountryCode>BW</iso3166CountryCode>
                           <name>Botswana</name>
                           <projectCount>1</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>BF</iso3166CountryCode>
                           <name>Burkina Faso</name>
                           <projectCount>3</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>BI</iso3166CountryCode>
                           <name>Burundi</name>
                           <projectCount>1</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>CM</iso3166CountryCode>
                           <name>Cameroon</name>
                           <projectCount>7</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>CD</iso3166CountryCode>
                           <name>Congo, Democratic Republic of the</name>
                           <projectCount>7</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>CI</iso3166CountryCode>
                           <name>C™te d'Ivoire</name>
                           <projectCount>1</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>EG</iso3166CountryCode>
                           <name>Egypt</name>
                           <projectCount>1</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>ET</iso3166CountryCode>
                           <name>Ethiopia</name>
                           <projectCount>6</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>GM</iso3166CountryCode>
                           <name>Gambia, The</name>
                           <projectCount>1</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>GH</iso3166CountryCode>
                           <name>Ghana</name>
                           <projectCount>23</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>GW</iso3166CountryCode>
                           <name>Guinea-Bissau</name>
                           <projectCount>1</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>KE</iso3166CountryCode>
                           <name>Kenya</name>
                           <projectCount>70</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>LR</iso3166CountryCode>
                           <name>Liberia</name>
                           <projectCount>4</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>MG</iso3166CountryCode>
                           <name>Madagascar</name>
                           <projectCount>3</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>MW</iso3166CountryCode>
                           <name>Malawi</name>
                           <projectCount>5</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>ML</iso3166CountryCode>
                           <name>Mali</name>
                           <projectCount>6</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>MA</iso3166CountryCode>
                           <name>Morocco</name>
                           <projectCount>2</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>MZ</iso3166CountryCode>
                           <name>Mozambique</name>
                           <projectCount>3</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>NA</iso3166CountryCode>
                           <name>Namibia</name>
                           <projectCount>4</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>NE</iso3166CountryCode>
                           <name>Niger</name>
                           <projectCount>3</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>NG</iso3166CountryCode>
                           <name>Nigeria</name>
                           <projectCount>10</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>RW</iso3166CountryCode>
                           <name>Rwanda</name>
                           <projectCount>20</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>SN</iso3166CountryCode>
                           <name>Senegal</name>
                           <projectCount>1</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>SL</iso3166CountryCode>
                           <name>Sierra Leone</name>
                           <projectCount>8</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>SO</iso3166CountryCode>
                           <name>Somalia</name>
                           <projectCount>2</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>ZA</iso3166CountryCode>
                           <name>South Africa</name>
                           <projectCount>20</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>SD</iso3166CountryCode>
                           <name>Sudan</name>
                           <projectCount>13</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>TZ</iso3166CountryCode>
                           <name>Tanzania, United Republic of</name>
                           <projectCount>12</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>TG</iso3166CountryCode>
                           <name>Togo</name>
                           <projectCount>3</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>UG</iso3166CountryCode>
                           <name>Uganda</name>
                           <projectCount>40</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>ZM</iso3166CountryCode>
                           <name>Zambia</name>
                           <projectCount>2</projectCount>
                        </country>
                        <country>
                           <iso3166CountryCode>ZW</iso3166CountryCode>
                           <name>Zimbabwe</name>
                           <projectCount>8</projectCount>
                        </country>
                     </countries>
                     <name>Africa</name>
                  </region>
                  <region>
                  .
                  .
                  .
                  </region>
               </regions>