2012-08-26 142 views
0

使用HttpsURLConnection的發送JSON對象我如下在android系統

try{ 
    JSONObject jobj=new JSONObject(); 

    jobj.put("UserName","demo1"); 
    jobj.put("Password","demo1"); 
    Log.e("json string :",jobj.toString()); 

    byte[] postData = jobj.toString().getBytes(); 

    URL url = new URL("https://services.myovs.com/TokenService/Token"); 

    SSLContext sc = SSLContext.getInstance("SSL"); 
    sc.init(null, trustAllCerts, new java.security.SecureRandom()); 
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); 

    HttpsURLConnection conn = (HttpsURLConnection) url.openConnection(); 

    conn.setDoOutput(true); 
    conn.setDoInput(true); 
    conn.setUseCaches(false); 

    conn.setRequestMethod("POST"); 
    conn.setRequestProperty("Content-Type", "application/json"); 

    OutputStream out = conn.getOutputStream(); 

    out.write(postData); 
    out.close(); 

    String temp = ((HttpsURLConnection)conn).getResponseMessage(); 
    Log.e("response :",temp); 
} catch(Exception ex) { 
    ex.printStackTrace(); 
} 

TrustManager[] trustAllCerts = new TrustManager[] { 
    new X509TrustManager() { 
     public java.security.cert.X509Certificate[] getAcceptedIssuers() { 
      return null; 
     } 
     public void checkClientTrusted(
       java.security.cert.X509Certificate[] certs, 
       String authType) {} 
     public void checkServerTrusted(
       java.security.cert.X509Certificate[] certs, 
       String authType) {} 
    } 
}; 

但發送一個JSON對象使用HttpsUrlConnection服務器它得到一個例外,像

08-26 20:12:30.179:WARN/System.err(12477):java.io.IOException:主機名'services.myovs.com'未驗證
08-26 20:12:30.187:WARN/System.err(12477):at org.apache.harmony.luni.internal.net.www.protocol.http.HttpConnection.verifySecureSocketHostname(HttpConnection.java:199)
08-26 20:12:30.187:WARN/System.err(12477):at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl $ HttpsEngine.makeConnection(HttpsURLConnectionImpl.java: 391)
08-26 20:12:30.187:WARN/System.err(12477):at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java: 205)
08-26 20:12:30.187:WARN/System.err(12477):at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.get OutputStream(HttpURLConnectionImpl.java:614)
08-26 20:12:30.187:WARN/System.err(12477):at org.apache.harmony.luni.internal.net.www.protocol.https.HttpsURLConnectionImpl.getOutputStream (HttpsURLConnectionImpl.java:268)
08-26 20:12:30.187:WARN/System.err(12477):at com.example.httpconnection.Main.httpMethode(Main.java:69)
08-26 20 :12:30.187:WARN/System.err(12477):at com.example.httpconnection.Main.onCreate(Main.java:27)
08-26 20:12:30.187:WARN/System.err(12477) :at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-26 20:12:30.187:WARN/System.err(12477):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615 )
08-26 20:12:30.187:WARN/System.err(12477):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-26 20:12:30.187:WARN/System。 err(12477):at android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
08-26 20:12:30.187:WARN/System.err(12477):at android.app.ActivityThread $ H。 handleMessage(ActivityThread.java:935)
08-26 20:12:30.195:WARN/System.err(12477):at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 20: 12:30.195:WARN/System.err(12477):在android.os.Looper.loop(Looper.java:130)> 08-26 20:12:30.195:WARN/System.err(12477):在android。 app.ActivityThread.main(ActivityThread.java:3687)
08-26 20:12:30.195:WARN/System.err(12477):at java.lang.reflect.Method.invokeNative(Native Method)
08-26 20:12:30.195:WARN/System.err(12477):at java.lang.reflect.Method.invoke(Method.java:507)
08-26 20:12:30.195:WARN/System.err(12477):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:867)
08-26 20:12:30.195:WARN/System.err(12477):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08- 26 20:12:30.195:WARN/System.err(12477):at dalvik.system.NativeStart.main(Native Method)

任何人都可以幫助我嗎?

+0

你可以請發佈整個堆棧跟蹤? – thermz

+0

我編輯的迴應請找到 – surendra

回答

2

據我所知,這不是一個例外。相反,它是寫入日誌的警告,包括堆棧跟蹤。

該警告指示連接不安全,因爲主機名無法驗證。它與您使用的HTTPS(而不是HTTP)連接有關。服務器可能沒有由公衆知名的證書頒發機構頒發的證書。