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.