Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android 4.0.3 https post返回空,http工作,两者都在2.0.3上工作_Java_Android_Apache Httpcomponents_Androidhttpclient - Fatal编程技术网

Java Android 4.0.3 https post返回空,http工作,两者都在2.0.3上工作

Java Android 4.0.3 https post返回空,http工作,两者都在2.0.3上工作,java,android,apache-httpcomponents,androidhttpclient,Java,Android,Apache Httpcomponents,Androidhttpclient,我正在将我的应用程序连接到REST类型的Web服务。我使用的是ApacheHTTP库,请求是一个标准post请求,在后台线程中运行 现在我的问题是如果我使用 http://myserver.com/api/command 它工作正常,我得到了正确的响应,但使用https的url相同: https://myserver.com/api/command 我得到了一个空洞的回答。http头甚至是200 OK 这两个都适用于2.0.3版,但不适用于4.0.3版。在4.0.3上,API似乎只有在使用纯

我正在将我的应用程序连接到REST类型的Web服务。我使用的是ApacheHTTP库,请求是一个标准post请求,在后台线程中运行

现在我的问题是如果我使用

http://myserver.com/api/command
它工作正常,我得到了正确的响应,但使用https的url相同:

https://myserver.com/api/command
我得到了一个空洞的回答。http头甚至是200 OK

这两个都适用于2.0.3版,但不适用于4.0.3版。在4.0.3上,API似乎只有在使用纯http时才起作用,而使用https则会得到空响应

代码如下:

@Override
protected HttpResponse doInBackground(String... params) {
    String link = params[0];
    HttpClient client = createHttpClient();
    try {
        HashMap<String, ContentBody> files = ApiManager.getFiles();
        MultipartEntity mpEntity = new MultipartEntity();
        if(files != null)  {
            for(String i : files.keySet()) {
                ContentBody k = files.get(i);
                mpEntity.addPart(i, k);
            }
        }
        if(this.callParameters != null) {
            for(NameValuePair i : this.callParameters) {
                StringBody sb = new StringBody((String)i.getValue(),"text/plain",Charset.forName("UTF-8"));
                mpEntity.addPart(i.getName(), sb);
            }
        }
        httppost.setEntity(mpEntity);
        // Execute HTTP Post Request
        Log.d("ApiTask","Executing request: "+httppost.getRequestLine());
        HttpResponse response = null;
        response = client.execute(httppost);
        client.getConnectionManager().shutdown();
        return response;
    } 
    catch(UnknownHostException e) {
        exception = e;
        return null;
    }
    catch (IOException e) {
        exception = e;
        return null;
    }
    catch(Exception e) {
        return null;
    }
}

@Override
protected void onPostExecute(HttpResponse result) {
    System.out.println("STATUS:"+result.getStatusLine());
    try {
        StringBuilder responseText = this.inputStreamToString(result.getEntity().getContent());
        System.out.println("RESPONSE:"+responseText);
    }
    catch(Exception e) {
        System.out.println("Error");
    }
}

private HttpClient createHttpClient() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET);
    HttpProtocolParams.setUseExpectContinue(params, true);
    HttpConnectionParams.setConnectionTimeout(params, 10000);
    HttpConnectionParams.setSoTimeout(params, 10000);

    SchemeRegistry schReg = new SchemeRegistry();
    schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
    schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
    ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schReg);
    return new DefaultHttpClient(conMgr, params);
}
@覆盖
受保护的HttpResponse doInBackground(字符串…参数){
字符串链接=参数[0];
HttpClient=createHttpClient();
试一试{
HashMap files=apimager.getFiles();
MultipartEntity mpEntity=新的MultipartEntity();
如果(文件!=null){
对于(字符串i:files.keySet()){
ContentBody k=files.get(i);
mpEntity.addPart(i,k);
}
}
if(this.callParameters!=null){
for(NameValuePair i:this.callParameters){
StringBody sb=新的StringBody((String)i.getValue(),“text/plain”,Charset.forName(“UTF-8”);
mpEntity.addPart(i.getName(),sb);
}
}
httppost.setEntity(mpEntity);
//执行HTTP Post请求
Log.d(“ApiTask”,“正在执行请求:”+httppost.getRequestLine());
HttpResponse响应=null;
响应=client.execute(httppost);
client.getConnectionManager().shutdown();
返回响应;
} 
捕获(未知后异常e){
例外=e;
返回null;
}
捕获(IOE异常){
例外=e;
返回null;
}
捕获(例外e){
返回null;
}
}
@凌驾
受保护的void onPostExecute(HttpResponse结果){
System.out.println(“状态:+result.getStatusLine());
试一试{
StringBuilder responseText=this.inputStreamToString(result.getEntity().getContent());
System.out.println(“响应:+responseText”);
}
捕获(例外e){
System.out.println(“错误”);
}
}
私有HttpClient createHttpClient(){
HttpParams params=新的BasicHttpParams();
HttpProtocolParams.setVersion(params,HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params,HTTP.DEFAULT\u CONTENT\u CHARSET);
HttpProtocolParams.setUseExpectContinue(params,true);
HttpConnectionParams.setConnectionTimeout(参数,10000);
HttpConnectionParams.setSoTimeout(参数,10000);
SchemeRegistry schReg=新SchemeRegistry();
寄存器(新方案(“http”,PlainSocketFactory.getSocketFactory(),80));
寄存器(新方案(“https”,SSLSocketFactory.getSocketFactory(),443));
ClientConnectionManager conMgr=new-ThreadSafeClientConnManager(params,schReg);
返回新的DefaultHttpClient(conMgr,params);
}

提前谢谢

您使用的是定制证书还是认证证书?我不使用证书。服务器本身是具有适当证书的受信任服务器只是猜测:不要使用
ThreadSafeClientConnManager
尝试使用
SingleClientConnManager