android将https post请求发送到服务器而没有弃用的metods

时间:2015-09-30 04:53:34

标签: android post ssl https request

在我的应用中,按照this source answer使用https发送请求。现在他们中的一些apache方法已被弃用。任何人都可以帮我解决一个新的方法吗?

6 个答案:

答案 0 :(得分:5)

为避免在API连接中使用已弃用的方法,请考虑使用 Retrofit 。它是第三方库,使HTTP通信更加简单。

使用Retrofit时,您可以创建API端点的接口,并将其用作方法。 HTTP请求的其余部分由库管理。

以下是Retrofit github主页的链接: http://square.github.io/retrofit/

答案 1 :(得分:3)

HttpURLConnection是API 1中SDK的一部分,您可以使用相同的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());

}

您可以从

获取更多详细信息
  1. http://www.mkyong.com/java/how-to-send-http-request-getpost-in-java/
  2. http://syntx.io/how-to-send-an-http-request-from-android-using-httpurlconnection/

答案 2 :(得分:1)

请检查以下功能:

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

答案 3 :(得分:0)

在Android SDK 23中

不推荐使用

HttpClient,您可以在HttpURLConnection

中迁移代码

像这样的东西

URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.connect();

答案 4 :(得分:0)

如果你想继续使用HttpClient进行API级别22和23 .. 在你项目的 Lib 文件夹中添加 org.apache.http.legacy.jar , 我会在 Android \ sdk \ platforms \ android-23 \ optional ..

中获取 .jar 文件

如果您使用android studio,在lib文件夹中复制粘贴jar文件后,右键单击jar文件并单击添加为库

你的问题将被解决。如果需要任何帮助,请注意。 感谢名单!

答案 5 :(得分:0)

您可以将此方法用于获取或发布任何目的。只需将此方法用于服务器请求。

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

    }

你还需要一个jar文件= android-async-http-1.3.1.jar 下载这个jar并在libs文件夹中添加你的android项目  之后在build.gradle中添加它

dependencies {

compile files('libs/<android-async-http-1.3.1.jar>')
}

最后重建项目,运行应用程序,获取服务器响应。