1

I have an android application which is using rest service . when I send any post/get request to rest service , operation is completing on rest server and server response 200 ok. But I when i try to get response body, i get the following error.

" Android  An exception occurred: java.lang.IllegalStateException "

my code block is shown below

 HttpEntity ResponseEntity = resp.getEntity();
                 if(ResponseEntity!=null){

                     String responseBody = EntityUtils.toString(ResponseEntity); // Error Occurs here
                 }

my class which is creating http request .

package httpOperations;

import java.io.IOException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.entity.StringEntity;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

import android.util.Base64;
import android.util.Log;

import com.google.gson.Gson;

public class HttpGetRequest extends baseHttpRequest {

    HttpGet httpGet = null;

    public HttpGetRequest(String methodName, String prms) {
        super((methodName));
        String base64EncodedCredentials = "Basic "
                + Base64.encodeToString(("ilhan" + ":" + "123").getBytes(),
                        Base64.NO_WRAP);
        String URL = GetServiceURL();
        httpGet = new HttpGet(URL);

        httpGet.setHeader("Authorization", base64EncodedCredentials);
        httpGet.setHeader(HTTP.CONTENT_TYPE, "application/json");
        httpGet.setHeader("accept", "application/json");
        Gson gson = new Gson();

        StringEntity se = null;

    }

    @Override
    public HttpResponse SendRequest() {
        HttpResponse resp = null;
        try {

            resp = GetHttpClient().execute(httpGet);
            final int statusCode = resp.getStatusLine().getStatusCode();

            if (statusCode != HttpStatus.SC_OK) {
                Log.w(getClass().getSimpleName(), "Error " + statusCode
                        + " for URL " + GetServiceURL());
            }

            HttpEntity ResponseEntity = resp.getEntity();

            if (ResponseEntity != null) {
                String responseBody = EntityUtils.toString(ResponseEntity);

            }

            // ResponseEntity.getContent();

        }

        catch (IOException e) {

            httpGet.abort();

            Log.w(getClass().getSimpleName(), "Error for URL "
                    + GetServiceURL(), e);

        }
        return resp;
    }

}

i'm not using asynctask or any other async operations. is this can be a reason ?

Thanks.

ilhan
  • 125
  • 2
  • 13
  • Please show more context (more code, longer stacktrace) – hgoebl Nov 09 '13 at 20:35
  • And try to format your code that it's a pleasure to read it ;-) – hgoebl Nov 09 '13 at 20:35
  • OK, maybe this is the point where you could enable most warnings in your IDE and first eliminate some noise in your code. It's hard to read because the code is not Java like. Seems like C# – hgoebl Nov 09 '13 at 21:04
  • Hi , hgoebl thanks for your help. I found the solution. as i expected before ; cause of problem is that i am not using async operations . when I add this operations in to the asyntask the problem is solved. by the way i am already C# developer and i am new in android and Eclipse :) – ilhan Nov 10 '13 at 05:04

0 Answers0