|
|
@@ -0,0 +1,986 @@
|
|
|
+package com.usai.util;
|
|
|
+
|
|
|
+import java.io.BufferedReader;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.FileOutputStream;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.InputStream;
|
|
|
+import java.io.InputStreamReader;
|
|
|
+import java.io.OutputStreamWriter;
|
|
|
+import java.io.UnsupportedEncodingException;
|
|
|
+import java.net.HttpURLConnection;
|
|
|
+import java.net.MalformedURLException;
|
|
|
+import java.net.URL;
|
|
|
+import java.util.HashSet;
|
|
|
+import java.util.Iterator;
|
|
|
+import java.util.Set;
|
|
|
+
|
|
|
+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.JSONArray;
|
|
|
+import org.json.JSONException;
|
|
|
+import org.json.JSONObject;
|
|
|
+
|
|
|
+import com.usai.apex.ApexTrackingApplication;
|
|
|
+
|
|
|
+import android.R.integer;
|
|
|
+import android.R.string;
|
|
|
+import android.content.Context;
|
|
|
+import android.content.Intent;
|
|
|
+import android.content.SharedPreferences.Editor;
|
|
|
+import android.net.ConnectivityManager;
|
|
|
+import android.net.NetworkInfo;
|
|
|
+import android.os.Bundle;
|
|
|
+import android.os.Environment;
|
|
|
+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_SESSION_EXPIRED = 4;
|
|
|
+
|
|
|
+ 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_NET_NOTAVAILABLE = 4;
|
|
|
+ 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_UPDATE_USERAUTH_ERROR = 11;
|
|
|
+ // 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";
|
|
|
+ public static String URL_VERIFY_USER = "http://192.168.23.1/xampp/apex_user.php";
|
|
|
+ public static String URL_UPDATE_AUTH = "http://192.168.23.1/xampp/apex_auth.php";
|
|
|
+ public static String FAKE_URL_UPDATE_AUTH = "http://192.168.23.1/xampp/auth.json";
|
|
|
+ public static String FAKE_SEARCH = "http://192.168.23.1/xampp/recordset.json";
|
|
|
+
|
|
|
+ // static String URL_UPLOAD_PHOTO =
|
|
|
+ // "http://192.168.23.1/xampp/save_upload_file.php";
|
|
|
+
|
|
|
+ private static String fakegetJson(String url)
|
|
|
+ {
|
|
|
+ return download(url, 10000);
|
|
|
+ // return null;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String download(String urlStr, int timeout)
|
|
|
+ {
|
|
|
+ String TAG = "net_dbg@download";
|
|
|
+
|
|
|
+ Log.d(TAG, urlStr);
|
|
|
+
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
+ String line = null;
|
|
|
+ BufferedReader buffer = null;
|
|
|
+ HttpURLConnection urlConn = null;
|
|
|
+ try
|
|
|
+ {
|
|
|
+ // 创建一个URL对象
|
|
|
+ URL url = new URL(urlStr);
|
|
|
+ // 创建一个Http连接
|
|
|
+ urlConn = (HttpURLConnection) url.openConnection();
|
|
|
+
|
|
|
+ urlConn.setConnectTimeout(timeout);
|
|
|
+ urlConn.setReadTimeout(timeout);
|
|
|
+ // urlConn.connect();
|
|
|
+
|
|
|
+ int irespon = urlConn.getResponseCode();
|
|
|
+ if (irespon != HttpURLConnection.HTTP_OK)
|
|
|
+ {
|
|
|
+ Log.e(TAG, "HTTP ERROR CODE " + irespon + " url: " + urlStr);
|
|
|
+ urlConn.disconnect();
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 使用IO流读取数据
|
|
|
+ buffer = new BufferedReader(new InputStreamReader(
|
|
|
+ urlConn.getInputStream()));
|
|
|
+ while ((line = buffer.readLine()) != null)
|
|
|
+ {
|
|
|
+ sb.append(line);
|
|
|
+ }
|
|
|
+ Log.d(TAG, "download success @" + urlStr);
|
|
|
+ }
|
|
|
+ catch (MalformedURLException e)
|
|
|
+ {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ catch (IOException e)
|
|
|
+ {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ if (urlConn != null)
|
|
|
+ urlConn.disconnect();
|
|
|
+ if (buffer != null)
|
|
|
+ buffer.close();
|
|
|
+ }
|
|
|
+ catch (Exception e)
|
|
|
+ {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static String getJson(String url, Bundle parms)
|
|
|
+ {
|
|
|
+ String TAG = "net_dbg@GetJson";
|
|
|
+ Log.d(TAG, "entry");
|
|
|
+
|
|
|
+ if (true)
|
|
|
+ return fakegetJson(url);
|
|
|
+ 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);
|
|
|
+ MultipartEntity reqEntity = new MultipartEntity(
|
|
|
+ HttpMultipartMode.BROWSER_COMPATIBLE);
|
|
|
+
|
|
|
+ Set<String> keys = parms.keySet();
|
|
|
+ for (String key : keys)
|
|
|
+ {
|
|
|
+ if (key.contains("_file"))
|
|
|
+ {
|
|
|
+ ;
|
|
|
+ }
|
|
|
+ else
|
|
|
+ reqEntity.addPart(key, new StringBody(parms.get(key)
|
|
|
+ .toString()));
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ 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 null;
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ // 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 null;
|
|
|
+ // 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());
|
|
|
+ return null;
|
|
|
+ // TODO: handle exception
|
|
|
+ }
|
|
|
+ finally
|
|
|
+ {
|
|
|
+ is.close();
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ else
|
|
|
+ {
|
|
|
+ /*
|
|
|
+ * resEntity is null
|
|
|
+ */
|
|
|
+ Log.e(TAG, "RESPONSE ENTITY IS NULL");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ catch (ConnectTimeoutException e)
|
|
|
+ {
|
|
|
+ Log.d(TAG, "request time out");
|
|
|
+ return null;
|
|
|
+
|
|
|
+ }
|
|
|
+ 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;
|
|
|
+ //
|
|
|
+ // 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));
|
|
|
+ // 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 int get_Auth(String name, String password)
|
|
|
+ {
|
|
|
+
|
|
|
+ String TAG = "net_dbg@get_Auth";
|
|
|
+ if (!Network.NetworkIsAvailable())
|
|
|
+ {
|
|
|
+ Log.d(TAG, "network not available!");
|
|
|
+ return Network.RESULT_NET_NOTAVAILABLE; // network not available
|
|
|
+ }
|
|
|
+ Bundle parms = new Bundle();
|
|
|
+ parms.putString("user", name);
|
|
|
+ parms.putString("password", password);
|
|
|
+ int ver = ApexTrackingApplication.get_instance()
|
|
|
+ .getSharedPreferences("Apex_auth", Context.MODE_PRIVATE)
|
|
|
+ .getInt("AuthInfoVer", 0);
|
|
|
+ parms.putString("auth_ver", ver + "");
|
|
|
+ String jstr = getJson(Network.FAKE_URL_UPDATE_AUTH, parms);
|
|
|
+ /*
|
|
|
+ * error occur while get authorization info from server. include can not
|
|
|
+ * reach server , wrong parms ,server get wrong , etc.
|
|
|
+ */
|
|
|
+ if (jstr == null || jstr.length() <= 0)
|
|
|
+ {
|
|
|
+ Log.d(TAG, "json is wrong");
|
|
|
+ return Network.RESULT_NET_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ return parse_authinfo(jstr);
|
|
|
+ }
|
|
|
+
|
|
|
+ private static int parse_authinfo(String json)
|
|
|
+ {
|
|
|
+ String TAG = "net_dbg@parse_authinfo";
|
|
|
+ // JSONArray array;
|
|
|
+ JSONObject jsobj;
|
|
|
+ try
|
|
|
+
|
|
|
+ {
|
|
|
+
|
|
|
+ // array = new JSONArray(json);
|
|
|
+ jsobj = new JSONObject(json);
|
|
|
+ ;
|
|
|
+ if (jsobj.length() > 0)
|
|
|
+ {
|
|
|
+ // JSONObject objresult = jsobj.get.getJSONObject(0);// result
|
|
|
+ // the
|
|
|
+ // server result
|
|
|
+ // ;
|
|
|
+ if (jsobj.getInt("result") != Network.AP_USER_AUTH)
|
|
|
+ {
|
|
|
+ // user not authorized return
|
|
|
+ Log.d(TAG,
|
|
|
+ "USER NOT AUTHORIZED CODE="
|
|
|
+ + jsobj.getInt("result"));
|
|
|
+ return RESULT_FALSE;
|
|
|
+ }
|
|
|
+
|
|
|
+ JSONObject objheader = jsobj.getJSONObject("header");
|
|
|
+ // JSONObject objsessionid = jsobj.getJSONObject(1); // session
|
|
|
+ // id
|
|
|
+ // on
|
|
|
+ // server;
|
|
|
+ ApexTrackingApplication.put_sessionid(objheader
|
|
|
+ .getInt("sessionid"));
|
|
|
+ // JSONObject objupdate = array.getJSONObject(3); // whether
|
|
|
+ // need
|
|
|
+ // update ;
|
|
|
+ if (objheader.getBoolean("update") == false)
|
|
|
+ {
|
|
|
+ // no update on the server;
|
|
|
+ return RESULT_TRUE;
|
|
|
+ }
|
|
|
+ // JSONObject objver = array.getJSONObject(4); // new version
|
|
|
+ // get
|
|
|
+ // from server;
|
|
|
+
|
|
|
+ Editor editor = ApexTrackingApplication
|
|
|
+ .get_instance()
|
|
|
+ .getSharedPreferences("Apex_auth", Context.MODE_PRIVATE)
|
|
|
+ .edit();
|
|
|
+
|
|
|
+ editor.putInt("AuthInfoVer", objheader.getInt("ver"));
|
|
|
+ JSONObject objfuncs = jsobj.getJSONObject("functions");
|
|
|
+ Iterator it = objfuncs.keys();
|
|
|
+ while (it.hasNext())
|
|
|
+ {
|
|
|
+ String key = (String) it.next();
|
|
|
+
|
|
|
+// Set<String> funset = new HashSet<String>();
|
|
|
+ JSONObject objfun = objfuncs.getJSONObject(key);
|
|
|
+
|
|
|
+ editor.putString(key,objfun.toString());
|
|
|
+ // JSONArray arrfun = jsobj.get(i);
|
|
|
+ // JSONObject objname = arrfun.getJSONObject(0);
|
|
|
+ //
|
|
|
+// funset.add(objfun.getString("name"));
|
|
|
+// funset.add(objfun.getJSONObject("search").toString());
|
|
|
+// funset.add(objfun.getJSONObject("result").toString());
|
|
|
+// editor.putStringSet(objfun.getString("name"), funset);
|
|
|
+ // String value = obj.getString(key);
|
|
|
+ // JSONArray array = obj.getJSONArray(key);
|
|
|
+ // for (int i = 0; i < array.length(); i++)
|
|
|
+ // {
|
|
|
+ // JSONObject jsonobject = array.getJSONObject(i);
|
|
|
+ // jsonobject.put("name", key);
|
|
|
+ // jsonobject.put("exp",
|
|
|
+ // key + "=" + jsonobject.getString("value"));
|
|
|
+ // newArray.put(jsonobject);
|
|
|
+ // }
|
|
|
+ }
|
|
|
+ // while (jsobj)
|
|
|
+ // for (int i = 4; i < jsobj.length(); i++) // get functions
|
|
|
+ // auth
|
|
|
+ // // info from server;
|
|
|
+ // {
|
|
|
+ // Set<String> funset = new HashSet<String>();
|
|
|
+ //
|
|
|
+ // JSONArray arrfun = jsobj.get(i);
|
|
|
+ // JSONObject objname = arrfun.getJSONObject(0);
|
|
|
+ //
|
|
|
+ // funset.add(objname.getString("name"));
|
|
|
+ // funset.add(jsobj.getJSONArray(1).toString());
|
|
|
+ // funset.add(jsobj.getJSONArray(2).toString());
|
|
|
+ // editor.putStringSet("function" + (i - 4), funset);
|
|
|
+ // }
|
|
|
+ editor.commit();
|
|
|
+ return RESULT_TRUE;
|
|
|
+ }
|
|
|
+ Log.d(TAG, "json is wrong");
|
|
|
+ return RESULT_USERAUTH_ERROR;
|
|
|
+ }
|
|
|
+ catch (JSONException e1)
|
|
|
+ {
|
|
|
+ // TODO Auto-generated catch block
|
|
|
+ e1.printStackTrace();
|
|
|
+ Log.d(TAG, "json is wrong");
|
|
|
+ }
|
|
|
+
|
|
|
+ // try
|
|
|
+ // {
|
|
|
+ // m_RemoteVerCode = Integer
|
|
|
+ // .parseInt(obj.getString("verCode"));
|
|
|
+ // m_RemoteVerName = obj.getString("verName");
|
|
|
+ // m_NewVerUrl = obj.getString("URL");
|
|
|
+ // }
|
|
|
+ // catch (Exception e)
|
|
|
+ // {
|
|
|
+ // m_RemoteVerCode = -1;
|
|
|
+ // m_RemoteVerName = "";
|
|
|
+ // m_NewVerUrl = "";
|
|
|
+ // dbgUtil.Log(Log.INFO,TAG, "download failed json file wrong!");
|
|
|
+ // return false;
|
|
|
+ // }
|
|
|
+ Log.d(TAG, "json is wrong");
|
|
|
+ return RESULT_USERAUTH_ERROR;
|
|
|
+ }
|
|
|
+
|
|
|
+ // private void writeData(JSONObject allData)
|
|
|
+ // {
|
|
|
+ //
|
|
|
+ // File file = new File(Environment.getExternalStorageDirectory()
|
|
|
+ // .toString() + File.separator + "live.txt");
|
|
|
+ // if (!file.exists())
|
|
|
+ // {
|
|
|
+ // try
|
|
|
+ // {
|
|
|
+ // file.createNewFile();
|
|
|
+ // }
|
|
|
+ // catch (IOException e)
|
|
|
+ // {
|
|
|
+ // e.printStackTrace();
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // FileOutputStream fos;
|
|
|
+ // OutputStreamWriter osw = null;
|
|
|
+ // try
|
|
|
+ // {
|
|
|
+ // fos = new FileOutputStream(file);
|
|
|
+ //
|
|
|
+ // osw = new OutputStreamWriter(fos, "UTF-8");
|
|
|
+ // osw.write(allData.toString());
|
|
|
+ // osw.flush();
|
|
|
+ // }
|
|
|
+ // catch (FileNotFoundException e)
|
|
|
+ // {
|
|
|
+ // e.printStackTrace();
|
|
|
+ // }
|
|
|
+ // catch (UnsupportedEncodingException e)
|
|
|
+ // {
|
|
|
+ // e.printStackTrace();
|
|
|
+ // }
|
|
|
+ // catch (IOException e)
|
|
|
+ // {
|
|
|
+ // e.printStackTrace();
|
|
|
+ // }
|
|
|
+ // finally
|
|
|
+ // {
|
|
|
+ // if (osw != null)
|
|
|
+ // {
|
|
|
+ // try
|
|
|
+ // {
|
|
|
+ // osw.close();
|
|
|
+ // }
|
|
|
+ // catch (IOException e)
|
|
|
+ // {
|
|
|
+ // e.printStackTrace();
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+ // }
|
|
|
+
|
|
|
+ public static boolean NetworkIsAvailable()// Context context)
|
|
|
+ {
|
|
|
+ String TAG = "net_dbg@CheckNetwork";
|
|
|
+ ConnectivityManager connManager = (ConnectivityManager) ApexTrackingApplication
|
|
|
+ .get_instance().getSystemService(Context.CONNECTIVITY_SERVICE);
|
|
|
+ // .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;
|
|
|
+ }
|
|
|
+}
|