Network.java 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. package com.usai.util;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.net.Socket;
  7. import java.net.UnknownHostException;
  8. import java.security.KeyManagementException;
  9. import java.security.KeyStore;
  10. import java.security.KeyStoreException;
  11. import java.security.NoSuchAlgorithmException;
  12. import java.security.UnrecoverableKeyException;
  13. import java.util.Iterator;
  14. import java.util.Set;
  15. import javax.net.ssl.SSLContext;
  16. import javax.net.ssl.TrustManager;
  17. import javax.net.ssl.X509TrustManager;
  18. import org.apache.http.HttpEntity;
  19. import org.apache.http.HttpResponse;
  20. import org.apache.http.HttpStatus;
  21. import org.apache.http.HttpVersion;
  22. import org.apache.http.client.HttpClient;
  23. import org.apache.http.client.methods.HttpPost;
  24. import org.apache.http.conn.ClientConnectionManager;
  25. import org.apache.http.conn.ConnectTimeoutException;
  26. import org.apache.http.conn.scheme.PlainSocketFactory;
  27. import org.apache.http.conn.scheme.Scheme;
  28. import org.apache.http.conn.scheme.SchemeRegistry;
  29. import org.apache.http.conn.ssl.SSLSocketFactory;
  30. import org.apache.http.entity.mime.HttpMultipartMode;
  31. import org.apache.http.entity.mime.MultipartEntity;
  32. import org.apache.http.entity.mime.content.StringBody;
  33. import org.apache.http.impl.client.DefaultHttpClient;
  34. import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
  35. import org.apache.http.params.BasicHttpParams;
  36. import org.apache.http.params.HttpConnectionParams;
  37. import org.apache.http.params.HttpParams;
  38. import org.apache.http.params.HttpProtocolParams;
  39. import org.apache.http.protocol.HTTP;
  40. import org.json.JSONException;
  41. import org.json.JSONObject;
  42. import com.usai.apex.ApexTrackingApplication;
  43. import com.usai.apex.CustomizeFieldsActivity.fieldedit;
  44. import android.content.Context;
  45. import android.content.SharedPreferences.Editor;
  46. import android.content.pm.PackageManager.NameNotFoundException;
  47. import android.database.Cursor;
  48. import android.database.sqlite.SQLiteDatabase;
  49. import android.database.sqlite.SQLiteStatement;
  50. import android.net.ConnectivityManager;
  51. import android.net.NetworkInfo;
  52. import android.os.Bundle;
  53. import android.text.TextUtils;
  54. import android.util.Log;
  55. public class Network
  56. {
  57. public static final int BEHAVIOR_SEARCH = 0;
  58. public static final int BEHAVIOR_RESULT = 1;
  59. private static final int REQUEST_TIMEOUT = 60 * 1000; // request
  60. // time
  61. // out
  62. // 20
  63. // secs
  64. private static final int SO_TIMEOUT = 60 * 1000; // so
  65. // time
  66. // out
  67. // 20
  68. // secs;
  69. public static int AP_USER_AUTH = 1;
  70. public static int AP_USER_NOT_AUTH = 2;
  71. public static int AP_USER_NOT_EXIST = 3;
  72. public static int AP_SESSION_EXPIRED = 4;
  73. public static int AP_MESSAGE_NEW = 5;
  74. public static int AP_MESSAGE_NONE = 6;
  75. //
  76. public static final int RESULT_FALSE = 0;
  77. public static final int RESULT_TRUE = -1;
  78. public static final int RESULT_NET_ERROR = -3;
  79. public static final int RESULT_NET_NOTAVAILABLE = -4;
  80. public static final int RESULT_ERROR = -5;
  81. public static final int RESULT_LOCALFILE_ERROR = -7;
  82. public static final int RESULT_USERAUTH_ERROR = -9;
  83. public static final int RESULT_UPDATE_USERAUTH_ERROR = -11;
  84. public static final int RESULT_SESSION_EXPIRED = -13;
  85. public static final int RESULT_VER_LOW = -15;
  86. public static String URL_UPDATE_AUTH = "https://ra.apexshipping.com/login.php";
  87. public static String URL_REQUEST_COUNT = "https://ra.apexshipping.com/main.php";
  88. public static String URL_REQUEST_RECORDS = "https://ra.apexshipping.com/main.php";
  89. public static String URL_RETRIEVE_PASS = "https://ra.apexshipping.com/main.php";
  90. public static String URL_ANNOUNCEMENTS = "https://ra.apexshipping.com/mobile_news.php";
  91. public static String URL_NEWS = "https://ra.apexshipping.com/mobile_news.php";
  92. public static String URL_LOCATIONS = "https://ra.apexshipping.com/mobile_news.php";
  93. public static String URL_PUSH = "https://ra.apexshipping.com/main.php";
  94. private static HttpClient getNewHttpClient()
  95. {
  96. try
  97. {
  98. KeyStore trustStore = KeyStore.getInstance(KeyStore
  99. .getDefaultType());
  100. trustStore.load(null, null);
  101. SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
  102. sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
  103. HttpParams params = new BasicHttpParams();
  104. HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  105. HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
  106. HttpConnectionParams.setConnectionTimeout(params, REQUEST_TIMEOUT);
  107. HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);
  108. SchemeRegistry registry = new SchemeRegistry();
  109. registry.register(new Scheme("http", PlainSocketFactory
  110. .getSocketFactory(), 80));
  111. registry.register(new Scheme("https", sf, 443));
  112. ClientConnectionManager ccm = new ThreadSafeClientConnManager(
  113. params, registry);
  114. return new DefaultHttpClient(ccm, params);
  115. }
  116. catch (Exception e)
  117. {
  118. return new DefaultHttpClient();
  119. }
  120. }
  121. public static String get_marketnews(String id, int limit)
  122. {
  123. String TAG = "net_dbg@get_marketnews";
  124. if (!Network.NetworkIsAvailable())
  125. {
  126. Log.d(TAG, "network not available!");
  127. return null; // network not available
  128. }
  129. Bundle parms = new Bundle();
  130. parms.putString("module_name", "Market News");
  131. parms.putString("action_type", "search");
  132. parms.putString("id", id + "");
  133. parms.putString("limit", limit + "");
  134. String jstr = getJson(Network.URL_NEWS, parms);
  135. if (jstr == null || jstr.length() <= 0)
  136. {
  137. Log.d(TAG, "json is wrong");
  138. return null;
  139. }
  140. return jstr;
  141. }
  142. public static String get_servicelocation()
  143. {
  144. String TAG = "net_dbg@get_servicelocation";
  145. if (!Network.NetworkIsAvailable())
  146. {
  147. Log.d(TAG, "network not available!");
  148. return null; // network not available
  149. }
  150. Bundle parms = new Bundle();
  151. parms.putString("module_name", "Location");
  152. parms.putString("action_type", "search");
  153. // parms.putString("id", id+"");
  154. parms.putString("limit", "999");
  155. String jstr = getJson(Network.URL_LOCATIONS, parms);
  156. if (jstr == null || jstr.length() <= 0)
  157. {
  158. Log.d(TAG, "json is wrong");
  159. return null;
  160. }
  161. return jstr;
  162. }
  163. public static String get_announcements(String id, int limit)
  164. {
  165. String TAG = "net_dbg@get_announcements";
  166. if (!Network.NetworkIsAvailable())
  167. {
  168. Log.d(TAG, "network not available!");
  169. return null; // network not available
  170. }
  171. Bundle parms = new Bundle();
  172. parms.putString("module_name", "Announcements");
  173. parms.putString("action_type", "search");
  174. parms.putString("id", id);
  175. parms.putString("limit", limit + "");
  176. String jstr = getJson(Network.URL_ANNOUNCEMENTS, parms);
  177. if (jstr == null || jstr.length() <= 0)
  178. {
  179. Log.d(TAG, "json is wrong");
  180. return null;
  181. }
  182. return jstr;
  183. }
  184. public static int change_pass(String oldpass, String newpass, String user)
  185. {
  186. String TAG = "net_dbg@change_pass";
  187. Log.d(TAG, "old:" + oldpass + ";new:" + newpass);
  188. if (!Network.NetworkIsAvailable())
  189. {
  190. Log.d(TAG, "network not available!");
  191. return Network.RESULT_NET_NOTAVAILABLE; // network not available
  192. }
  193. Bundle parms = new Bundle();
  194. parms.putString("user", user);
  195. parms.putString("oldpass", oldpass);
  196. parms.putString("newpass", newpass);
  197. parms.putString("module_name", "Change Password");
  198. parms.putString("sessionid", ApexTrackingApplication.get_sessionid());
  199. parms.putString("action", "handset_search");
  200. String jstr = getJson(Network.URL_RETRIEVE_PASS, parms);
  201. /*
  202. * error occur while get authorization info from server. include can not
  203. * reach server , wrong parms ,server get wrong , etc.
  204. */
  205. if (jstr == null || jstr.length() <= 0)
  206. {
  207. Log.d(TAG, "json is wrong");
  208. return Network.RESULT_NET_ERROR;
  209. }
  210. JSONObject jsobj;
  211. try
  212. {
  213. jsobj = new JSONObject(jstr);
  214. if (jsobj.length() > 0)
  215. {
  216. int iresult = jsobj.getInt("result");
  217. if (iresult == Network.AP_SESSION_EXPIRED)
  218. {
  219. Log.e(TAG, "AP_SESSION_EXPIRED");
  220. Network.get_Auth(user, oldpass);
  221. jstr = getJson(Network.URL_RETRIEVE_PASS, parms);
  222. jsobj = new JSONObject(jstr);
  223. iresult = jsobj.getInt("result");
  224. }
  225. if (iresult != Network.AP_USER_AUTH)
  226. {
  227. // user not authorized return
  228. Log.d(TAG,
  229. "USER NOT AUTHORIZED CODE="
  230. + jsobj.getInt("result"));
  231. return RESULT_FALSE;
  232. }
  233. else
  234. {
  235. return RESULT_TRUE;
  236. }
  237. }
  238. return RESULT_NET_ERROR;
  239. }
  240. catch (JSONException e)
  241. {
  242. // TODO Auto-generated catch block
  243. e.printStackTrace();
  244. }
  245. return RESULT_NET_ERROR;
  246. }
  247. public static int retrieve_pass(String user, String email)
  248. {
  249. String TAG = "net_dbg@retrieve_pass";
  250. Log.d(TAG, "u:" + user + ";e:" + email);
  251. if (!Network.NetworkIsAvailable())
  252. {
  253. Log.d(TAG, "network not available!");
  254. return Network.RESULT_NET_NOTAVAILABLE; // network not available
  255. }
  256. Bundle parms = new Bundle();
  257. parms.putString("user", user);
  258. parms.putString("email", email);
  259. parms.putString("module_name", "Retrieve Password");
  260. parms.putString("action", "handset_search");
  261. String jstr = getJson(Network.URL_RETRIEVE_PASS, parms);
  262. /*
  263. * error occur while get authorization info from server. include can not
  264. * reach server , wrong parms ,server get wrong , etc.
  265. */
  266. if (jstr == null || jstr.length() <= 0)
  267. {
  268. Log.d(TAG, "json is wrong");
  269. return Network.RESULT_NET_ERROR;
  270. }
  271. JSONObject jsobj;
  272. // array = new JSONArray(json);
  273. try
  274. {
  275. jsobj = new JSONObject(jstr);
  276. if (jsobj.length() > 0)
  277. {
  278. int iresult = jsobj.getInt("result");
  279. if (iresult != Network.AP_USER_AUTH)
  280. {
  281. // user not authorized return
  282. Log.d(TAG,
  283. "USER NOT AUTHORIZED CODE="
  284. + jsobj.getInt("result"));
  285. return RESULT_FALSE;
  286. }
  287. return RESULT_TRUE;
  288. }
  289. return RESULT_NET_ERROR;
  290. }
  291. catch (JSONException e)
  292. {
  293. // TODO Auto-generated catch block
  294. e.printStackTrace();
  295. }
  296. return RESULT_NET_ERROR;
  297. }
  298. public static String getJson(String url, Bundle parms)
  299. {
  300. String TAG = "net_dbg@GetJson";
  301. Log.d(TAG, "entry");
  302. parms.putString("sessionid", ApexTrackingApplication.get_sessionid());
  303. // if (true)
  304. // return fakegetJson(url);
  305. try
  306. {
  307. // BasicHttpParams httpParams = new BasicHttpParams();
  308. // HttpConnectionParams.setConnectionTimeout(httpParams,
  309. // REQUEST_TIMEOUT);
  310. // HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
  311. HttpClient client = getNewHttpClient();// new
  312. // DefaultHttpClient(httpParams);
  313. HttpPost post = new HttpPost(url);
  314. MultipartEntity reqEntity = new MultipartEntity(
  315. HttpMultipartMode.BROWSER_COMPATIBLE);
  316. Set<String> keys = parms.keySet();
  317. Log.d(TAG, "================parms============");
  318. for (String key : keys)
  319. {
  320. if (key.contains("_file"))
  321. {
  322. ;
  323. }
  324. else
  325. reqEntity.addPart(key, new StringBody(parms.get(key)
  326. .toString()));
  327. Log.d(TAG, "key=" + key + " val="
  328. + parms.get(key).toString());
  329. }
  330. Log.d(TAG, "================parms============");
  331. post.setEntity(reqEntity);
  332. HttpResponse response = client.execute(post);
  333. int statucode = response.getStatusLine().getStatusCode();
  334. if (statucode == HttpStatus.SC_OK)
  335. {
  336. HttpEntity resEntity = response.getEntity();
  337. if (resEntity != null)
  338. {
  339. Log.d(TAG,
  340. "Response: content len==>"
  341. + resEntity.getContentLength());
  342. InputStream is = resEntity.getContent();
  343. try
  344. {
  345. BufferedReader br = new BufferedReader(
  346. new InputStreamReader(is, "utf-8"), 8);
  347. StringBuilder sb = new StringBuilder();
  348. String line = null;
  349. while ((line = br.readLine()) != null)
  350. {
  351. sb.append(line + "\n");
  352. }
  353. Log.d(TAG, "Response: content begin");
  354. Log.d(TAG, sb.toString());
  355. Log.d(TAG, "Response: content end");
  356. if (sb.length() <= 0)
  357. {
  358. return null;
  359. }
  360. return sb.toString();
  361. }
  362. catch (Exception e)
  363. {
  364. Log.e(TAG, e.toString());
  365. return null;
  366. // TODO: handle exception
  367. }
  368. finally
  369. {
  370. is.close();
  371. }
  372. }
  373. else
  374. {
  375. /*
  376. * resEntity is null
  377. */
  378. Log.d(TAG, "RESPONSE ENTITY IS NULL");
  379. return null;
  380. }
  381. }
  382. else
  383. {
  384. /*
  385. * Http error; out put error code;
  386. */
  387. Log.d(TAG, "HTTP " + statucode);
  388. HttpEntity resEntity = response.getEntity();
  389. if (resEntity != null)
  390. {
  391. InputStream is = resEntity.getContent();
  392. try
  393. {
  394. BufferedReader br = new BufferedReader(
  395. new InputStreamReader(is, "utf-8"), 8);
  396. StringBuilder sb = new StringBuilder();
  397. String line = null;
  398. while ((line = br.readLine()) != null)
  399. {
  400. sb.append(line + "\n");
  401. }
  402. Log.d(TAG, "Response: content begin");
  403. Log.d(TAG, sb.toString());
  404. Log.d(TAG, "Response: content end");
  405. return null;
  406. }
  407. catch (Exception e)
  408. {
  409. Log.e(TAG, e.toString());
  410. return null;
  411. // TODO: handle exception
  412. }
  413. finally
  414. {
  415. is.close();
  416. }
  417. }
  418. else
  419. {
  420. /*
  421. * resEntity is null
  422. */
  423. Log.e(TAG, "RESPONSE ENTITY IS NULL");
  424. return null;
  425. }
  426. }
  427. }
  428. catch (ConnectTimeoutException e)
  429. {
  430. Log.d(TAG, "request time out");
  431. return null;
  432. }
  433. catch (Exception e)
  434. {
  435. Log.d(TAG, e.toString());
  436. return null;
  437. }
  438. }
  439. public static int get_recordcount(Bundle parms)
  440. {
  441. String TAG = "net_dbg@get_recordcount";
  442. parms.putString("action", "handset_search_count");
  443. parms.putString("sessionid", ApexTrackingApplication.get_sessionid());
  444. Log.d(TAG, "sessionid=" + ApexTrackingApplication.get_sessionid());
  445. String jstr = getJson(Network.URL_REQUEST_COUNT, parms);
  446. if (jstr == null || jstr.length() <= 0)
  447. {
  448. Log.d(TAG, "json is wrong");
  449. return Network.RESULT_NET_ERROR;
  450. }
  451. JSONObject jsobj;
  452. // array = new JSONArray(json);
  453. try
  454. {
  455. jsobj = new JSONObject(jstr);
  456. if (jsobj.length() > 0)
  457. {
  458. int iresult = jsobj.getInt("result");
  459. if (iresult == Network.AP_SESSION_EXPIRED)
  460. {
  461. Log.e(TAG, "AP_SESSION_EXPIRED");
  462. String user, pass;
  463. user = ApexTrackingApplication.get_user();
  464. pass = ApexTrackingApplication.get_pass();
  465. Network.get_Auth(user, pass);
  466. jstr = getJson(Network.URL_REQUEST_COUNT, parms);
  467. jsobj = new JSONObject(jstr);
  468. iresult = jsobj.getInt("result");
  469. }
  470. if (iresult != Network.AP_USER_AUTH)
  471. {
  472. // session expired
  473. Log.d(TAG,
  474. "USER NOT AUTHORIZED CODE="
  475. + jsobj.getInt("result"));
  476. return RESULT_SESSION_EXPIRED;
  477. // if (get_Auth(name, password) == RESULT_TRUE)
  478. // {
  479. // return get_recordcount(name, password, parms);
  480. //
  481. // }
  482. // else
  483. // {
  484. //
  485. // Log.d(TAG,
  486. // "USER NOT AUTHORIZED CODE="
  487. // + jsobj.getInt("result"));
  488. // return RESULT_SESSION_EXPIRED;
  489. // }
  490. }
  491. else
  492. {
  493. ApexTrackingApplication.SetAuthorizeStatus(true);
  494. return jsobj.getInt("count");
  495. }
  496. }
  497. else
  498. {
  499. Log.d(TAG, "json is wrong");
  500. return Network.RESULT_NET_ERROR;
  501. }
  502. }
  503. catch (JSONException e)
  504. {
  505. // TODO Auto-generated catch block
  506. e.printStackTrace();
  507. }
  508. return RESULT_ERROR;
  509. }
  510. public static String get_records(String name, String password, Bundle parms)
  511. {
  512. String TAG = "net_dbg@get_records";
  513. // parms.putString("user", name);
  514. // parms.putString("password", password);
  515. // if (!Network.NetworkIsAvailable())
  516. // {
  517. // Log.d(TAG, "network not available!");
  518. // return "Network.RESULT_NET_NOTAVAILABLE"; // network not available
  519. // }
  520. parms.putString("action", "handset_search");
  521. parms.putString("sessionid", ApexTrackingApplication.get_sessionid());
  522. String jstr = getJson(Network.URL_REQUEST_RECORDS, parms);
  523. if (jstr == null || jstr.length() <= 0)
  524. {
  525. Log.d(TAG, "json is wrong");
  526. return null;
  527. }
  528. JSONObject jsobj;
  529. //
  530. // array = new JSONArray(json);
  531. try
  532. {
  533. jsobj = new JSONObject(jstr);
  534. if (jsobj.length() > 0)
  535. {
  536. int iresult = jsobj.getInt("result");
  537. if (iresult == Network.AP_SESSION_EXPIRED)
  538. {
  539. Log.e(TAG, "AP_SESSION_EXPIRED");
  540. String user, pass;
  541. user = ApexTrackingApplication.get_user();
  542. pass = ApexTrackingApplication.get_pass();
  543. Network.get_Auth(user, pass);
  544. jstr = getJson(Network.URL_REQUEST_RECORDS, parms);
  545. jsobj = new JSONObject(jstr);
  546. iresult = jsobj.getInt("result");
  547. }
  548. if (iresult != Network.AP_USER_AUTH)
  549. {
  550. // session expired
  551. Log.d(TAG,
  552. "USER NOT AUTHORIZED CODE="
  553. + jsobj.getInt("result"));
  554. return null;
  555. // if (get_Auth(name, password) == RESULT_TRUE)
  556. // {
  557. // return get_records(name, password, parms);
  558. //
  559. // }
  560. // else
  561. // {
  562. //
  563. // Log.d(TAG,
  564. // "USER NOT AUTHORIZED CODE="
  565. // + jsobj.getInt("result"));
  566. // return null;
  567. // }
  568. }
  569. else
  570. {
  571. ApexTrackingApplication.SetAuthorizeStatus(true);
  572. return jstr;
  573. }
  574. }
  575. else
  576. {
  577. Log.d(TAG, "json is wrong");
  578. return null;
  579. }
  580. }
  581. catch (JSONException e)
  582. {
  583. // TODO Auto-generated catch block
  584. e.printStackTrace();
  585. }
  586. return null;
  587. // return RESULT_ERROR;
  588. }
  589. public static String get_push(String e_id, String s_id, int limit,
  590. int offset)
  591. {
  592. String TAG = "net_dbg@get_push";
  593. Bundle parms = new Bundle();
  594. parms.putString("action", "handset_search");
  595. parms.putString("e_id", e_id);
  596. parms.putString("s_id", s_id);
  597. parms.putString("limit", limit + "");
  598. parms.putString("offset", offset + "");
  599. parms.putString("action_type", "detail");
  600. parms.putString("module_name", "Pull Message");
  601. parms.putString("uuid", commonUtil.getDUID());
  602. String sessionid = ApexTrackingApplication.get_sessionid();
  603. // if (TextUtils.isEmpty(sessionid))
  604. // {
  605. //
  606. // Log.d(TAG, "no sessionid return!");
  607. // return null;
  608. //
  609. // }
  610. String user = ApexTrackingApplication.get_user();
  611. // if (TextUtils.isEmpty(user))
  612. // {
  613. //
  614. // Log.d(TAG, "no user name return!");
  615. // return null;
  616. //
  617. // }
  618. parms.putString("sessionid", sessionid);
  619. parms.putString("user", user);
  620. // SQLiteDatabase db =
  621. // dbUtil.OpenDB(ApexTrackingApplication.get_instance(), null, false);
  622. // Cursor cursor = db.query("push_message", new String[] {"MAX(e_id)"},
  623. // "user = '"+ApexTrackingApplication.get_user()+"'", null, null, null,
  624. // null, null);
  625. // if (cursor.moveToNext())
  626. // {
  627. // String maxid = cursor.getString(0);
  628. // if(!TextUtils.isEmpty(maxid))
  629. // parms.putString("id", maxid);
  630. // }
  631. // dbUtil.CloseCursor(cursor);
  632. // dbUtil.CloseDB(db);
  633. String jstr = getJson(Network.URL_PUSH, parms);
  634. if (jstr == null || jstr.length() <= 0)
  635. {
  636. Log.d(TAG, "json is wrong");
  637. return null;
  638. }
  639. JSONObject jsobj;
  640. //
  641. // array = new JSONArray(json);
  642. try
  643. {
  644. jsobj = new JSONObject(jstr);
  645. if (jsobj.length() > 0)
  646. {
  647. int iresult = jsobj.getInt("result");
  648. if (iresult == Network.AP_SESSION_EXPIRED)
  649. {
  650. Log.e(TAG, "AP_SESSION_EXPIRED");
  651. String pass;
  652. pass = ApexTrackingApplication.get_pass();
  653. Network.get_Auth(user, pass);
  654. jstr = getJson(Network.URL_REQUEST_RECORDS, parms);
  655. jsobj = new JSONObject(jstr);
  656. iresult = jsobj.getInt("result");
  657. }
  658. if (iresult != Network.AP_MESSAGE_NEW)
  659. {
  660. // session expired
  661. Log.d(TAG,
  662. "DOSE NOT GET NEW MESSAGE CODE="
  663. + jsobj.getInt("result"));
  664. return null;
  665. // if (get_Auth(name, password) == RESULT_TRUE)
  666. // {
  667. // return get_records(name, password, parms);
  668. //
  669. // }
  670. // else
  671. // {
  672. //
  673. // Log.d(TAG,
  674. // "USER NOT AUTHORIZED CODE="
  675. // + jsobj.getInt("result"));
  676. // return null;
  677. // }
  678. }
  679. else
  680. {
  681. return jstr;
  682. }
  683. }
  684. else
  685. {
  686. Log.d(TAG, "json is wrong");
  687. return null;
  688. }
  689. }
  690. catch (JSONException e)
  691. {
  692. // TODO Auto-generated catch block
  693. e.printStackTrace();
  694. }
  695. return null;
  696. // return RESULT_ERROR;
  697. }
  698. public static String check_push()
  699. {
  700. String TAG = "net_dbg@check_push";
  701. Bundle parms = new Bundle();
  702. parms.putString("action", "handset_search");
  703. parms.putString("module_name", "Pull Message");
  704. parms.putString("uuid", commonUtil.getDUID());
  705. String sessionid = ApexTrackingApplication.get_sessionid();
  706. // if (TextUtils.isEmpty(sessionid))
  707. // {
  708. //
  709. // Log.d(TAG, "no sessionid return!");
  710. // return null;
  711. //
  712. // }
  713. String user = ApexTrackingApplication.get_user();
  714. if (TextUtils.isEmpty(user))
  715. {
  716. Log.d(TAG, "no user name return!");
  717. return null;
  718. }
  719. // parms.putString("sessionid", sessionid);
  720. parms.putString("user", user);
  721. SQLiteDatabase db = dbUtil.OpenDB(
  722. ApexTrackingApplication.get_instance(), null, false);
  723. Cursor cursor = db.query("push_message", new String[] { "MAX(e_id)" },
  724. "user = '" + ApexTrackingApplication.get_user() + "'", null,
  725. null, null, null, null);
  726. if (cursor.moveToNext())
  727. {
  728. String maxid = cursor.getString(0);
  729. if (!TextUtils.isEmpty(maxid))
  730. parms.putString("id", maxid);
  731. }
  732. dbUtil.CloseCursor(cursor);
  733. dbUtil.CloseDB(db);
  734. String jstr = getJson(Network.URL_PUSH, parms);
  735. if (jstr == null || jstr.length() <= 0)
  736. {
  737. Log.d(TAG, "json is wrong");
  738. return null;
  739. }
  740. JSONObject jsobj;
  741. //
  742. // array = new JSONArray(json);
  743. try
  744. {
  745. jsobj = new JSONObject(jstr);
  746. if (jsobj.length() > 0)
  747. {
  748. int iresult = jsobj.getInt("result");
  749. if (iresult == Network.AP_SESSION_EXPIRED)
  750. {
  751. Log.e(TAG, "AP_SESSION_EXPIRED");
  752. String pass;
  753. pass = ApexTrackingApplication.get_pass();
  754. Network.get_Auth(user, pass);
  755. jstr = getJson(Network.URL_PUSH, parms);
  756. jsobj = new JSONObject(jstr);
  757. iresult = jsobj.getInt("result");
  758. }
  759. if (iresult != Network.AP_MESSAGE_NEW)
  760. {
  761. // session expired
  762. Log.d(TAG,
  763. "DOSE NOT GET NEW MESSAGE CODE="
  764. + jsobj.getInt("result"));
  765. return null;
  766. // if (get_Auth(name, password) == RESULT_TRUE)
  767. // {
  768. // return get_records(name, password, parms);
  769. //
  770. // }
  771. // else
  772. // {
  773. //
  774. // Log.d(TAG,
  775. // "USER NOT AUTHORIZED CODE="
  776. // + jsobj.getInt("result"));
  777. // return null;
  778. // }
  779. }
  780. else
  781. {
  782. return jstr;
  783. }
  784. }
  785. else
  786. {
  787. Log.d(TAG, "json is wrong");
  788. return null;
  789. }
  790. }
  791. catch (JSONException e)
  792. {
  793. // TODO Auto-generated catch block
  794. e.printStackTrace();
  795. }
  796. return null;
  797. // return RESULT_ERROR;
  798. }
  799. public static String get_detail(Bundle parms)
  800. {
  801. String TAG = "net_dbg@get_detail";
  802. parms.putString("action", "handset_search");
  803. parms.putString("sessionid", ApexTrackingApplication.get_sessionid());
  804. String jstr = getJson(Network.URL_REQUEST_RECORDS, parms);
  805. if (jstr == null || jstr.length() <= 0)
  806. {
  807. Log.d(TAG, "json is wrong");
  808. return null;
  809. }
  810. JSONObject jsobj;
  811. //
  812. // array = new JSONArray(json);
  813. try
  814. {
  815. jsobj = new JSONObject(jstr);
  816. if (jsobj.length() > 0)
  817. {
  818. int iresult = jsobj.getInt("result");
  819. if (iresult == Network.AP_SESSION_EXPIRED)
  820. {
  821. Log.e(TAG, "AP_SESSION_EXPIRED");
  822. String user, pass;
  823. user = ApexTrackingApplication.get_user();
  824. pass = ApexTrackingApplication.get_pass();
  825. Network.get_Auth(user, pass);
  826. jstr = getJson(Network.URL_REQUEST_RECORDS, parms);
  827. jsobj = new JSONObject(jstr);
  828. iresult = jsobj.getInt("result");
  829. }
  830. if (iresult != Network.AP_USER_AUTH)
  831. {
  832. // session expired
  833. Log.d(TAG,
  834. "USER NOT AUTHORIZED CODE="
  835. + jsobj.getInt("result"));
  836. return null;
  837. // if (get_Auth(name, password) == RESULT_TRUE)
  838. // {
  839. // return get_records(name, password, parms);
  840. //
  841. // }
  842. // else
  843. // {
  844. //
  845. // Log.d(TAG,
  846. // "USER NOT AUTHORIZED CODE="
  847. // + jsobj.getInt("result"));
  848. // return null;
  849. // }
  850. }
  851. else
  852. {
  853. ApexTrackingApplication.SetAuthorizeStatus(true);
  854. return jstr;
  855. }
  856. }
  857. else
  858. {
  859. Log.d(TAG, "json is wrong");
  860. return null;
  861. }
  862. }
  863. catch (JSONException e)
  864. {
  865. // TODO Auto-generated catch block
  866. e.printStackTrace();
  867. }
  868. return null;
  869. // return RESULT_ERROR;
  870. }
  871. public static int get_Auth(String name, String password)
  872. {
  873. String TAG = "net_dbg@get_Auth";
  874. Log.d(TAG, "u:" + name + ";p:" + password);
  875. if (!Network.NetworkIsAvailable())
  876. {
  877. Log.d(TAG, "network not available!");
  878. return Network.RESULT_NET_NOTAVAILABLE; // network not available
  879. }
  880. Bundle parms = new Bundle();
  881. parms.putString("user", name);
  882. parms.putString("password", password);
  883. parms.putString("action", "handset_login");
  884. int ver = ApexTrackingApplication
  885. .get_instance()
  886. .getSharedPreferences(name + "_Apex_auth", Context.MODE_PRIVATE)
  887. .getInt("AuthInfoVer", 0);
  888. parms.putString("auth_ver", ver + "");
  889. String jstr = getJson(Network.URL_UPDATE_AUTH, parms);
  890. /*
  891. * error occur while get authorization info from server. include can not
  892. * reach server , wrong parms ,server get wrong , etc.
  893. */
  894. if (jstr == null || jstr.length() <= 0)
  895. {
  896. Log.d(TAG, "json is wrong");
  897. return Network.RESULT_NET_ERROR;
  898. }
  899. return parse_authinfo(jstr, name, password);
  900. }
  901. private static int parse_authinfo(String json, String user, String pass)
  902. {
  903. String TAG = "net_dbg@parse_authinfo";
  904. Log.d(TAG, json);
  905. // JSONArray array;
  906. JSONObject jsobj;
  907. SQLiteDatabase db = null;
  908. try
  909. {
  910. // array = new JSONArray(json);
  911. jsobj = new JSONObject(json);
  912. if (jsobj.length() > 0)
  913. {
  914. // JSONObject objresult = jsobj.get.getJSONObject(0);// result
  915. // the
  916. // server result
  917. // ;
  918. if (jsobj.getInt("result") != Network.AP_USER_AUTH)
  919. {
  920. ApexTrackingApplication.logout();
  921. // user not authorized return
  922. Log.d(TAG,
  923. "USER NOT AUTHORIZED CODE="
  924. + jsobj.getInt("result"));
  925. return RESULT_FALSE;
  926. }
  927. JSONObject objheader = jsobj.getJSONObject("header");
  928. String required_ver = objheader.getString("client_ver");
  929. String current_ver = ApexTrackingApplication.get_instance()
  930. .getPackageManager().getPackageInfo("com.usai.apex", 0).versionName;
  931. if (current_ver.compareTo(required_ver) < 0)
  932. return RESULT_VER_LOW;
  933. // JSONObject objsessionid = jsobj.getJSONObject(1); // session
  934. // id
  935. // on
  936. // server;
  937. // ApexTrackingApplication.put_sessionid(objheader
  938. // .getString("sessionid"));
  939. // ApexTrackingApplication.put_user(user);
  940. // ApexTrackingApplication.put_password(pass);
  941. // ApexTrackingApplication.put_authorization(true);
  942. ApexTrackingApplication.login(objheader.getString("sessionid"),
  943. user, pass);
  944. Log.d(TAG, "sessionid=" + objheader.getString("sessionid"));
  945. // JSONObject objupdate = array.getJSONObject(3); // whether
  946. // need
  947. // update ;
  948. if (objheader.getBoolean("update") == false)
  949. {
  950. // no update on the server;
  951. return RESULT_TRUE;
  952. }
  953. // JSONObject objver = array.getJSONObject(4); // new version
  954. // get
  955. // from server;
  956. Editor editor = ApexTrackingApplication
  957. .get_instance()
  958. .getSharedPreferences(user + "_Apex_auth",
  959. Context.MODE_PRIVATE).edit();
  960. editor.putInt("AuthInfoVer", objheader.getInt("ver"));
  961. JSONObject objfuncs = jsobj.getJSONObject("functions");
  962. Iterator<?> it = objfuncs.keys();
  963. db = dbUtil.OpenDB(ApexTrackingApplication.get_instance(),
  964. null, true);
  965. String sql = "insert into fields_info(name,aname,field_type,function_name,behavior,priority,show,user) values(?,?,?,?,?,?,?,?)";
  966. String sql1 = "insert into actions_info(name,function_name,user,priority) values(?,?,?,?)";
  967. SQLiteStatement stat = db.compileStatement(sql);
  968. SQLiteStatement stat1 = db.compileStatement(sql1);
  969. db.beginTransaction();
  970. db.execSQL("update fields_info set abandon = 1 where user ='"
  971. + user + "'");
  972. db.execSQL("update actions_info set abandon = 1 where user ='"
  973. + user + "'");
  974. String field_name, field_type, alias_name;
  975. while (it.hasNext()) // loop for each function
  976. {
  977. String func_name = (String) it.next();
  978. // Set<String> funset = new HashSet<String>();
  979. JSONObject objfun = objfuncs.getJSONObject(func_name);
  980. int behavior, priority = 999;
  981. int show = 1;
  982. JSONObject objbehavior = objfun.getJSONObject("search");
  983. Iterator<?> itbehavior = objbehavior.keys();
  984. behavior = BEHAVIOR_SEARCH;
  985. while (itbehavior.hasNext()) // loop for search fields in
  986. // certain function
  987. {
  988. String field = (String) itbehavior.next();
  989. JSONObject field_info = objbehavior
  990. .getJSONObject(field);
  991. field_name = field_info.getString("name");
  992. field_type = field_info.getString("type");
  993. alias_name = field_info.getString("alias");
  994. priority = field_info.getInt("priority");
  995. if (priority == 999)
  996. show = 0;
  997. else
  998. show = 1;
  999. int id = dbUtil.get_recordid(db, "fields_info",
  1000. "name='" + field_name + "' and behavior="
  1001. + behavior + " and function_name='"
  1002. + func_name + "' and user='" + user
  1003. + "'");
  1004. if (id >= 0) // record exist;
  1005. {
  1006. db.execSQL("update fields_info set abandon = 0 where _id ="
  1007. + id);
  1008. }
  1009. else
  1010. {
  1011. stat.bindString(1, field_name);
  1012. stat.bindString(2, alias_name);
  1013. stat.bindString(3, field_type);
  1014. stat.bindString(4, func_name);
  1015. stat.bindLong(5, behavior);
  1016. stat.bindLong(6, priority);
  1017. stat.bindLong(7, show);
  1018. stat.bindString(8, user);
  1019. stat.executeInsert();
  1020. }
  1021. }
  1022. objbehavior = objfun.getJSONObject("result");
  1023. Iterator<?> itresult = objbehavior.keys();
  1024. behavior = BEHAVIOR_RESULT;
  1025. while (itresult.hasNext()) // loop for result fields in
  1026. // certain function
  1027. {
  1028. String field = (String) itresult.next();
  1029. JSONObject field_info = objbehavior
  1030. .getJSONObject(field);
  1031. field_name = field_info.getString("name");
  1032. field_type = field_info.getString("type");
  1033. alias_name = field_info.getString("alias");
  1034. priority = field_info.getInt("priority");
  1035. if (priority == 999)
  1036. show = 0;
  1037. else
  1038. show = 1;
  1039. int id = dbUtil.get_recordid(db, "fields_info",
  1040. "name='" + field_name + "' and behavior="
  1041. + behavior + " and function_name='"
  1042. + func_name + "' and user='" + user
  1043. + "'");
  1044. if (id >= 0) // record exist;
  1045. {
  1046. db.execSQL("update fields_info set abandon = 0 where _id ="
  1047. + id);
  1048. }
  1049. else
  1050. {
  1051. stat.bindString(1, field_name);
  1052. stat.bindString(2, alias_name);
  1053. stat.bindString(3, field_type);
  1054. stat.bindString(4, func_name);
  1055. stat.bindLong(5, behavior);
  1056. stat.bindLong(6, priority);
  1057. stat.bindLong(7, show);
  1058. stat.bindString(8, user);
  1059. stat.executeInsert();
  1060. }
  1061. }
  1062. // objfun.has("actions")
  1063. objbehavior = objfun.getJSONObject("actions");
  1064. Iterator<?> itactions = objbehavior.keys();
  1065. // behavior = BEHAVIOR_RESULT;
  1066. while (itactions.hasNext()) // loop for result fields in
  1067. // certain function
  1068. {
  1069. String actionname = (String) itactions.next();
  1070. int action_priority = objbehavior.getInt(actionname);
  1071. int id = dbUtil.get_recordid(db, "actions_info",
  1072. "name='" + actionname + "' and function_name='"
  1073. + func_name + "' and user='" + user
  1074. + "'");
  1075. if (id >= 0) // record exist;
  1076. {
  1077. db.execSQL("update actions_info set abandon = 0 , priority="
  1078. + action_priority + " where _id =" + id);
  1079. }
  1080. else
  1081. {
  1082. stat1.bindString(1, actionname);
  1083. stat1.bindString(2, func_name);
  1084. stat1.bindString(3, user);
  1085. stat1.bindLong(4, action_priority);
  1086. stat1.executeInsert();
  1087. }
  1088. }
  1089. }
  1090. db.execSQL("delete from fields_info where abandon = 1");
  1091. db.execSQL("delete from actions_info where abandon = 1");
  1092. db.setTransactionSuccessful();
  1093. db.endTransaction();
  1094. editor.commit();
  1095. return RESULT_TRUE;
  1096. }
  1097. Log.d(TAG, "json is wrong");
  1098. return RESULT_USERAUTH_ERROR;
  1099. }
  1100. catch (JSONException e1)
  1101. {
  1102. // TODO Auto-generated catch block
  1103. e1.printStackTrace();
  1104. Log.d(TAG, "json is wrong");
  1105. }
  1106. catch (NameNotFoundException e)
  1107. {
  1108. // TODO Auto-generated catch block
  1109. e.printStackTrace();
  1110. }
  1111. finally
  1112. {
  1113. dbUtil.CloseDB(db);
  1114. }
  1115. // try
  1116. // {
  1117. // m_RemoteVerCode = Integer
  1118. // .parseInt(obj.getString("verCode"));
  1119. // m_RemoteVerName = obj.getString("verName");
  1120. // m_NewVerUrl = obj.getString("URL");
  1121. // }
  1122. // catch (Exception e)
  1123. // {
  1124. // m_RemoteVerCode = -1;
  1125. // m_RemoteVerName = "";
  1126. // m_NewVerUrl = "";
  1127. // dbgUtil.Log(Log.INFO,TAG, "download failed json file wrong!");
  1128. // return false;
  1129. // }
  1130. Log.d(TAG, "json is wrong");
  1131. return RESULT_USERAUTH_ERROR;
  1132. }
  1133. // private void writeData(JSONObject allData)
  1134. // {
  1135. //
  1136. // File file = new File(Environment.getExternalStorageDirectory()
  1137. // .toString() + File.separator + "live.txt");
  1138. // if (!file.exists())
  1139. // {
  1140. // try
  1141. // {
  1142. // file.createNewFile();
  1143. // }
  1144. // catch (IOException e)
  1145. // {
  1146. // e.printStackTrace();
  1147. // }
  1148. // }
  1149. // FileOutputStream fos;
  1150. // OutputStreamWriter osw = null;
  1151. // try
  1152. // {
  1153. // fos = new FileOutputStream(file);
  1154. //
  1155. // osw = new OutputStreamWriter(fos, "UTF-8");
  1156. // osw.write(allData.toString());
  1157. // osw.flush();
  1158. // }
  1159. // catch (FileNotFoundException e)
  1160. // {
  1161. // e.printStackTrace();
  1162. // }
  1163. // catch (UnsupportedEncodingException e)
  1164. // {
  1165. // e.printStackTrace();
  1166. // }
  1167. // catch (IOException e)
  1168. // {
  1169. // e.printStackTrace();
  1170. // }
  1171. // finally
  1172. // {
  1173. // if (osw != null)
  1174. // {
  1175. // try
  1176. // {
  1177. // osw.close();
  1178. // }
  1179. // catch (IOException e)
  1180. // {
  1181. // e.printStackTrace();
  1182. // }
  1183. // }
  1184. // }
  1185. // }
  1186. public static boolean NetworkIsAvailable()// Context context)
  1187. {
  1188. String TAG = "net_dbg@CheckNetwork";
  1189. ConnectivityManager connManager = (ConnectivityManager) ApexTrackingApplication
  1190. .get_instance().getSystemService(Context.CONNECTIVITY_SERVICE);
  1191. // .getSystemService(Context.CONNECTIVITY_SERVICE);
  1192. NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
  1193. if (networkInfo == null)
  1194. {
  1195. Log.d(TAG, "can not get Active NetworkInfo!");
  1196. // dbgUtil.Log(Log.DEBUG, "Current Network info",
  1197. // "can not get Active NetworkInfo!");
  1198. return false;
  1199. }
  1200. NetworkInfo.State netState = networkInfo.getState();
  1201. if (netState != NetworkInfo.State.CONNECTED)
  1202. {
  1203. Log.d(TAG, "not Connected!State=" + netState);
  1204. // dbgUtil.Log(Log.DEBUG, "Current Network info",
  1205. // "not Connected!State=" + netState);
  1206. return false;
  1207. }
  1208. // int iconntype = -1;
  1209. // iconntype = networkInfo.getType();
  1210. // boolean bUseMobileNetwork = context.getSharedPreferences(
  1211. // "PhoneAsstPref", 0).getBoolean("UseMobileNetwork", false);
  1212. //
  1213. // if (bUseMobileNetwork == false
  1214. // && iconntype != ConnectivityManager.TYPE_WIFI && iconntype != 9/*
  1215. // earthnet */)
  1216. // {
  1217. // Log.d(TAG,);
  1218. // dbgUtil.Log(Log.DEBUG, "Current Network info",
  1219. // "not allowed!Connection type=" + networkInfo.getTypeName());
  1220. // return false;
  1221. // }
  1222. boolean bavailable = networkInfo.isAvailable();
  1223. String strtype = networkInfo.getTypeName();
  1224. Log.d(TAG, " type = " + strtype + " abailable = " + bavailable
  1225. + " state " + netState);
  1226. // dbgUtil.Log(Log.INFO, "Current Network info", " type = " + strtype
  1227. // + " abailable = " + bavailable + " state " + netState);
  1228. return bavailable;
  1229. }
  1230. public static class SSLSocketFactoryEx extends SSLSocketFactory
  1231. {
  1232. SSLContext sslContext = SSLContext.getInstance("TLS");
  1233. public SSLSocketFactoryEx(KeyStore truststore)
  1234. throws NoSuchAlgorithmException, KeyManagementException,
  1235. KeyStoreException, UnrecoverableKeyException
  1236. {
  1237. super(truststore);
  1238. TrustManager tm = new X509TrustManager()
  1239. {
  1240. public java.security.cert.X509Certificate[] getAcceptedIssuers()
  1241. {
  1242. return null;
  1243. }
  1244. @Override
  1245. public void checkClientTrusted(
  1246. java.security.cert.X509Certificate[] chain,
  1247. String authType)
  1248. throws java.security.cert.CertificateException
  1249. {
  1250. }
  1251. @Override
  1252. public void checkServerTrusted(
  1253. java.security.cert.X509Certificate[] chain,
  1254. String authType)
  1255. throws java.security.cert.CertificateException
  1256. {
  1257. }
  1258. };
  1259. sslContext.init(null, new TrustManager[] { tm }, null);
  1260. }
  1261. @Override
  1262. public Socket createSocket(Socket socket, String host, int port,
  1263. boolean autoClose) throws IOException, UnknownHostException
  1264. {
  1265. return sslContext.getSocketFactory().createSocket(socket, host,
  1266. port, autoClose);
  1267. }
  1268. @Override
  1269. public Socket createSocket() throws IOException
  1270. {
  1271. return sslContext.getSocketFactory().createSocket();
  1272. }
  1273. }
  1274. }