0
votes

I am trying to fetch full name of employee using URL Fetch and Directory Services in App engine

String url = "myurl"+email;
try {

      final URLFetchService urlFetchService = URLFetchServiceFactory.getURLFetchService();

    final HTTPRequest httpRequest = new HTTPRequest(new URL(url));
    final HTTPResponse httpResponse = urlFetchService.fetch(httpRequest);

    ByteArrayInputStream input = new ByteArrayInputStream(httpResponse.getContent());


 String driveJson = IOUtils.toString(input);
            JSONObject driveJsonObject = (JSONObject) JSONValue.parseWithException(driveJson);




if((JSONArray) driveJsonObject.get("items")==null){

}else{
      JSONArray driveJsonArray = (JSONArray) driveJsonObject.get("items");





    for(int i=0;i<driveJsonArray.size();i++){
          JSONObject firstGenre = (JSONObject) driveJsonArray.get(i);



             fName=firstGenre.get("firstName").toString();
             lName=firstGenre.get("lastName").toString();

             Fullname=fName+" "+lName;
    }
} 

Error Details : error recieving fullname Timeout while fetching URL: https://mydirectory.appspot.com/directory/v1/query/[email protected]

I got this exception only few times . can anyone suggest a possible solution for this.

I read the below lines on Google App Engine FAQ

You can set a deadline for a request, the most amount of time the service will wait for a response. By default, the deadline for a fetch is 5 seconds. The maximum deadline is 60 seconds for HTTP requests and 10 minutes for task queue and cron job requests. When using the URLConnection interface, the service uses the connection timeout (setConnectTimeout()) plus the read timeout (setReadTimeout()) as the deadline.

2

2 Answers

2
votes

As the FAQ suggests you should use URLConnection instead of URLFetchService. Then you can set the setConnectTimeout(..) and setReadTimeout(..).

0
votes

Or you can set deadline in fetchoptions, soemthign like :

private FetchOptions fetchOptions = FetchOptions.Builder.withDefaults();

    public void doSomething() {
        fetchOptions.setDeadline(10d);
        HTTPRequest httpRequest = new HTTPRequest(url, HTTPMethod.GET, fetchOptions);
    }