Java.lang.nosuchmethoderror: No Virtual Method Execute For Httpclientresponse
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/HttpUr
Solution 1:
Above solutions did not work for me.
useLibrary 'org.apache.http.legacy'
Adding this line in gradle worked for me.
Solution 2:
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!
Whether execute() method is deprecated if so what is the solution to execute the http connection call using post method.
You can refer to the following sample code:
Utils.java:
publicstaticStringbuildPostParameters(Object content) {
String output = null;
if ((content instanceofString) ||
(content instanceofJSONObject) ||
(content instanceofJSONArray)) {
output = content.toString();
} elseif (content instanceofMap) {
Uri.Builder builder = newUri.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;
}
publicstaticURLConnectionmakeRequest(String method, String apiAddress, String accessToken, String mimeType, String requestBody) throws IOException {
URL url = newURL(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 = newBufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = newBufferedWriter(newOutputStreamWriter(outputStream, "utf-8"));
writer.write(requestBody);
writer.flush();
writer.close();
outputStream.close();
urlConnection.connect();
return urlConnection;
}
MainActivity.java:
@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
newAPIRequest().execute();
}
privateclassAPIRequestextendsAsyncTask<Void, Void, Object> {
@OverrideprotectedObjectdoInBackground(Void... params) {
// Of course, you should comment the other CASES when testing one CASE// CASE 1: For FromBody parameterString url = "http://10.0.2.2/api/frombody";
String requestBody = Utils.buildPostParameters("'FromBody Value'"); // must have '' for FromBody parameterHttpURLConnection 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 parameterString url = "http://10.0.2.2/api/testjsonobject";
JSONObject jsonBody;
String requestBody;
HttpURLConnection urlConnection;
try {
jsonBody = newJSONObject();
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 parameterString url = "http://10.0.2.2/api/token";
HttpURLConnection urlConnection;
Map<String, String> stringMap = newHashMap<>();
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 = newJSONObject();
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();
}
}
returnnull;
}
@OverrideprotectedvoidonPostExecute(Object response) {
super.onPostExecute(response);
if (response instanceofString) {
...
} elseif (response instanceofJSONObject) {
...
} else {
...
}
}
}
Solution 3:
Try changing the android sdk version that you are using to something before Marshmallow and removing the apache jar files from your project. These classes are already available in the sdk.
Post a Comment for "Java.lang.nosuchmethoderror: No Virtual Method Execute For Httpclientresponse"