|
|
@@ -22,42 +22,22 @@ import java.security.UnrecoverableKeyException;
|
|
|
import java.util.Iterator;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
-import java.util.Set;
|
|
|
import java.util.UUID;
|
|
|
|
|
|
import javax.net.ssl.SSLContext;
|
|
|
import javax.net.ssl.TrustManager;
|
|
|
import javax.net.ssl.X509TrustManager;
|
|
|
|
|
|
-import org.apache.http.HttpEntity;
|
|
|
-import org.apache.http.HttpResponse;
|
|
|
-import org.apache.http.HttpStatus;
|
|
|
-import org.apache.http.HttpVersion;
|
|
|
-import org.apache.http.client.HttpClient;
|
|
|
-import org.apache.http.client.methods.HttpPost;
|
|
|
-import org.apache.http.conn.ClientConnectionManager;
|
|
|
-import org.apache.http.conn.ConnectTimeoutException;
|
|
|
-import org.apache.http.conn.scheme.PlainSocketFactory;
|
|
|
-import org.apache.http.conn.scheme.Scheme;
|
|
|
-import org.apache.http.conn.scheme.SchemeRegistry;
|
|
|
import org.apache.http.conn.ssl.SSLSocketFactory;
|
|
|
-import org.apache.http.entity.mime.HttpMultipartMode;
|
|
|
-import org.apache.http.entity.mime.MultipartEntity;
|
|
|
-import org.apache.http.entity.mime.content.StringBody;
|
|
|
-import org.apache.http.impl.client.DefaultHttpClient;
|
|
|
-import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
|
|
|
-import org.apache.http.params.BasicHttpParams;
|
|
|
-import org.apache.http.params.HttpConnectionParams;
|
|
|
-import org.apache.http.params.HttpParams;
|
|
|
-import org.apache.http.params.HttpProtocolParams;
|
|
|
-import org.apache.http.protocol.HTTP;
|
|
|
+
|
|
|
+
|
|
|
import org.json.JSONArray;
|
|
|
import org.json.JSONException;
|
|
|
import org.json.JSONObject;
|
|
|
|
|
|
+
|
|
|
import com.usai.apex.ApexTrackingApplication;
|
|
|
-import com.usai.apex.CustomizeFieldsActivity.fieldedit;
|
|
|
-import com.usai.apex.R;
|
|
|
+
|
|
|
|
|
|
import android.content.Context;
|
|
|
import android.content.SharedPreferences.Editor;
|
|
|
@@ -65,7 +45,6 @@ import android.content.pm.PackageInfo;
|
|
|
import android.content.pm.PackageManager;
|
|
|
import android.content.pm.PackageManager.NameNotFoundException;
|
|
|
import android.database.Cursor;
|
|
|
-import android.database.SQLException;
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
import android.database.sqlite.SQLiteStatement;
|
|
|
import android.net.ConnectivityManager;
|
|
|
@@ -152,37 +131,7 @@ public class Network
|
|
|
|
|
|
|
|
|
|
|
|
- private static HttpClient getNewHttpClient()
|
|
|
- {
|
|
|
- try
|
|
|
- {
|
|
|
- KeyStore trustStore = KeyStore.getInstance(KeyStore
|
|
|
- .getDefaultType());
|
|
|
- trustStore.load(null, null);
|
|
|
-
|
|
|
- SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
|
|
|
- sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
|
|
|
-
|
|
|
- HttpParams params = new BasicHttpParams();
|
|
|
- HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
|
|
|
- HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
|
|
|
- HttpConnectionParams.setConnectionTimeout(params, REQUEST_TIMEOUT);
|
|
|
- HttpConnectionParams.setSoTimeout(params, SO_TIMEOUT);
|
|
|
- SchemeRegistry registry = new SchemeRegistry();
|
|
|
- registry.register(new Scheme("http", PlainSocketFactory
|
|
|
- .getSocketFactory(), 80));
|
|
|
- registry.register(new Scheme("https", sf, 443));
|
|
|
-
|
|
|
- ClientConnectionManager ccm = new ThreadSafeClientConnManager(
|
|
|
- params, registry);
|
|
|
|
|
|
- return new DefaultHttpClient(ccm, params);
|
|
|
- }
|
|
|
- catch (Exception e)
|
|
|
- {
|
|
|
- return new DefaultHttpClient();
|
|
|
- }
|
|
|
- }
|
|
|
|
|
|
public static String get_marketnews(String id, int limit)
|
|
|
{
|
|
|
@@ -211,11 +160,9 @@ public class Network
|
|
|
|
|
|
public static String get_servicelocation()
|
|
|
{
|
|
|
- String TAG = "net_dbg@get_servicelocation";
|
|
|
|
|
|
if (!Network.NetworkIsAvailable())
|
|
|
{
|
|
|
- Log.d(TAG, "network not available!");
|
|
|
return null; // network not available
|
|
|
}
|
|
|
|
|
|
@@ -227,7 +174,6 @@ public class Network
|
|
|
String jstr = getJson(Network.URL_LOCATIONS, parms);
|
|
|
if (jstr == null || jstr.length() <= 0)
|
|
|
{
|
|
|
- Log.d(TAG, "json is wrong");
|
|
|
return null;
|
|
|
}
|
|
|
return jstr;
|
|
|
@@ -236,11 +182,9 @@ public class Network
|
|
|
|
|
|
public static String get_announcements(String id, int limit)
|
|
|
{
|
|
|
- String TAG = "net_dbg@get_announcements";
|
|
|
|
|
|
if (!Network.NetworkIsAvailable())
|
|
|
{
|
|
|
- Log.d(TAG, "network not available!");
|
|
|
return null; // network not available
|
|
|
}
|
|
|
|
|
|
@@ -252,7 +196,6 @@ public class Network
|
|
|
String jstr = getJson(Network.URL_ANNOUNCEMENTS, parms);
|
|
|
if (jstr == null || jstr.length() <= 0)
|
|
|
{
|
|
|
- Log.d(TAG, "json is wrong");
|
|
|
return null;
|
|
|
}
|
|
|
return jstr;
|
|
|
@@ -401,166 +344,8 @@ public class Network
|
|
|
|
|
|
public static String getJson(String url, Bundle parms)
|
|
|
{
|
|
|
- String TAG = "net_dbg@GetJson";
|
|
|
- Log.d(TAG, "entry");
|
|
|
-
|
|
|
prepare_addtional_params(parms);
|
|
|
-// parms.putString("sessionid", ApexTrackingApplication.get_sessionid());
|
|
|
-
|
|
|
- // if (true)
|
|
|
- // return fakegetJson(url);
|
|
|
- try
|
|
|
- {
|
|
|
- // BasicHttpParams httpParams = new BasicHttpParams();
|
|
|
- // HttpConnectionParams.setConnectionTimeout(httpParams,
|
|
|
- // REQUEST_TIMEOUT);
|
|
|
- // HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
|
|
|
- HttpClient client = getNewHttpClient();// new
|
|
|
- // DefaultHttpClient(httpParams);
|
|
|
- HttpPost post = new HttpPost(url);
|
|
|
- MultipartEntity reqEntity = new MultipartEntity(
|
|
|
- HttpMultipartMode.BROWSER_COMPATIBLE);
|
|
|
-
|
|
|
- Set<String> keys = parms.keySet();
|
|
|
- Log.d(TAG, "================parms============");
|
|
|
- for (String key : keys)
|
|
|
- {
|
|
|
- if (key.contains("_file"))
|
|
|
- {
|
|
|
- ;
|
|
|
- }
|
|
|
- else
|
|
|
- reqEntity.addPart(key, new StringBody(parms.get(key)
|
|
|
- .toString()));
|
|
|
- Log.d(TAG, "key=" + key + " val="
|
|
|
- + parms.get(key).toString());
|
|
|
- }
|
|
|
- Log.d(TAG, "================parms============");
|
|
|
-
|
|
|
- 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();
|
|
|
-
|
|
|
- }
|
|
|
- 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;
|
|
|
- }
|
|
|
-
|
|
|
+ return com.usai.redant.rautils.utils.Network.getJson(url, parms, REQUEST_TIMEOUT);
|
|
|
}
|
|
|
|
|
|
public static int get_recordcount(Bundle parms)
|
|
|
@@ -1377,50 +1162,7 @@ public class Network
|
|
|
|
|
|
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;
|
|
|
+ return com.usai.redant.rautils.utils.Network.isNetworkAvailable(ApexTrackingApplication.get_instance());
|
|
|
}
|
|
|
|
|
|
// public static class SSLSocketFactoryEx extends SSLSocketFactory
|
|
|
@@ -1561,138 +1303,10 @@ public class Network
|
|
|
return sslContext.getSocketFactory().createSocket();
|
|
|
}
|
|
|
}
|
|
|
- private static String createPostParameters(Bundle parms) throws UnsupportedEncodingException {
|
|
|
- StringBuilder result = new StringBuilder();
|
|
|
- boolean first = true;
|
|
|
- for(String key : parms.keySet()){
|
|
|
- if (first)
|
|
|
- first = false;
|
|
|
- else
|
|
|
- result.append("&");
|
|
|
-
|
|
|
- result.append(URLEncoder.encode(key, "UTF-8"));
|
|
|
- result.append("=");
|
|
|
- result.append(URLEncoder.encode(parms.get(key).toString(), "UTF-8"));
|
|
|
- }
|
|
|
-
|
|
|
- return result.toString();
|
|
|
- }
|
|
|
-
|
|
|
- //读取响应头
|
|
|
- public static JSONObject getResponseHeader(HttpURLConnection conn) {
|
|
|
- Map<String, List<String>> responseHeaderMap = conn.getHeaderFields();
|
|
|
- int size = responseHeaderMap.size();
|
|
|
- try {
|
|
|
- JSONObject responseHeader = new JSONObject();
|
|
|
- for(int i = 0; i < size; i++){
|
|
|
- String responseHeaderKey = conn.getHeaderFieldKey(i);
|
|
|
- String responseHeaderValue = conn.getHeaderField(i);
|
|
|
- if (responseHeaderKey != null && !responseHeaderKey.isEmpty() && responseHeaderValue != null) {
|
|
|
- responseHeader.put(responseHeaderKey,responseHeaderValue);
|
|
|
- }
|
|
|
- }
|
|
|
- return responseHeader;
|
|
|
- } catch (JSONException e) {
|
|
|
- e.printStackTrace();
|
|
|
- return null;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- public static String getResponsesuggestedFilename(JSONObject response) {
|
|
|
-
|
|
|
- if (response == null) {
|
|
|
- return null;
|
|
|
- }
|
|
|
-
|
|
|
- try {
|
|
|
- String key = "Content-Disposition";
|
|
|
- String content_disposition = response.getString(key);
|
|
|
- if (content_disposition != null) {
|
|
|
- String[] components = content_disposition.split(";");
|
|
|
- if (components[1] != null) {
|
|
|
- String fileName = components[1].split("=")[1];
|
|
|
- fileName = fileName.replace("\"","");
|
|
|
- return fileName;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- } catch (JSONException e) {
|
|
|
- e.printStackTrace();
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
- return null;
|
|
|
- }
|
|
|
|
|
|
public static File downloadFile(Bundle params, String download_url,String dir) {
|
|
|
-
|
|
|
- if (params == null) {
|
|
|
- params = new Bundle();
|
|
|
- }
|
|
|
-
|
|
|
prepare_addtional_params(params);
|
|
|
- HttpURLConnection connection = null;
|
|
|
-
|
|
|
- File returnFile = null;
|
|
|
- try {
|
|
|
-
|
|
|
- String fileName = UUID.randomUUID().toString();
|
|
|
- if (dir == null) {
|
|
|
- dir = Environment.getExternalStorageDirectory() + File.separator + "download";
|
|
|
- }
|
|
|
-
|
|
|
- if (RAUtil.isFileExist(fileName,dir)) {
|
|
|
- returnFile = new File(dir + File.separator + fileName);
|
|
|
- } else {
|
|
|
- // 创建一个URL对象
|
|
|
- URL url=new URL(download_url);
|
|
|
- // 创建一个HTTP链接
|
|
|
- connection=(HttpURLConnection)url.openConnection();
|
|
|
- connection.setRequestMethod("POST");
|
|
|
-
|
|
|
- // 拼参数
|
|
|
- if (params != null && params.keySet().size() > 0) {
|
|
|
- OutputStream os = connection.getOutputStream();
|
|
|
- BufferedWriter writer = new BufferedWriter(
|
|
|
- new OutputStreamWriter(os, "UTF-8"));
|
|
|
- writer.write(createPostParameters(params));
|
|
|
-
|
|
|
- writer.flush();
|
|
|
- writer.close();
|
|
|
- os.close();
|
|
|
- }
|
|
|
-
|
|
|
- // 获取响应
|
|
|
- JSONObject responseHeader = getResponseHeader(connection);
|
|
|
- String suggestedFileName = getResponsesuggestedFilename(responseHeader);
|
|
|
- Log.d("Download", "download_query: " + suggestedFileName);
|
|
|
- if (suggestedFileName != null && !suggestedFileName.isEmpty()) {
|
|
|
- fileName = suggestedFileName;
|
|
|
- }
|
|
|
-
|
|
|
- // 使用IO流获取数据
|
|
|
- InputStream inputStream=connection.getInputStream();
|
|
|
- // 写文件
|
|
|
- File downloadFile = RAUtil.write2SDFromInput(fileName,dir,inputStream);
|
|
|
-
|
|
|
- if (downloadFile == null) {
|
|
|
- returnFile = null;
|
|
|
- } else {
|
|
|
- returnFile = downloadFile;
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- } catch (IOException e) {
|
|
|
- e.printStackTrace();
|
|
|
- } finally {
|
|
|
- if (connection != null) {
|
|
|
- connection.disconnect();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
- return returnFile;
|
|
|
+ return com.usai.redant.rautils.utils.Network.downloadFile(params, download_url, dir);
|
|
|
}
|
|
|
|
|
|
public static File download_query(String download_url,String path) {
|
|
|
@@ -1929,20 +1543,20 @@ public class Network
|
|
|
|
|
|
params.putString("action","handset_search");
|
|
|
|
|
|
- String url = "http://192.168.1.150:8080/MyWeb/SearchBooking";
|
|
|
+ String url = "http://192.168.1.151:8080/MyWeb/SearchBooking";
|
|
|
|
|
|
String module_name = params.getString("module_name");
|
|
|
if (module_name.equals("Ocean Booking")) {
|
|
|
|
|
|
} else if (module_name.equals("Ocean B/L info.")) {
|
|
|
|
|
|
- url = "http://192.168.1.150:8080/MyWeb/SearchBL";
|
|
|
+ url = "http://192.168.1.151:8080/MyWeb/SearchBL";
|
|
|
} else if (module_name.equals("Container detail")) {
|
|
|
|
|
|
- url = "http://192.168.1.150:8080/MyWeb/SearchContainer";
|
|
|
+ url = "http://192.168.1.151:8080/MyWeb/SearchContainer";
|
|
|
} else if (module_name.equals("Download Document")) {
|
|
|
|
|
|
- url = "http://192.168.1.150:8080/MyWeb/SearchDoc";
|
|
|
+ url = "http://192.168.1.151:8080/MyWeb/SearchDoc";
|
|
|
}
|
|
|
|
|
|
try {
|