2013-04-04 53 views
1

我已經做了PHP腳本,我想執行從Android的這個PHP腳本。 執行PHP腳本我寫下面的代碼:PHP腳本不運行在4.2(果凍豆)模擬器

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(GINGERBREAD_MR1)模擬器的工作。 但在4.2(果凍豆)模擬器不起作用。 有沒有版本差距?請給我上述問題的原因....

+0

我看來,像你調用主線程網絡。請添加您的logcat – thepoosh 2013-04-04 07:12:00

+0

您是否嘗試從資產文件夾加載腳本或真正使用http .. – 2013-04-04 07:12:11

+0

定義「不工作」。 – 2013-04-04 07:12:57

回答

0

我願意打賭你得到NetworkOnMainThreadException。在Honeycomb中,引入了此異常以確保您在後臺線程上執行這些長時間運行的任務。沒有更多的信息,如果它在Honeycomb之前工作,並且在Honeycomb之後不工作,我會認爲是這種情況。

+0

感謝您的答案,問題是NetworkOnMainThreadException,我解決了它。 – 2013-04-04 08:14:36

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

}