新浪微博sdk错误---The target server failed to respond

 
  
下了新浪微博sdk发现2.3不能成功发布
新浪微博官方httpclient  httpsandroid4.0一下都有一个https请求bug,于是只好sdk源码只要改com.weibo.sdk.android.nethttpmanager.class.找到MySSLSocketFactory类.
1.修改一下createSocket方法:
 
  
@Override
		public Socket createSocket(Socket socket, String host, int port, boolean autoClose)
				throws IOException, UnknownHostException
		{
			int currentApiVersion=android.os.Build.VERSION.SDK_INT;
			Log.e("version", currentApiVersion+"");
			if(currentApiVersion<0x0000000e)  //api version smaller than ICE_CREAM_SANDWICH
			{
				injectHostname(socket, host);
			}
			return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose);
		}

2. 增加 injectHostname 方法:

private void injectHostname(Socket socket, String host)
        {
            try 
            {
                Field field = InetAddress.class.getDeclaredField("hostName");
                field.setAccessible(true);
                field.set(socket.getInetAddress(), host);
            }
            catch (Exception ignored)
            {
            	ignored.printStackTrace();
            }
        }

 
  
 
 

你可能感兴趣的:(Android,网络)