java.lang.NoSuchMethodError:无虚方法执行的HttpClientResponse [英] java.lang.NoSuchMethodError: No virtual method execute for HttpClientResponse

查看:4986
本文介绍了java.lang.NoSuchMethodError:无虚方法执行的HttpClientResponse的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试启动它显示像这样的logcat异常后运行应用程序:

When I try to run the app after launching it is showing exception in logcat like this:

java.lang.NoSuchMethodError: No virtual method execute(Lorg/apache/http/client/methods/HttpUriRequest;)
Lorg/apache/http/client/methods/CloseableHttpResponse; 
in class Lorg/apache/http/impl/client/DefaultHttpClient;
or its super classes (declaration of 'org.apache.http.impl.client.DefaultHttpClient' appears in /system/framework/ext.jar)

在类文件被调用execute方法后得到错误。

In class file is getting error after calling execute method.

CloseableHttpResponse httpResponse = null;

httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(strServiceURL);
//When executing the below line get null value for response.
httpResponse = httpClient.execute(httpGet);  
httpEntity = httpResponse.getEntity();
responsePayload = EntityUtils.toString(httpEntity);

在试图调用execute()方法时,我改变了Htt presponse,并通过添加HTTP的客户4.3.5.jar它越来越空值。

While trying to call execute() method it's getting null value when I change to HttpResponse and by adding http-client-4.3.5.jar.

无论execute()方法是,如果这样有什么用POST方法来执行HTTP连接呼叫解决方案pcated德$ P $。

Whether execute() method is deprecated if so what is the solution to execute the http connection call using post method.

请给我提供了这个错误的一些建议。
感谢提前的建议和解答!

Please provide me some suggestions for this error. Thanks for suggestions and answers in advance!

推荐答案

你使用的HttpClient 4.5在你的Andr​​oid项目?转到阿帕奇,我发现Android的,他们使用的HttpClient为Android 4.3.5。我的项目中使用这些JAR文件作为库(在没有的gradle编译)。您可以尝试下载 rel=\"nofollow\">。希望这有助于!

Do you use HttpClient 4.5 in your Android project? Go to Apache, I have found for Android they use HttpClient for Android 4.3.5. My projects use these Jar files as library (not compiled in gradle). You can try download here. Hope this helps!

无论execute()方法是,如果这样有什么用POST方法来执行HTTP连接呼叫解决方案pcated德$ P $。

Whether execute() method is deprecated if so what is the solution to execute the http connection call using post method.

您可以参考下面的示例code:

You can refer to the following sample code:

Utils.java

public static String buildPostParameters(Object content) {
        String output = null;
        if ((content instanceof String) ||
                (content instanceof JSONObject) ||
                (content instanceof JSONArray)) {
            output = content.toString();
        } else if (content instanceof Map) {
            Uri.Builder builder = new Uri.Builder();
            HashMap hashMap = (HashMap) content;
            if (hashMap != null) {
                Iterator entries = hashMap.entrySet().iterator();
                while (entries.hasNext()) {
                    Map.Entry entry = (Map.Entry) entries.next();
                    builder.appendQueryParameter(entry.getKey().toString(), entry.getValue().toString());
                    entries.remove(); // avoids a ConcurrentModificationException
                }
                output = builder.build().getEncodedQuery();
            }
        }

        return output;
    }

public static URLConnection makeRequest(String method, String apiAddress, String accessToken, String mimeType, String requestBody) throws IOException {
        URL url = new URL(apiAddress);
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        urlConnection.setDoInput(true);
        urlConnection.setDoOutput(!method.equals("GET"));
        urlConnection.setRequestMethod(method);

        urlConnection.setRequestProperty("Authorization", "Bearer " + accessToken);        

        urlConnection.setRequestProperty("Content-Type", mimeType);
        OutputStream outputStream = new BufferedOutputStream(urlConnection.getOutputStream());
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "utf-8"));
        writer.write(requestBody);
        writer.flush();
        writer.close();
        outputStream.close();            

        urlConnection.connect();

        return urlConnection;
    }

MainActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    new APIRequest().execute();
}

private class APIRequest extends AsyncTask<Void, Void, Object> {

        @Override
        protected Object doInBackground(Void... params) {

            // Of course, you should comment the other CASES when testing one CASE

            // CASE 1: For FromBody parameter
            String url = "http://10.0.2.2/api/frombody";
            String requestBody = Utils.buildPostParameters("'FromBody Value'"); // must have '' for FromBody parameter
            HttpURLConnection urlConnection = null;
            try {
                urlConnection = (HttpURLConnection) Utils.makeRequest("POST", url, null, "application/json", requestBody);                    
                if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    ...
                } else {
                    ...
                }
                ...
                return response;
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }

            // CASE 2: For JSONObject parameter
            String url = "http://10.0.2.2/api/testjsonobject";
            JSONObject jsonBody;
            String requestBody;
            HttpURLConnection urlConnection;
            try {
                jsonBody = new JSONObject();
                jsonBody.put("Title", "BNK Title");
                jsonBody.put("Author", "BNK");
                jsonBody.put("Date", "2015/08/08");
                requestBody = Utils.buildPostParameters(jsonBody);
                urlConnection = (HttpURLConnection) Utils.makeRequest("POST", url, null, "application/json", requestBody);                    
                if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                     ...
                } else {
                    ...
                }
                ...
                return response;
            } catch (JSONException | IOException e) {
                e.printStackTrace();
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }           

            // CASE 3: For form-urlencoded parameter
            String url = "http://10.0.2.2/api/token";
            HttpURLConnection urlConnection;
            Map<String, String> stringMap = new HashMap<>();
            stringMap.put("grant_type", "password");
            stringMap.put("username", "username");
            stringMap.put("password", "password");
            String requestBody = Utils.buildPostParameters(stringMap);
            try {
                urlConnection = (HttpURLConnection) Utils.makeRequest("POST", url, null, "application/x-www-form-urlencoded", requestBody);
                JSONObject jsonObject = new JSONObject();
                try {
                    if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
                        ...
                    } else {
                        ...
                    }                    
                } catch (IOException | JSONException e) {
                    e.printStackTrace();
                }
                return jsonObject;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }

            return null;
        }

        @Override
        protected void onPostExecute(Object response) {
            super.onPostExecute(response);
            if (response instanceof String) {
                ...               
            } else if (response instanceof JSONObject) {
                ...
            } else {
                ...
            }
        }
    }

这篇关于java.lang.NoSuchMethodError:无虚方法执行的HttpClientResponse的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆