I Want To Send Http Request In Marshmallow Android?
I am beginner for android application. I develop android application and its my first application. I develop android application for all version of Android, my application based on
Solution 1:
publicstatic Boolean excutePost(String targetURL, JSONObject jsonParam)
{
URL url;
HttpURLConnectionconnection=null;
try {
url = newURL(targetURL);
connection = (HttpURLConnection)url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST"); // hear you are telling that it is a POST request, which can be changed into "PUT", "GET", "DELETE" etc.
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); // here you are setting the `Content-Type` for the data you are sending which is `application/json`
connection.connect();
//Send requestDataOutputStreamwr=newDataOutputStream(
connection.getOutputStream ());
wr.writeBytes(jsonParam.toString());
wr.flush();
wr.close ();
InputStream is;
intresponse= connection.getResponseCode();
if (response >= 200 && response <=399){
//return is = connection.getInputStream();returntrue;
} else {
//return is = connection.getErrorStream();returnfalse;
}
} catch (Exception e) {
e.printStackTrace();
returnfalse;
} finally {
if(connection != null) {
connection.disconnect();
}
}
}
Solution 2:
- You can use HttpURLConnection
To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file:
android { useLibrary 'org.apache.http.legacy' }
Post a Comment for "I Want To Send Http Request In Marshmallow Android?"