【问题标题】:Android and HTTPS RequestAndroid 和 HTTPS 请求
【发布时间】:2012-01-31 14:17:20
【问题描述】:

请告诉我如何在 Android 上通过 https 发送请求。 我试试这个。但我得到了 IOExeption。

try {
    HostnameVerifier hostnameVerifier = SSLSocketFactory.STRICT_HOSTNAME_VERIFIER;

    DefaultHttpClient client = new DefaultHttpClient();

    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier)hostnameVerifier);
    registry.register(new Scheme("https", socketFactory,443));
    SingleClientConnManager mngr = new SingleClientConnManager(client.getParams(), registry);
    trustEveryone();
    DefaultHttpClient httpClient = new DefaultHttpClient(mngr,client.getParams());

    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    HttpPost httpPost = new HttpPost(URL); 

    StringEntity se = new StringEntity(obj);
    httpPost.setEntity(se);
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/json");

    HttpResponse response = (HttpResponse)httpClient.execute(httpPost);
    StatusLine status = response.getStatusLine();
    if((status.getStatusCode())==200) {
    HttpEntity entity = response.getEntity();
        if(entity!=null) {
            InputStream instream = entity.getContent();
                result = convertStreamToString(instream);
                instream.close();
         } else {
             result=null;
         }
    }
} catch (ClientProtocolException e) {}
  catch (IOException e) {}

【问题讨论】:

  • 请向您的 LogCat 显示确切的错误。没有这个就很难猜了。
  • 然后进行编辑并指出问题所在,这将对其他人有所帮助。
  • 对不起...此任务未完成。我有新问题:
  • Maxim,请看我的新帖子...

标签: android https httprequest


【解决方案1】:

试试这个代码..这会帮助你

public static class getUserLoginAsyncTask extends AsyncTask<Void, Void, Boolean> {

    private void postData(String userName, String eMail) {
        int count = 0;
        int len =5000;

        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("url");

        try {
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("username", userName));
            nameValuePairs.add(new BasicNameValuePair("password", eMail));

            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            Log.e("log_tag", "--:  "+response);

            InputStream is = response.getEntity().getContent();
            int contentSize = (int) response.getEntity().getContentLength();
            System.out.println("Content size ["+contentSize+"]");
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error:  "+e.toString());
        }
    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected Boolean doInBackground(Void... params) {
        postData("test.a@gmail.com", "test");
        return null;
    }
}

【讨论】:

    猜你喜欢
    • 2011-12-01
    • 2023-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-06-27
    • 1970-01-01
    • 2018-03-11
    • 2011-03-06
    • 1970-01-01
    相关资源
    最近更新 更多