1

我有一个使用休息服务的 android 应用程序。当我向休息服务发送任何发布/获取请求时,休息服务器上的操作正在完成,服务器响应 200 ok。但是当我尝试获取响应正文时,出现以下错误。

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

我的代码块如下所示

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

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

我的班级正在创建 http 请求。

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;
    }

}

我没有使用 asynctask 或任何其他异步操作。这可能是一个原因吗?

谢谢。

4

0 回答 0