Network.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. package com.usai.util;
  2. import java.io.BufferedReader;
  3. import java.io.File;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import org.apache.http.HttpEntity;
  7. import org.apache.http.HttpResponse;
  8. import org.apache.http.HttpStatus;
  9. import org.apache.http.client.HttpClient;
  10. import org.apache.http.client.methods.HttpPost;
  11. import org.apache.http.conn.ConnectTimeoutException;
  12. import org.apache.http.entity.mime.HttpMultipartMode;
  13. import org.apache.http.entity.mime.MultipartEntity;
  14. import org.apache.http.entity.mime.content.FileBody;
  15. import org.apache.http.entity.mime.content.StringBody;
  16. import org.apache.http.impl.client.DefaultHttpClient;
  17. import org.apache.http.params.BasicHttpParams;
  18. import org.apache.http.params.HttpConnectionParams;
  19. import org.json.JSONObject;
  20. import android.content.Context;
  21. import android.net.ConnectivityManager;
  22. import android.net.NetworkInfo;
  23. import android.util.Log;
  24. public class Network
  25. {
  26. private static final int REQUEST_TIMEOUT = 5 * 1000;// request time out 20
  27. // secs
  28. private static final int SO_TIMEOUT = 5 * 1000; // so time out 20 secs;
  29. public static int AP_USER_AUTH = 1;
  30. public static int AP_USER_NOT_AUTH = 2;
  31. public static int AP_USER_NOT_EXIST = 3;
  32. public static int AP_UPLOAD_SUCCESS = 4;
  33. public static int AP_UPLOAD_FAIL = 5;
  34. //
  35. public static final int RESULT_FALSE = 0;
  36. public static final int RESULT_TRUE = 1;
  37. public static final int RESULT_NET_ERROR = 3;
  38. public static final int RESULT_ERROR = 5;
  39. public static final int RESULT_LOCALFILE_ERROR= 7;
  40. public static final int RESULT_USERAUTH_ERROR = 9;
  41. // public static final int RESULT_NOCONNECT = 2;
  42. // public static final int RESULT_CONNECT = 3;
  43. // public static final int RESULT_TIMEOUT = 4;
  44. // public static final int RESULT_RESPONSE_NULL= 6;
  45. // public static final int RESULT_ERR_USERAUTH= 8;
  46. // enum RES_VERIFYUSER{RESULT_FALSE,RESULT_TRUE,}
  47. // public static int PROTOCAL_FALSE = 0;
  48. // public static int PROTOCAL_TRUE = 1;
  49. // public static int PROTOCAL_NOT_AUTH = 2;
  50. // public static int PROTOCAL_SUCCESS = 1;
  51. // public static int PROTOCAL_FAILED = 1;
  52. // public static int PROTOCAL_MORE=4;
  53. // public static int ERR_LOCALFILE = 0;
  54. // static String URL_JSON_TEST = "http://192.168.23.1/xampp/json_test.php";
  55. // static String URL_VERIFY_USER = "http://192.168.1.10/xampp/verify_user.php";
  56. // static String URL_UPLOAD_PHOTO = "http://192.168.1.10/xampp/save_upload_file.php";
  57. static String URL_VERIFY_USER = "http://192.168.23.1/xampp/verify_user.php";
  58. static String URL_UPLOAD_PHOTO = "http://192.168.23.1/xampp/save_upload_file.php";
  59. // public static String VerifyUser(String user, String password,
  60. // Context context)
  61. // {
  62. // String TAG = "net_dbg@VerifyUser";
  63. // Log.d(TAG, "entry");
  64. // try
  65. // {
  66. // BasicHttpParams httpParams = new BasicHttpParams();
  67. // HttpConnectionParams.setConnectionTimeout(httpParams,
  68. // REQUEST_TIMEOUT);
  69. // HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
  70. // HttpClient client = new DefaultHttpClient(httpParams);
  71. // HttpPost post = new HttpPost(URL_VERIFY_USER);
  72. // MultipartEntity reqEntity = new MultipartEntity(
  73. // HttpMultipartMode.BROWSER_COMPATIBLE);
  74. // reqEntity.addPart("user", new StringBody(user));
  75. // reqEntity.addPart("password", new StringBody(password));
  76. //
  77. // post.setEntity(reqEntity);
  78. // HttpResponse response = client.execute(post);
  79. // int statucode = response.getStatusLine().getStatusCode();
  80. // if (statucode == HttpStatus.SC_OK)
  81. // {
  82. // HttpEntity resEntity = response.getEntity();
  83. // if (resEntity != null)
  84. // {
  85. // Log.d(TAG,
  86. // "Response: content len==>"
  87. // + resEntity.getContentLength());
  88. // InputStream is = resEntity.getContent();
  89. // try
  90. // {
  91. //
  92. // BufferedReader br = new BufferedReader(
  93. // new InputStreamReader(is, "utf-8"), 8);
  94. // StringBuilder sb = new StringBuilder();
  95. // String line = null;
  96. // while ((line = br.readLine()) != null)
  97. // {
  98. // sb.append(line + "\n");
  99. // }
  100. //
  101. // Log.d(TAG, "Response: content begin");
  102. // Log.d(TAG, sb.toString());
  103. // Log.d(TAG, "Response: content end");
  104. // return sb.toString();
  105. // }
  106. // catch (Exception e)
  107. // {
  108. // Log.e(TAG, e.toString());
  109. // // TODO: handle exception
  110. // }
  111. // finally
  112. // {
  113. // is.close();
  114. //
  115. // }
  116. //
  117. // }
  118. // else
  119. // {
  120. // /*
  121. // * resEntity is null
  122. // */
  123. // Log.d(TAG, "RESPONSE ENTITY IS NULL");
  124. // return null;
  125. // }
  126. //
  127. // }
  128. // else
  129. // {
  130. // /*
  131. // * Http error; out put error code;
  132. // */
  133. // Log.d(TAG, "HTTP " + statucode);
  134. //
  135. // HttpEntity resEntity = response.getEntity();
  136. // if (resEntity != null)
  137. // {
  138. //
  139. // InputStream is = resEntity.getContent();
  140. // try
  141. // {
  142. //
  143. // BufferedReader br = new BufferedReader(
  144. // new InputStreamReader(is, "utf-8"), 8);
  145. // StringBuilder sb = new StringBuilder();
  146. // String line = null;
  147. // while ((line = br.readLine()) != null)
  148. // {
  149. // sb.append(line + "\n");
  150. // }
  151. //
  152. // Log.d(TAG, "Response: content begin");
  153. // Log.d(TAG, sb.toString());
  154. // Log.d(TAG, "Response: content end");
  155. // return null;
  156. // }
  157. // catch (Exception e)
  158. // {
  159. // Log.e(TAG, e.toString());
  160. // // TODO: handle exception
  161. // }
  162. // finally
  163. // {
  164. // is.close();
  165. //
  166. // }
  167. //
  168. // }
  169. // else
  170. // {
  171. // /*
  172. // * resEntity is null
  173. // */
  174. // Log.e(TAG, "RESPONSE ENTITY IS NULL");
  175. // return null;
  176. // }
  177. // return null;
  178. // }
  179. //
  180. // }
  181. // catch (ConnectTimeoutException e)
  182. // {
  183. // Log.d(TAG, "request time out");
  184. //
  185. // }
  186. // catch (Exception e)
  187. // {
  188. // Log.d(TAG, e.toString());
  189. // }
  190. // return null;
  191. // }
  192. public static int VerifyUser(String user, String password)
  193. {
  194. String TAG = "net_dbg@VerifyUser";
  195. Log.d(TAG, "entry");
  196. try
  197. {
  198. BasicHttpParams httpParams = new BasicHttpParams();
  199. HttpConnectionParams.setConnectionTimeout(httpParams,
  200. REQUEST_TIMEOUT);
  201. HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
  202. HttpClient client = new DefaultHttpClient(httpParams);
  203. HttpPost post = new HttpPost(URL_VERIFY_USER);
  204. MultipartEntity reqEntity = new MultipartEntity(
  205. HttpMultipartMode.BROWSER_COMPATIBLE);
  206. reqEntity.addPart("user", new StringBody(user));
  207. reqEntity.addPart("password", new StringBody(password));
  208. post.setEntity(reqEntity);
  209. HttpResponse response = client.execute(post);
  210. int statucode = response.getStatusLine().getStatusCode();
  211. if (statucode == HttpStatus.SC_OK)
  212. {
  213. HttpEntity resEntity = response.getEntity();
  214. if (resEntity != null)
  215. {
  216. Log.d(TAG,
  217. "Response: content len==>"
  218. + resEntity.getContentLength());
  219. InputStream is = resEntity.getContent();
  220. try
  221. {
  222. BufferedReader br = new BufferedReader(
  223. new InputStreamReader(is, "utf-8"), 8);
  224. StringBuilder sb = new StringBuilder();
  225. String line = null;
  226. while ((line = br.readLine()) != null)
  227. {
  228. sb.append(line + "\n");
  229. }
  230. Log.d(TAG, "Response: content begin");
  231. Log.d(TAG, sb.toString());
  232. Log.d(TAG, "Response: content end");
  233. if (sb.length()<=0)
  234. {
  235. return RESULT_NET_ERROR;
  236. }
  237. JSONObject obj = new JSONObject(sb.toString());
  238. if (obj.length() > 0)
  239. {
  240. // JSONObject obj = array.getJSONObject(0);
  241. try
  242. {
  243. int verifyresult = Integer.parseInt(obj
  244. .getString("result"));
  245. if (verifyresult == Network.AP_USER_AUTH)
  246. {
  247. return RESULT_TRUE;
  248. }
  249. else
  250. {
  251. return RESULT_FALSE;
  252. }
  253. }
  254. catch (Exception e)
  255. {
  256. Log.e(TAG, e.toString());
  257. return RESULT_ERROR;
  258. }
  259. }
  260. }
  261. catch (Exception e)
  262. {
  263. Log.e(TAG, e.toString());
  264. return RESULT_ERROR;
  265. // TODO: handle exception
  266. }
  267. finally
  268. {
  269. is.close();
  270. }
  271. }
  272. else
  273. {
  274. /*
  275. * resEntity is null
  276. */
  277. Log.d(TAG, "RESPONSE ENTITY IS NULL");
  278. return RESULT_NET_ERROR;
  279. }
  280. }
  281. else
  282. {
  283. /*
  284. * Http error; out put error code;
  285. */
  286. Log.d(TAG, "HTTP " + statucode);
  287. HttpEntity resEntity = response.getEntity();
  288. if (resEntity != null)
  289. {
  290. InputStream is = resEntity.getContent();
  291. try
  292. {
  293. BufferedReader br = new BufferedReader(
  294. new InputStreamReader(is, "utf-8"), 8);
  295. StringBuilder sb = new StringBuilder();
  296. String line = null;
  297. while ((line = br.readLine()) != null)
  298. {
  299. sb.append(line + "\n");
  300. }
  301. Log.d(TAG, "Response: content begin");
  302. Log.d(TAG, sb.toString());
  303. Log.d(TAG, "Response: content end");
  304. return RESULT_ERROR;
  305. }
  306. catch (Exception e)
  307. {
  308. Log.e(TAG, e.toString());
  309. return RESULT_ERROR;
  310. // TODO: handle exception
  311. }
  312. finally
  313. {
  314. is.close();
  315. }
  316. }
  317. else
  318. {
  319. /*
  320. * resEntity is null
  321. */
  322. Log.e(TAG, "RESPONSE ENTITY IS NULL");
  323. return RESULT_NET_ERROR;
  324. }
  325. }
  326. }
  327. catch (ConnectTimeoutException e)
  328. {
  329. Log.d(TAG, "request time out");
  330. return RESULT_NET_ERROR;
  331. }
  332. catch (Exception e)
  333. {
  334. Log.d(TAG, e.toString());
  335. return RESULT_ERROR;
  336. }
  337. return RESULT_ERROR;
  338. }
  339. public static int UploadFile( String path, String user,
  340. String password, String pid)
  341. {
  342. String TAG = "net_dbg@VerifyUser";
  343. Log.d(TAG, "entry");
  344. Log.d(TAG, "sourcefile:" + path);
  345. File file = new File(path);
  346. if (file.exists() == false)
  347. return RESULT_LOCALFILE_ERROR;
  348. String md5= MD5.md5sum(path);
  349. if(md5==null)
  350. {
  351. Log.d(TAG, "md5 sum failed");
  352. return RESULT_LOCALFILE_ERROR;
  353. }
  354. try
  355. {
  356. BasicHttpParams httpParams = new BasicHttpParams();
  357. HttpConnectionParams.setConnectionTimeout(httpParams,
  358. REQUEST_TIMEOUT);
  359. HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
  360. HttpClient client = new DefaultHttpClient(httpParams);
  361. HttpPost post = new HttpPost(URL_UPLOAD_PHOTO);
  362. MultipartEntity reqEntity = new MultipartEntity(
  363. HttpMultipartMode.BROWSER_COMPATIBLE);
  364. reqEntity.addPart("user", new StringBody(user));
  365. reqEntity.addPart("password", new StringBody(password));
  366. reqEntity.addPart("pid", new StringBody(pid));
  367. reqEntity.addPart("md5", new StringBody(md5));
  368. FileBody bin = new FileBody(file);
  369. reqEntity.addPart("imagefile", bin);
  370. post.setEntity(reqEntity);
  371. HttpResponse response = client.execute(post);
  372. int statucode = response.getStatusLine().getStatusCode();
  373. if (statucode == HttpStatus.SC_OK)
  374. {
  375. HttpEntity resEntity = response.getEntity();
  376. if (resEntity != null)
  377. {
  378. Log.d(TAG,
  379. "Response: content len==>"
  380. + resEntity.getContentLength());
  381. InputStream is = resEntity.getContent();
  382. try
  383. {
  384. BufferedReader br = new BufferedReader(
  385. new InputStreamReader(is, "utf-8"), 8);
  386. StringBuilder sb = new StringBuilder();
  387. String line = null;
  388. while ((line = br.readLine()) != null)
  389. {
  390. sb.append(line + "\n");
  391. }
  392. Log.d(TAG, "Response: content begin");
  393. Log.d(TAG, sb.toString());
  394. Log.d(TAG, "Response: content end");
  395. if (sb.length()<=0)
  396. {
  397. return RESULT_NET_ERROR;
  398. }
  399. JSONObject obj = new JSONObject(sb.toString());
  400. if (obj.length() > 0)
  401. {
  402. // JSONObject obj = array.getJSONObject(0);
  403. try
  404. {
  405. int uploadresult = Integer.parseInt(obj
  406. .getString("result"));
  407. if (uploadresult == Network.AP_UPLOAD_SUCCESS)
  408. {
  409. return RESULT_TRUE;
  410. }
  411. else if(uploadresult == Network.AP_USER_NOT_AUTH)
  412. {
  413. Log.d(TAG,"RESULT_ERR_USERAUTH");
  414. return RESULT_USERAUTH_ERROR;
  415. }
  416. else
  417. {
  418. return RESULT_FALSE;
  419. }
  420. }
  421. catch (Exception e)
  422. {
  423. Log.e(TAG, e.toString());
  424. return RESULT_FALSE;
  425. }
  426. }
  427. }
  428. catch (Exception e)
  429. {
  430. Log.e(TAG, e.toString());
  431. return RESULT_ERROR;
  432. // TODO: handle exception
  433. }
  434. finally
  435. {
  436. is.close();
  437. }
  438. }
  439. else
  440. {
  441. /*
  442. * resEntity is null
  443. */
  444. Log.d(TAG, "RESPONSE ENTITY IS NULL");
  445. return RESULT_NET_ERROR;
  446. }
  447. }
  448. else
  449. {
  450. /*
  451. * Http error; out put error code;
  452. */
  453. Log.d(TAG, "HTTP " + statucode);
  454. HttpEntity resEntity = response.getEntity();
  455. if (resEntity != null)
  456. {
  457. InputStream is = resEntity.getContent();
  458. try
  459. {
  460. BufferedReader br = new BufferedReader(
  461. new InputStreamReader(is, "utf-8"), 8);
  462. StringBuilder sb = new StringBuilder();
  463. String line = null;
  464. while ((line = br.readLine()) != null)
  465. {
  466. sb.append(line + "\n");
  467. }
  468. Log.d(TAG, "Response: content begin");
  469. Log.d(TAG, sb.toString());
  470. Log.d(TAG, "Response: content end");
  471. return RESULT_ERROR;
  472. }
  473. catch (Exception e)
  474. {
  475. Log.e(TAG, e.toString());
  476. return RESULT_ERROR;
  477. // TODO: handle exception
  478. }
  479. finally
  480. {
  481. is.close();
  482. }
  483. }
  484. else
  485. {
  486. /*
  487. * resEntity is null
  488. */
  489. Log.e(TAG, "RESPONSE ENTITY IS NULL");
  490. return RESULT_NET_ERROR;
  491. }
  492. }
  493. }
  494. catch (ConnectTimeoutException e)
  495. {
  496. Log.d(TAG, "request time out");
  497. return RESULT_NET_ERROR;
  498. }
  499. catch (Exception e)
  500. {
  501. Log.d(TAG, e.toString());
  502. return RESULT_ERROR;
  503. }
  504. return RESULT_FALSE;
  505. // try
  506. // {
  507. // HttpClient client = new DefaultHttpClient();
  508. // HttpPost post = new HttpPost(url);
  509. // MultipartEntity reqEntity = new MultipartEntity(
  510. // HttpMultipartMode.BROWSER_COMPATIBLE);
  511. // reqEntity.addPart("user", new StringBody(user));
  512. // reqEntity.addPart("password", new StringBody(password));
  513. // reqEntity.addPart("pid", new StringBody(pid));
  514. // FileBody bin = new FileBody(file);
  515. // reqEntity.addPart("imagefile", bin);
  516. // post.setEntity(reqEntity);
  517. // HttpResponse response = client.execute(post);
  518. // HttpEntity resEntity = response.getEntity();
  519. // if (resEntity != null)
  520. // {
  521. //
  522. // String result = EntityUtils.toString(resEntity);
  523. // Log.d(TAG, "Response:" + result);
  524. // return Integer.parseInt(result);
  525. // // if(Boolean.parseBoolean(result)==true)
  526. // // return true;
  527. // // else
  528. // // return false;
  529. // }
  530. //
  531. // }
  532. // catch (Exception e)
  533. // {
  534. // e.printStackTrace();
  535. // }
  536. // return 0;
  537. }
  538. public static boolean NetworkIsAvailable(Context context)
  539. {
  540. String TAG = "net_dbg@CheckNetwork";
  541. ConnectivityManager connManager = (ConnectivityManager) context
  542. .getSystemService(Context.CONNECTIVITY_SERVICE);
  543. NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
  544. if (networkInfo == null)
  545. {
  546. Log.d(TAG, "can not get Active NetworkInfo!");
  547. // dbgUtil.Log(Log.DEBUG, "Current Network info",
  548. // "can not get Active NetworkInfo!");
  549. return false;
  550. }
  551. NetworkInfo.State netState = networkInfo.getState();
  552. if (netState != NetworkInfo.State.CONNECTED)
  553. {
  554. Log.d(TAG, "not Connected!State=" + netState);
  555. // dbgUtil.Log(Log.DEBUG, "Current Network info",
  556. // "not Connected!State=" + netState);
  557. return false;
  558. }
  559. // int iconntype = -1;
  560. // iconntype = networkInfo.getType();
  561. // boolean bUseMobileNetwork = context.getSharedPreferences(
  562. // "PhoneAsstPref", 0).getBoolean("UseMobileNetwork", false);
  563. //
  564. // if (bUseMobileNetwork == false
  565. // && iconntype != ConnectivityManager.TYPE_WIFI && iconntype != 9/*
  566. // earthnet */)
  567. // {
  568. // Log.d(TAG,);
  569. // dbgUtil.Log(Log.DEBUG, "Current Network info",
  570. // "not allowed!Connection type=" + networkInfo.getTypeName());
  571. // return false;
  572. // }
  573. boolean bavailable = networkInfo.isAvailable();
  574. String strtype = networkInfo.getTypeName();
  575. Log.d(TAG, " type = " + strtype + " abailable = " + bavailable
  576. + " state " + netState);
  577. // dbgUtil.Log(Log.INFO, "Current Network info", " type = " + strtype
  578. // + " abailable = " + bavailable + " state " + netState);
  579. return bavailable;
  580. }
  581. }