Search code examples
javaandroidnetwork-programmingbing-apirobospice

Trying to use bing translator API with Robospice in Android


I need to implement robospice for doing the networking part in my Translator app. I previously used async task class and it was working fine, but now i want to improve my application with implementing robospice. I'am trying to execute the following code but it doesn't't throw any exception it just never executes....

@Override
public TranslatedText loadDataFromNetwork() throws Exception {

    String jsonString = getJsonString();
    String headerValue = getHeaderValue(jsonString);

    String text = pair.getWordPairs().getWordFrom();
    String languageFrom = pair.getLanguagePairs().getLanguageFrom().getCode();
    String languageTo = pair.getLanguagePairs().getLangougateTo().getCode();

    String uri = String
            .format("http://api.microsofttranslator.com/v2/Http.svc/Translate?text=%s&from=%s&to=%s&contentType=text/html",
                    URLEncoder.encode(text, "UTF-8"),
                    URLEncoder.encode(languageFrom, "UTF-8"),
                    URLEncoder.encode(languageTo, "UTF-8"));


    HttpHeaders headers = new HttpHeaders();

    headers.set("Authorization", headerValue);

    // Create a new RestTemplate instance
    RestTemplate restTemplate = new RestTemplate();

    // Add the Simple XML message converter
    getRestTemplate().getMessageConverters().add(new SimpleXmlHttpMessageConverter());

    //set the headerValue in the Entity
    org.springframework.http.HttpEntity<?> request = new org.springframework.http.HttpEntity<Object>(headerValue);
    // Make the HTTP GET request, marshaling the response from XML to an
    // EventList
    Log.v("request","Making request!");

    //This line never finish execuitng, doesen't throw exception or anything in logCat
    ResponseEntity<Object> responseEntity = getRestTemplate().exchange(uri, HttpMethod.GET, request, null);
    Log.v("request", responseEntity.getBody().toString());
    Log.d("Load Data From Network", request.getBody().toString());
    return null;
}

The last thing it shows in log cat is Request First!! And nothing after that. It never even gets to The Request Listener onRequestFailure. Can any 1 tell me what i do wrong ?


Solution

  • I fixed the problem. So if you need to handle stream you will have to provide the following code

     ResponseEntity<byte[]> responseEntity = getRestTemplate().exchange(uri, HttpMethod.GET, request, byte[]);