| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632 |
- package com.usai.util;
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import org.apache.http.HttpEntity;
- import org.apache.http.HttpResponse;
- import org.apache.http.HttpStatus;
- import org.apache.http.client.HttpClient;
- import org.apache.http.client.methods.HttpPost;
- import org.apache.http.conn.ConnectTimeoutException;
- import org.apache.http.entity.mime.HttpMultipartMode;
- import org.apache.http.entity.mime.MultipartEntity;
- import org.apache.http.entity.mime.content.FileBody;
- import org.apache.http.entity.mime.content.StringBody;
- import org.apache.http.impl.client.DefaultHttpClient;
- import org.apache.http.params.BasicHttpParams;
- import org.apache.http.params.HttpConnectionParams;
- import org.json.JSONObject;
- import android.content.Context;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.util.Log;
- public class Network
- {
- private static final int REQUEST_TIMEOUT = 5 * 1000;// request time out 20
- // secs
- private static final int SO_TIMEOUT = 5 * 1000; // so time out 20 secs;
- public static int AP_USER_AUTH = 1;
- public static int AP_USER_NOT_AUTH = 2;
- public static int AP_USER_NOT_EXIST = 3;
- public static int AP_UPLOAD_SUCCESS = 4;
- public static int AP_UPLOAD_FAIL = 5;
- //
- public static final int RESULT_FALSE = 0;
- public static final int RESULT_TRUE = 1;
- public static final int RESULT_NET_ERROR = 3;
- public static final int RESULT_ERROR = 5;
- public static final int RESULT_LOCALFILE_ERROR= 7;
- public static final int RESULT_USERAUTH_ERROR = 9;
- // public static final int RESULT_NOCONNECT = 2;
- // public static final int RESULT_CONNECT = 3;
- // public static final int RESULT_TIMEOUT = 4;
- // public static final int RESULT_RESPONSE_NULL= 6;
- // public static final int RESULT_ERR_USERAUTH= 8;
- // enum RES_VERIFYUSER{RESULT_FALSE,RESULT_TRUE,}
- // public static int PROTOCAL_FALSE = 0;
- // public static int PROTOCAL_TRUE = 1;
- // public static int PROTOCAL_NOT_AUTH = 2;
- // public static int PROTOCAL_SUCCESS = 1;
- // public static int PROTOCAL_FAILED = 1;
- // public static int PROTOCAL_MORE=4;
- // public static int ERR_LOCALFILE = 0;
- // static String URL_JSON_TEST = "http://192.168.23.1/xampp/json_test.php";
- // static String URL_VERIFY_USER = "http://192.168.1.10/xampp/verify_user.php";
- // static String URL_UPLOAD_PHOTO = "http://192.168.1.10/xampp/save_upload_file.php";
- static String URL_VERIFY_USER = "http://192.168.23.1/xampp/verify_user.php";
- static String URL_UPLOAD_PHOTO = "http://192.168.23.1/xampp/save_upload_file.php";
- // public static String VerifyUser(String user, String password,
- // Context context)
- // {
- // String TAG = "net_dbg@VerifyUser";
- // Log.d(TAG, "entry");
- // try
- // {
- // BasicHttpParams httpParams = new BasicHttpParams();
- // HttpConnectionParams.setConnectionTimeout(httpParams,
- // REQUEST_TIMEOUT);
- // HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
- // HttpClient client = new DefaultHttpClient(httpParams);
- // HttpPost post = new HttpPost(URL_VERIFY_USER);
- // MultipartEntity reqEntity = new MultipartEntity(
- // HttpMultipartMode.BROWSER_COMPATIBLE);
- // reqEntity.addPart("user", new StringBody(user));
- // reqEntity.addPart("password", new StringBody(password));
- //
- // post.setEntity(reqEntity);
- // HttpResponse response = client.execute(post);
- // int statucode = response.getStatusLine().getStatusCode();
- // if (statucode == HttpStatus.SC_OK)
- // {
- // HttpEntity resEntity = response.getEntity();
- // if (resEntity != null)
- // {
- // Log.d(TAG,
- // "Response: content len==>"
- // + resEntity.getContentLength());
- // InputStream is = resEntity.getContent();
- // try
- // {
- //
- // BufferedReader br = new BufferedReader(
- // new InputStreamReader(is, "utf-8"), 8);
- // StringBuilder sb = new StringBuilder();
- // String line = null;
- // while ((line = br.readLine()) != null)
- // {
- // sb.append(line + "\n");
- // }
- //
- // Log.d(TAG, "Response: content begin");
- // Log.d(TAG, sb.toString());
- // Log.d(TAG, "Response: content end");
- // return sb.toString();
- // }
- // catch (Exception e)
- // {
- // Log.e(TAG, e.toString());
- // // TODO: handle exception
- // }
- // finally
- // {
- // is.close();
- //
- // }
- //
- // }
- // else
- // {
- // /*
- // * resEntity is null
- // */
- // Log.d(TAG, "RESPONSE ENTITY IS NULL");
- // return null;
- // }
- //
- // }
- // else
- // {
- // /*
- // * Http error; out put error code;
- // */
- // Log.d(TAG, "HTTP " + statucode);
- //
- // HttpEntity resEntity = response.getEntity();
- // if (resEntity != null)
- // {
- //
- // InputStream is = resEntity.getContent();
- // try
- // {
- //
- // BufferedReader br = new BufferedReader(
- // new InputStreamReader(is, "utf-8"), 8);
- // StringBuilder sb = new StringBuilder();
- // String line = null;
- // while ((line = br.readLine()) != null)
- // {
- // sb.append(line + "\n");
- // }
- //
- // Log.d(TAG, "Response: content begin");
- // Log.d(TAG, sb.toString());
- // Log.d(TAG, "Response: content end");
- // return null;
- // }
- // catch (Exception e)
- // {
- // Log.e(TAG, e.toString());
- // // TODO: handle exception
- // }
- // finally
- // {
- // is.close();
- //
- // }
- //
- // }
- // else
- // {
- // /*
- // * resEntity is null
- // */
- // Log.e(TAG, "RESPONSE ENTITY IS NULL");
- // return null;
- // }
- // return null;
- // }
- //
- // }
- // catch (ConnectTimeoutException e)
- // {
- // Log.d(TAG, "request time out");
- //
- // }
- // catch (Exception e)
- // {
- // Log.d(TAG, e.toString());
- // }
- // return null;
- // }
- public static int VerifyUser(String user, String password)
- {
- String TAG = "net_dbg@VerifyUser";
- Log.d(TAG, "entry");
- try
- {
- BasicHttpParams httpParams = new BasicHttpParams();
- HttpConnectionParams.setConnectionTimeout(httpParams,
- REQUEST_TIMEOUT);
- HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
- HttpClient client = new DefaultHttpClient(httpParams);
- HttpPost post = new HttpPost(URL_VERIFY_USER);
- MultipartEntity reqEntity = new MultipartEntity(
- HttpMultipartMode.BROWSER_COMPATIBLE);
- reqEntity.addPart("user", new StringBody(user));
- reqEntity.addPart("password", new StringBody(password));
- post.setEntity(reqEntity);
- HttpResponse response = client.execute(post);
- int statucode = response.getStatusLine().getStatusCode();
- if (statucode == HttpStatus.SC_OK)
- {
- HttpEntity resEntity = response.getEntity();
- if (resEntity != null)
- {
- Log.d(TAG,
- "Response: content len==>"
- + resEntity.getContentLength());
- InputStream is = resEntity.getContent();
- try
- {
- BufferedReader br = new BufferedReader(
- new InputStreamReader(is, "utf-8"), 8);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = br.readLine()) != null)
- {
- sb.append(line + "\n");
- }
- Log.d(TAG, "Response: content begin");
- Log.d(TAG, sb.toString());
- Log.d(TAG, "Response: content end");
- if (sb.length()<=0)
- {
-
- return RESULT_NET_ERROR;
- }
- JSONObject obj = new JSONObject(sb.toString());
- if (obj.length() > 0)
- {
- // JSONObject obj = array.getJSONObject(0);
- try
- {
- int verifyresult = Integer.parseInt(obj
- .getString("result"));
- if (verifyresult == Network.AP_USER_AUTH)
- {
- return RESULT_TRUE;
- }
- else
- {
- return RESULT_FALSE;
- }
- }
- catch (Exception e)
- {
- Log.e(TAG, e.toString());
- return RESULT_ERROR;
- }
- }
- }
- catch (Exception e)
- {
- Log.e(TAG, e.toString());
- return RESULT_ERROR;
- // TODO: handle exception
- }
- finally
- {
- is.close();
- }
- }
- else
- {
- /*
- * resEntity is null
- */
- Log.d(TAG, "RESPONSE ENTITY IS NULL");
- return RESULT_NET_ERROR;
- }
- }
- else
- {
- /*
- * Http error; out put error code;
- */
- Log.d(TAG, "HTTP " + statucode);
- HttpEntity resEntity = response.getEntity();
- if (resEntity != null)
- {
- InputStream is = resEntity.getContent();
- try
- {
- BufferedReader br = new BufferedReader(
- new InputStreamReader(is, "utf-8"), 8);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = br.readLine()) != null)
- {
- sb.append(line + "\n");
- }
- Log.d(TAG, "Response: content begin");
- Log.d(TAG, sb.toString());
- Log.d(TAG, "Response: content end");
- return RESULT_ERROR;
- }
- catch (Exception e)
- {
- Log.e(TAG, e.toString());
- return RESULT_ERROR;
- // TODO: handle exception
- }
- finally
- {
- is.close();
- }
- }
- else
- {
- /*
- * resEntity is null
- */
- Log.e(TAG, "RESPONSE ENTITY IS NULL");
- return RESULT_NET_ERROR;
- }
- }
- }
- catch (ConnectTimeoutException e)
- {
- Log.d(TAG, "request time out");
- return RESULT_NET_ERROR;
- }
- catch (Exception e)
- {
- Log.d(TAG, e.toString());
- return RESULT_ERROR;
- }
- return RESULT_ERROR;
- }
- public static int UploadFile( String path, String user,
- String password, String pid)
- {
- String TAG = "net_dbg@VerifyUser";
- Log.d(TAG, "entry");
- Log.d(TAG, "sourcefile:" + path);
- File file = new File(path);
- if (file.exists() == false)
- return RESULT_LOCALFILE_ERROR;
- String md5= MD5.md5sum(path);
- if(md5==null)
- {
- Log.d(TAG, "md5 sum failed");
- return RESULT_LOCALFILE_ERROR;
-
- }
- try
- {
- BasicHttpParams httpParams = new BasicHttpParams();
- HttpConnectionParams.setConnectionTimeout(httpParams,
- REQUEST_TIMEOUT);
- HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
- HttpClient client = new DefaultHttpClient(httpParams);
- HttpPost post = new HttpPost(URL_UPLOAD_PHOTO);
- MultipartEntity reqEntity = new MultipartEntity(
- HttpMultipartMode.BROWSER_COMPATIBLE);
- reqEntity.addPart("user", new StringBody(user));
- reqEntity.addPart("password", new StringBody(password));
- reqEntity.addPart("pid", new StringBody(pid));
- reqEntity.addPart("md5", new StringBody(md5));
-
- FileBody bin = new FileBody(file);
- reqEntity.addPart("imagefile", bin);
- post.setEntity(reqEntity);
- HttpResponse response = client.execute(post);
- int statucode = response.getStatusLine().getStatusCode();
- if (statucode == HttpStatus.SC_OK)
- {
- HttpEntity resEntity = response.getEntity();
- if (resEntity != null)
- {
- Log.d(TAG,
- "Response: content len==>"
- + resEntity.getContentLength());
- InputStream is = resEntity.getContent();
- try
- {
- BufferedReader br = new BufferedReader(
- new InputStreamReader(is, "utf-8"), 8);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = br.readLine()) != null)
- {
- sb.append(line + "\n");
- }
- Log.d(TAG, "Response: content begin");
- Log.d(TAG, sb.toString());
- Log.d(TAG, "Response: content end");
- if (sb.length()<=0)
- {
-
- return RESULT_NET_ERROR;
- }
- JSONObject obj = new JSONObject(sb.toString());
- if (obj.length() > 0)
- {
- // JSONObject obj = array.getJSONObject(0);
- try
- {
- int uploadresult = Integer.parseInt(obj
- .getString("result"));
- if (uploadresult == Network.AP_UPLOAD_SUCCESS)
- {
- return RESULT_TRUE;
- }
- else if(uploadresult == Network.AP_USER_NOT_AUTH)
- {
- Log.d(TAG,"RESULT_ERR_USERAUTH");
- return RESULT_USERAUTH_ERROR;
- }
- else
- {
- return RESULT_FALSE;
- }
- }
- catch (Exception e)
- {
- Log.e(TAG, e.toString());
- return RESULT_FALSE;
- }
- }
- }
- catch (Exception e)
- {
- Log.e(TAG, e.toString());
- return RESULT_ERROR;
- // TODO: handle exception
- }
- finally
- {
- is.close();
- }
- }
- else
- {
- /*
- * resEntity is null
- */
- Log.d(TAG, "RESPONSE ENTITY IS NULL");
- return RESULT_NET_ERROR;
- }
- }
- else
- {
- /*
- * Http error; out put error code;
- */
- Log.d(TAG, "HTTP " + statucode);
- HttpEntity resEntity = response.getEntity();
- if (resEntity != null)
- {
- InputStream is = resEntity.getContent();
- try
- {
- BufferedReader br = new BufferedReader(
- new InputStreamReader(is, "utf-8"), 8);
- StringBuilder sb = new StringBuilder();
- String line = null;
- while ((line = br.readLine()) != null)
- {
- sb.append(line + "\n");
- }
- Log.d(TAG, "Response: content begin");
- Log.d(TAG, sb.toString());
- Log.d(TAG, "Response: content end");
- return RESULT_ERROR;
- }
- catch (Exception e)
- {
- Log.e(TAG, e.toString());
- return RESULT_ERROR;
- // TODO: handle exception
- }
- finally
- {
- is.close();
- }
- }
- else
- {
- /*
- * resEntity is null
- */
- Log.e(TAG, "RESPONSE ENTITY IS NULL");
- return RESULT_NET_ERROR;
- }
- }
- }
- catch (ConnectTimeoutException e)
- {
- Log.d(TAG, "request time out");
- return RESULT_NET_ERROR;
- }
- catch (Exception e)
- {
- Log.d(TAG, e.toString());
- return RESULT_ERROR;
- }
-
- return RESULT_FALSE;
- // try
- // {
- // HttpClient client = new DefaultHttpClient();
- // HttpPost post = new HttpPost(url);
- // MultipartEntity reqEntity = new MultipartEntity(
- // HttpMultipartMode.BROWSER_COMPATIBLE);
- // reqEntity.addPart("user", new StringBody(user));
- // reqEntity.addPart("password", new StringBody(password));
- // reqEntity.addPart("pid", new StringBody(pid));
- // FileBody bin = new FileBody(file);
- // reqEntity.addPart("imagefile", bin);
- // post.setEntity(reqEntity);
- // HttpResponse response = client.execute(post);
- // HttpEntity resEntity = response.getEntity();
- // if (resEntity != null)
- // {
- //
- // String result = EntityUtils.toString(resEntity);
- // Log.d(TAG, "Response:" + result);
- // return Integer.parseInt(result);
- // // if(Boolean.parseBoolean(result)==true)
- // // return true;
- // // else
- // // return false;
- // }
- //
- // }
- // catch (Exception e)
- // {
- // e.printStackTrace();
- // }
- // return 0;
- }
- public static boolean NetworkIsAvailable(Context context)
- {
- String TAG = "net_dbg@CheckNetwork";
- ConnectivityManager connManager = (ConnectivityManager) context
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
- if (networkInfo == null)
- {
- Log.d(TAG, "can not get Active NetworkInfo!");
- // dbgUtil.Log(Log.DEBUG, "Current Network info",
- // "can not get Active NetworkInfo!");
- return false;
- }
- NetworkInfo.State netState = networkInfo.getState();
- if (netState != NetworkInfo.State.CONNECTED)
- {
- Log.d(TAG, "not Connected!State=" + netState);
- // dbgUtil.Log(Log.DEBUG, "Current Network info",
- // "not Connected!State=" + netState);
- return false;
- }
- // int iconntype = -1;
- // iconntype = networkInfo.getType();
- // boolean bUseMobileNetwork = context.getSharedPreferences(
- // "PhoneAsstPref", 0).getBoolean("UseMobileNetwork", false);
- //
- // if (bUseMobileNetwork == false
- // && iconntype != ConnectivityManager.TYPE_WIFI && iconntype != 9/*
- // earthnet */)
- // {
- // Log.d(TAG,);
- // dbgUtil.Log(Log.DEBUG, "Current Network info",
- // "not allowed!Connection type=" + networkInfo.getTypeName());
- // return false;
- // }
- boolean bavailable = networkInfo.isAvailable();
- String strtype = networkInfo.getTypeName();
- Log.d(TAG, " type = " + strtype + " abailable = " + bavailable
- + " state " + netState);
- // dbgUtil.Log(Log.INFO, "Current Network info", " type = " + strtype
- // + " abailable = " + bavailable + " state " + netState);
- return bavailable;
- }
- }
|