1

我已经制作了 php 脚本,我想从 android 执行这​​个 php 脚本。要执行 php 脚本,我编写以下代码:

    HttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse;
        try
        {
            //execute php script for creating group
        HttpGet httpPost = new HttpGet("http://ip address/CreateGroup.php?gname=temp&desc=grouptesting);

        try {
            httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

此代码适用于 2.3.3(GIGERBREAD_MR1) 模拟器。但在 4.2(果冻豆)模拟器中不起作用。有版本差距吗??请给我上述问题的原因....

4

2 回答 2

0

我敢打赌你会得到一个NetworkOnMainThreadException. 在 Honeycomb 中,引入了此异常以确保您在后台线程上执行此类长时间运行的任务。如果没有更多信息,我认为如果它在 Honeycomb 之前有效而在 Honeycomb 之后无效。

于 2013-04-04T07:13:06.397 回答
0

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

HttpClient httpclient = getNewHttpClient();

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

}

于 2013-04-04T07:15:19.037 回答