In my app is used send request by https by following this source answer. Now some of them apache methods deprecated. Can anyone help me in order solve in a new approach?
问题:
回答1:
To avoid using deprecated methods in API connectivity, think about using Retrofit. It's a third party library which makes HTTP communication much simpler.
When using Retrofit, you can create an interface of an API endpoint and the use it like a method. The rest of the HTTP request is managed by the library.
Here is the link to the Retrofit github homepage: http://square.github.io/retrofit/
回答2:
HttpURLConnection is part of SDK from API 1, you can use same http://developer.android.com/reference/java/net/HttpURLConnection.html.
// HTTP POST request
private void sendPost() throws Exception {
//Your server URL
String url = "https://selfsolve.apple.com/wcResults.do";
URL obj = new URL(url);
HttpsURLConnection con = (HttpsURLConnection) obj.openConnection();
//add reuqest header
con.setRequestMethod("POST");
con.setRequestProperty("Accept-Language", "en-US,en;q=0.5");
//Request Parameters you want to send
String urlParameters = "sn=C02G8416DRJM&cn=&locale=&caller=&num=12345";
// Send post request
con.setDoOutput(true);// Should be part of code only for .Net web-services else no need for PHP
DataOutputStream wr = new DataOutputStream(con.getOutputStream());
wr.writeBytes(urlParameters);
wr.flush();
wr.close();
int responseCode = con.getResponseCode();
System.out.println("\nSending 'POST' request to URL : " + url);
System.out.println("Post parameters : " + urlParameters);
System.out.println("Response Code : " + responseCode);
BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
//print result
System.out.println(response.toString());
}
More details you can get from
- http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/
- http://syntx.io/how-to-send-an-http-request-from-android-using-httpurlconnection/
回答3:
Please check the below function:
public String makeServiceCall(String url1, MultipartEntity reqEntity) {
try {
// http client
URL url= new URL(url1);
HttpURLConnection httpClient = (HttpURLConnection) url.openConnection();
httpClient.setRequestMethod("POST");
httpClient.setUseCaches(false);
httpClient.setDoInput(true);
httpClient.setDoOutput(true);
httpClient.setRequestProperty("Connection", "Keep-Alive");
httpClient.addRequestProperty("Content-length", reqEntity.getContentLength()+"");
OutputStream os = httpClient.getOutputStream();
reqEntity.writeTo(httpClient.getOutputStream());
os.close();
httpClient.connect();
if (httpClient.getResponseCode() == HttpURLConnection.HTTP_OK) {
return readStream(httpClient.getInputStream());
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
回答4:
In Android SDK 23
HttpClient
is deprecated, you can migrate your code in HttpURLConnection
Something like this
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.connect();
回答5:
if u want to continue using HttpClient for API level 22 and 23.. add org.apache.http.legacy.jar in ur project's Lib folder, ull get .jar file in Android\sdk\platforms\android-23\optional..
if ur using android studio, after copy pasting jar file in lib folder,right click on jar file and click add as library
ur problem will be solved..comment if need any help. thanx!
回答6:
You can Use this Method for Get or Post Any purpose. Just use this method for Server request.
public void RequestToServer() {
// String User_id = "h";
AsyncHttpClient client = new AsyncHttpClient();
RequestParams params = new RequestParams();
// params.put("uid", User_id.toString());
client.post("http:// Your Url", params, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(String s) {
super.onSuccess(s);
Log.d("Server Response for success :", s);
tv.append("service_ReloadSqlDB" + " " + s);
}
@Override
public void onFailure(Throwable throwable) {
super.onFailure(throwable);
Log.d("Server Response for onFailure ", throwable.toString());
}
});
}
You also need a jar file = android-async-http-1.3.1.jar download this jar and add your android project in libs folder After that add this in your build.gradle
dependencies {
compile files('libs/<android-async-http-1.3.1.jar>')
}
Finally rebuild your project , run the application , Get your Server response.