【问题标题】:http request does not work for android version 4http 请求不适用于 android 版本 4
【发布时间】:2013-04-06 14:13:59
【问题描述】:

我使用 android 版本 2 运行我的应用程序,它工作正常,但是当我使用版本 4 运行它时,它在request.setURI(new URI(url)); 行中出现异常

public String getXmlFromUrl(String url) {
    String xml = null;

    try {
        // defaultHttpClient
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        try {
            request.setURI(new URI(url));
        } catch (URISyntaxException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.print("http\n");
        }
        HttpResponse httpResponse = client.execute(request);
        HttpEntity httpEntity = httpResponse.getEntity();
        xml = EntityUtils.toString(httpEntity);

    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // return XML
    return xml;
}

我不明白是什么问题。

【问题讨论】:

  • 请发布异常的堆栈跟踪
  • 你是在异步任务中做这项工作吗?因为从 andorid 4 开始,你不能在主线程上建立网络连接。 AsyncTask tutorial
  • 没有。我不使用异步任务
  • slayton try catch 没有出现异常,所以我找不到问题所在
  • logcat 中应该有什么。发布您的日志猫。并检查您的 URI 是否正常。而且,如果可以从您正在测试的设备访问该链接,还请查看您是否拥有互联网权限。

标签: android http


【解决方案1】:

试试下面的代码。你必须使用SSLSocketFactory

把这个放在 setContentView 之后

int SDK_INT = android.os.Build.VERSION.SDK_INT;

} if (SDK_INT>8){

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();

StrictMode.setThreadPolicy(policy);

}

HttpClient httpclient = getNewHttpClient();

.... // HttpGet 请求 = new HttpGet(); ..

public HttpClient getNewHttpClient() {
        try {
            KeyStore trustStore = KeyStore.getInstance(KeyStore
                    .getDefaultType());
            trustStore.load(null, null);

            SSLSocketFactory sf = new MySSLSocketFactory(trustStore);
            sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

            HttpParams params = new BasicHttpParams();
            HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
            HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);

            SchemeRegistry registry = new SchemeRegistry();
            registry.register(new Scheme("http", PlainSocketFactory
                    .getSocketFactory(), 80));
            registry.register(new Scheme("https", sf, 443));



            ClientConnectionManager ccm = new ThreadSafeClientConnManager(
                    params, registry);

            return new DefaultHttpClient(ccm, params);
        } catch (Exception e) {
            return new DefaultHttpClient();
        }
    }

MySSLSocketFactory.java

public class MySSLSocketFactory extends SSLSocketFactory {
    SSLContext sslContext = SSLContext.getInstance("TLS");

public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException, UnrecoverableKeyException {
    super(truststore);

    TrustManager tm = new X509TrustManager() {
        public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
        }

        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    };

    sslContext.init(null, new TrustManager[] { tm }, null);
}

@Override
public Socket createSocket(Socket socket, String host, int port, boolean autoClose) throws IOException, UnknownHostException {
    return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
}

@Override
public Socket createSocket() throws IOException {
    return sslContext.getSocketFactory().createSocket();
}

}

【讨论】:

  • 为什么。他没有使用安全套接字。
  • @DaleWilson:没关系。如果他想解决这个问题,他已经使用了这个
  • @NiravRanpara 当 OP 甚至没有发布日志输出时,你怎么能确定这是解决问题的方法?鉴于我们现在掌握的信息,我认为没有理由认为这会解决问题......事实上,我认为 NetworkOnMainThread 更有可能是罪魁祸首。
猜你喜欢
  • 2018-05-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-08
  • 1970-01-01
相关资源
最近更新 更多