Android POST方法停止工作

时间:2016-11-21 19:08:23

标签: android https http-post

我有Android应用程序,它使用我的Web服务器中的一些POST方法。在3天之前,我的应用程序在每个Android设备上顺利运行。今天我遇到的问题是我的https POST调用仅适用于Android设备6.0。优化版本。我有更多的设备(5.0和4.3.x),在他们身上相同的应用程序无法与我的WEB服务器建立通信。我曾与托管公司谈过,该公司持有我的网络服务器,他们告诉我他们在过去三天内没有改变任何事情。奇怪的是,相同的代码在api 23上运行,但不是在api上运行(测试5.0和4.3.x)。所以我的问题是在最近三天发生了一些变化,我没有注意到来自android的https调用或什么?

我从android进行https调用的代码如下(AsyncTask)

protected String doInBackground(Object... params) {
    String param1="a";
    String param2 = "b";
    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    DefaultHttpClient client = new DefaultHttpClient();
    SchemeRegistry registry = new SchemeRegistry();
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
    //registry.register(new Scheme("http", socketFactory, 80));
    registry.register(new Scheme("https", socketFactory, 443));
    SingleClientConnManager mgr = new SingleClientConnManager(client.getParams(), registry);
    DefaultHttpClient httpclient = new DefaultHttpClient(mgr, client.getParams());

    // Set verifier
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

    // Example send http request
    final String url = Config.url_login_async_task;
    HttpPost httppost = new HttpPost(url);

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("param1", param1));
        nameValuePairs.add(new BasicNameValuePair("param2", param2));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        // Execute HTTP Post Request
        response = httpclient.execute(httppost);

    } catch (ClientProtocolException e) {
    } catch (IOException e) {
    }
    try {
        //Log.d("response", response.toString());
        odgovor = EntityUtils.toString(response.getEntity());
    } catch (ParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return odgovor;
}

我有点困惑,因为应用程序在某些设备上停止正常工作,有些没有,我想知道为什么会这样。

编辑:添加了项目助手

buildscript {
repositories {
    jcenter()
}
dependencies {
    classpath 'com.android.tools.build:gradle:1.3.0'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
  }
 }

allprojects {
repositories {
    jcenter()
}
}

模块Gradle

apply plugin: 'com.android.application'

android {

    compileSdkVersion 23
    buildToolsVersion "23.0.1"
    packagingOptions {
        exclude 'META-INF/DEPENDENCIES.txt'
        exclude 'META-INF/DEPENDENCIES'
        exclude 'META-INF/dependencies.txt'
        exclude 'META-INF/LICENSE.txt'
        exclude 'META-INF/LICENSE'
        exclude 'META-INF/license.txt'
        exclude 'META-INF/LGPL2.1'
        exclude 'META-INF/NOTICE.txt'
        exclude 'META-INF/NOTICE'
        exclude 'META-INF/notice.txt'
    }
    defaultConfig {
        applicationId "com.x.y.z"
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}
dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:appcompat-v7:23.0.1'
    compile 'com.google.android.gms:play-services:7.8.0'
}

编辑2:

尝试在useLibrary 'org.apache.http.legacy'中添加android {}以及从其他链接添加所有其他解决方案但仍然没有成功。我读过这个&#34;已弃用&#34; apache库,奇怪的是,在低于API 22的设备上,我的应用程序崩溃(它应该可以工作,因为在那些应用程序上它应该可以工作)并且在API 23上它可以工作(并且它不应该因为在API 23上它&# 39;已弃用。

编辑3:新错误

经过仔细调试并花费数小时试图查找错误后,我在日志中出现了下一个错误

  

无法在表面0xa379c740上设置EGL_SWAP_BEHAVIOR,错误= EGL_SUCCESS   javax.net.ssl.SSLPeerUnverifiedException:没有对等证书   在com.android.org.conscrypt.SSLNullSession.getPeerCertificates(SSLNullSession.java:104)   at org.apache.http.conn.ssl.AbstractVerifier.verify(AbstractVerifier.java:98)   at org.apache.http.conn.ssl.SSLSocketFactory.createSocket(SSLSocketFactory.java:393)   at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:170)   在org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:169)   在org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:124)   在org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:365)   在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:560)   在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:492)   在org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:470)

有人可以尝试向我解释一下发生此错误的证书在服务器上发生了什么吗?

1 个答案:

答案 0 :(得分:1)

此问题基于HTTPS:服务器中的Securityexception,一旦在托管域中更新您的SSL,您将得到正确的响应,否则您将使用以下代码

HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
DefaultHttpClient httpClient = new DefaultHttpClient();
SchemeRegistry registry = new SchemeRegistry();
SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
registry.register(new Scheme("https", socketFactory, 443));
registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
SingleClientConnManager mgr = new SingleClientConnManager(httpClient.getParams(), registry);
DefaultHttpClient client = new DefaultHttpClient(mgr, httpClient.getParams());

// Set verifier
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);

DefaultHttpClient client=new DefaultHttpClient();
HttpPost request = new HttpPost(url);
request.setEntity(new StringEntity(jobject.toString()));
request.setHeader("Content-type", "application/json");
HttpResponse response = client.execute(request);
HttpEntity resEntity = response.getEntity();
String json = EntityUtils.toString(resEntity);