banxitan

          統(tǒng)計(jì)

          留言簿(2)

          閱讀排行榜

          評(píng)論排行榜

          2013年3月13日 #

          Android Activity MainThread 中不能執(zhí)行http通信解決辦法

          今天在做離線(xiàn)文件傳輸時(shí)。用HTTP上傳文件。程序一運(yùn)行 報(bào)如下的錯(cuò)誤
          android.os.NetworkOnMainThreadException

          原因是Google從3開(kāi)始,強(qiáng)制默認(rèn)禁止UI主線(xiàn)程發(fā)起通信請(qǐng)求 

          解決辦法: 

          把通信的代碼轉(zhuǎn)移到子線(xiàn)程里去做, 比較靠譜的是new一個(gè)AyncTask,在里面做通信

                                    有幾個(gè)細(xì)節(jié)性的問(wèn)題是,

                                          第一, 一般原有通信的代碼都是共通的API, 被許多個(gè)Activity調(diào)用, 一個(gè)良好的修改方法是在共通API里面new AyncTask

                                         第二, 通信的API一般來(lái)說(shuō)都是同步的, 你通信,然后畫(huà)面主線(xiàn)程需要堵塞住,等待API的通信結(jié)果,再?zèng)Q定下面的業(yè)務(wù)邏輯的走向。

          所以,這塊可以采取 AyncTask.get(), 讓主線(xiàn)程堵塞,直到通信結(jié)束。 當(dāng)然,如果你需要將通信異步的話(huà), 可以用Handler機(jī)制來(lái)解決


          具體調(diào)用代碼如下:HTTP異

          步請(qǐng)求

          package com.qqtech.ucstar.utils;

          import java.io.File;
          import java.io.IOException;
          import java.nio.charset.Charset;

          import org.apache.http.HttpResponse;
          import org.apache.http.client.HttpClient;
          import org.apache.http.client.methods.HttpPost;
          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.impl.client.DefaultHttpClient;

          import android.os.AsyncTask;

          public class HttpReqTask extends AsyncTask<Object, Object, HttpResponse> {

          @Override
          protected HttpResponse doInBackground(Object arg0) {
          String fileUploadUrl
          = (String) arg0[0];
          String streamid
          = (String) arg0[1];
          File file
          = (File) arg0[2];
          //boolean paramBoolean = Boolean.parseBoolean((String) arg0[3]);
          //String paramString3 = (String) arg0[4];
          HttpClient localHttpClient = new DefaultHttpClient();
          //String str1 = "fileName";
          File localFile = file;
          try {
          if ((localFile == null) || (!localFile.exists()))
          throw new IOException("文件不存在:" + localFile);
          }
          catch (IOException e1) {
          e1.printStackTrace();
          }

          HttpPost localPostMethod
          = new HttpPost(fileUploadUrl+"?streamid="+streamid+"&fileencode=UTF-8");
          //MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
          MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE,null, Charset.forName("UTF-8"));
          try {
          FileBody bin
          = new FileBody(file);
          reqEntity.addPart(
          "file", bin);
          localPostMethod.setEntity(reqEntity);
          HttpResponse response
          = localHttpClient.execute(localPostMethod);
          return response;
          }
          catch (Exception e) {
          e.printStackTrace();
          }

          return null;
          }


          }



          調(diào)用代碼如下:


          Object[] param = new Object[5];
               param[0] = answer.getUploadURL();
               param[1] = answer.getStreamid();
               param[2] = new File(answer.getFileURL());
               param[3] = "false";
               param[4] = "";
               AsyncTask res = new HttpReqTask().execute(param);
               HttpResponse rep = null;
               rep = (HttpResponse) res.get();
               if (rep.getStatusLine().getStatusCode() == 200) {
                System.out.println("文件上傳成功");
               }else{
                System.out.println("文件上傳失敗");
               }
              }



          posted @ 2013-07-11 10:51 MikyTan 閱讀(1087) | 評(píng)論 (0)編輯 收藏

          修改模擬器DNS方法

          今天在做手機(jī)終端開(kāi)發(fā)時(shí),發(fā)現(xiàn)連上域名服務(wù)器老是連不上,而直接用IP連接是OK的,初步懷凝是DNS問(wèn)題引起來(lái),經(jīng)查相關(guān)的資料,現(xiàn)記錄如下:

          Android模擬器默認(rèn)的地址是10.0.2.3,默認(rèn)的DNS也是10.0.2.3,而一般電腦的IP都是192.168.1.100之類(lèi)的,不在同一個(gè)網(wǎng)段。所以就會(huì)出現(xiàn)電腦可以上網(wǎng)但是模擬器不能上網(wǎng)的情況。其實(shí)設(shè)置方法很簡(jiǎn)單,只要把模擬器的默認(rèn)DNS設(shè)置成電腦的DNS地址即可。
          第一步:用系統(tǒng)的命令進(jìn)入Android開(kāi)發(fā)包的tools目錄
           cd X:\...\android-sdk-windows\tool
          第二布:使用adb的shell,確認(rèn)系統(tǒng)的各項(xiàng)屬性
          adb shell
          getprop 
          getprop會(huì)列出系統(tǒng)當(dāng)前的各項(xiàng)屬性
          第三步:得到模擬器的DNS地址
          在結(jié)果里可以看到:
          [net.dns1]: [10.0.2.3]
          [net.dns2]: [10.0.2.4]
          [net.dns3]: [10.0.2.5]
          [net.dns4]: [10.0.2.6]
          第四步:把dns改成我們自己的DNS
          setprop net.dns1 192.168.1.1

          posted @ 2013-06-04 20:24 MikyTan 閱讀(1274) | 評(píng)論 (0)編輯 收藏

          JAVA 連接 ORCLE RAC連接URL串

          連接的URL串

          jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=vip1)(PORT

          =1521))(ADDRESS=(PROTOCOL=TCP)(HOST=vip2)(PORT=1521))(LOAD_BALANCE=yes)(FAILOVER=

          on))(CONNECT_DATA=(SERVICE_NAME=gs)))


          posted @ 2013-05-21 16:48 MikyTan 閱讀(257) | 評(píng)論 (0)編輯 收藏

          Oralce 監(jiān)聽(tīng)器啟動(dòng)


          啟動(dòng)      lsnrctl start
          狀態(tài)      lsnrctl status
          關(guān)閉      lsnrctl  stop

          posted @ 2013-05-11 14:47 MikyTan 閱讀(183) | 評(píng)論 (0)編輯 收藏

          Oracle 導(dǎo)出、導(dǎo)入某用戶(hù)所有數(shù)據(jù)(包括表、視圖、存儲(chǔ)過(guò)程...)


          導(dǎo)出命令:exp 用戶(hù)名/密碼@數(shù)據(jù)庫(kù) owner=用戶(hù)名 file=文件存儲(chǔ)路徑(如:F:\abcd.dmp)

          測(cè)試截圖:exp ZM/sql123@ORCL owner=ZM file=F\abcd.dmp



          導(dǎo)入命令:imp 用戶(hù)名/密碼@數(shù)據(jù)庫(kù) fromuser=用戶(hù)名 touser=用戶(hù)名 file=d:\cu.dmp ignore=y

           imp:命令類(lèi)型  

          cu/mycu@db:導(dǎo)入的數(shù)據(jù)庫(kù)登陸(用戶(hù)名/密碼@數(shù)據(jù)庫(kù))  

          fromuser:文件的指定用戶(hù)

           touser:指定導(dǎo)入到當(dāng)前登錄的數(shù)據(jù)庫(kù)某個(gè)用戶(hù)  

          file:需要導(dǎo)入的數(shù)據(jù)文件  

          ignore:是否忽略創(chuàng)建錯(cuò)誤

           

          測(cè)試截圖:

          imp ZM/sql123@ORCL fromuser=ZM touser=SZZM file=F:\test.dmp ignore=y





          posted @ 2013-05-11 09:59 MikyTan 閱讀(393) | 評(píng)論 (0)編輯 收藏

          linux環(huán)境下的rmi常見(jiàn)問(wèn)題

          linux環(huán)境下的rmi常見(jiàn)問(wèn)題

          問(wèn)題一:RMI服務(wù)提供程序運(yùn)行在Windows操作系統(tǒng)下,RMI服務(wù)可以正常訪(fǎng)問(wèn),但將RMI服務(wù)提供程序部署到Linux操作系統(tǒng)下后,RMI服務(wù)無(wú)法訪(fǎng)問(wèn),提示

          org.springframework.remoting.RemoteConnectFailureException:

          Cannot connect to remote service [rmi://192.168.0.106:1199/ItemRetag]; nested exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1; ……   
           解決辦法:在加載RMI服務(wù)之前將當(dāng)前服務(wù)器的IP指定給hostName,如 System.setProperty("java.rmi.server.hostname", "192.168.100.7");或者修改/etc/hosts文件,在這個(gè)文件中加 192.168.100.54  testlinux1 localhost.localdomain localhost 就行,或者將/etc/hosts文件中默認(rèn)的127.0.0.1改成當(dāng)前機(jī)器的IP即可!

          問(wèn)題二:java.rmi.server.ExportException: internal error: ObjID already in use Caused by: java.rmi.server.ExportException: internal error: ObjID already in use……

          出現(xiàn)這種問(wèn)題及有可能是/etc/hosts文件中指定的IP并不是當(dāng)前服務(wù)器的真實(shí)IP,RMI在初始化時(shí)注冊(cè)服務(wù)失敗。

          通過(guò)System.out.println(InetAddress.getLocalHost().toString());查看當(dāng)前主機(jī)的IP是否為真實(shí)IP,如顯示為SIMBANK/220.250.64.24,而真實(shí)IP為192.168.1.2         

          解決辦法:修改/etc/hosts文件中錯(cuò)誤的IP即可,將:

          220.250.64.24          SIMBANK
          修改為

          192.168.1.2          SIMBANK

          posted @ 2013-05-08 20:26 MikyTan 閱讀(3785) | 評(píng)論 (0)編輯 收藏

          IBM JDK 、SUN JDK、HP JDK如何產(chǎn)生Heapdump文件

          JAVA中,通過(guò)分析Heapdump文件可以檢查程序是否存在內(nèi)存泄露,但是這個(gè)文件一般是在程序遇到致命問(wèn)題時(shí)才會(huì)產(chǎn)生,而如何事前生成這個(gè)文件,從而在程序尚末崩潰前找出問(wèn)題的所在。

          以下記述了各個(gè)版本的JDK產(chǎn)生DUMP文件的方法:

          UN JDK生成Heapdump文件只需要在tomcat啟動(dòng)腳本中增加 HeapDumpOnOutOfMemoryError 參數(shù)
          此參數(shù)需要Java SE release 5.0 update 14 或以上支持

          設(shè)置示例:
          set JAVA_OPTS=%JAVA_OPTS% -server -Xms512m -Xmx800m -XX:PermSize=64M -XX:MaxPermSize=128m -Djava.awt.headless=true -XX:+HeapDumpOnOutOfMemoryError -XX:+HeapDumpOnCtrlBreak

          IBM JDK生成Heapdump文件的開(kāi)關(guān):
          — export IBM_HEAPDUMP=true
          — export IBM_HEAP_DUMP=true
          — export IBM_HEAPDUMP_OUTOFMEMORY=true
          — export IBM_JAVADUMP_OUTOFMEMORY=true
          — export IBM_JAVACORE_OUTOFMEMORY=true
          — export IBM_HEAPDUMPDIR=<directory_path>

          HP JDK生成Heapdump文件需要在在環(huán)境變量上,加上export _JAVA_HEAPDUMP=1

          posted @ 2013-03-26 16:03 MikyTan 閱讀(1106) | 評(píng)論 (0)編輯 收藏

          MySql 的批量操作,要加rewriteBatchedStatements參數(shù)

          今天在做某項(xiàng)目的POC測(cè)試,甲方提供了一個(gè)三十萬(wàn)記錄的TXT數(shù)據(jù)文件,需要把該文件的記錄插入到數(shù)據(jù)庫(kù)中,由于項(xiàng)目部的同事在搭建測(cè)試環(huán)境中用的是Mysql數(shù)據(jù)庫(kù),在把數(shù)據(jù)導(dǎo)入到數(shù)據(jù)庫(kù)中用的是JDBC的批處理。代碼如下
          private void batchParseGroup(){
                  Connection con
          = null;
                  PreparedStatement ps 
          =null;
                  
          try {
                      con
          = DbConnectionManager.getConnection();
                      con.setAutoCommit(
          false);
                      String sql 
          = " insert into jivegroup(uri,groupname,pgroupid,description,creationdate,modificationdate,priority,selfpriority) values(?,?,?,?,?,?,?,?)";
                      ps
          = con.prepareStatement(sql);
                      
          for(int i=0;i<groupList.size();i++){
                          Group group 
          = groupList.get(i);
                          ps.setString(
          1, group.getUri());
                          ps.setString(
          2, group.getName());
                          ps.setString(
          3, group.getPgroupId());
                          ps.setString(
          4, group.getName());
                          ps.setString(
          5""+System.currentTimeMillis());
                          ps.setString(
          6""+System.currentTimeMillis());
                          ps.setInt(
          7, group.getPriority());
                          ps.setInt(
          8, group.getPriority());
                          ps.addBatch();
                          
          if(i%100==0){
                              ps.executeBatch();
                          }

                      }

                      con.commit();
                      ps.executeBatch();
                  }
           catch (SQLException e) {
                      e.printStackTrace();
                  }
          finally{
                      DbConnectionManager.closeConnection(ps, con);
                  }

              }

          在測(cè)試時(shí),發(fā)現(xiàn)三十萬(wàn)的數(shù)據(jù)居然需要十分鐘左右的時(shí)間。首先想到的就是Mysql的相關(guān)配置是不是有問(wèn)題,反復(fù)修改了Mysql的相應(yīng)配置參數(shù),收效甚微。

          在Mysql的官網(wǎng)上查到如下:
           http://dev.mysql.com/doc/refman/5.1/en/connector-j-reference-configuration-properties.html

          關(guān)于rewriteBatchedStatements參數(shù),Mysql官方的說(shuō)明:

          Should the driver use multiqueries (irregardless of the setting of "allowMultiQueries") as well as rewriting of prepared statements for INSERT into multi-value inserts when executeBatch() is called? Notice that this has the potential for SQL injection if using plain java.sql.Statements and your code doesn't sanitize input correctly. Notice that for prepared statements, server-side prepared statements can not currently take advantage of this rewrite option, and that if you don't specify stream lengths when using PreparedStatement.set*Stream(), the driver won't be able to determine the optimum number of parameters per batch and you might receive an error from the driver that the resultant packet is too large. Statement.getGeneratedKeys() for these rewritten statements only works when the entire batch includes INSERT statements.



          解決辦法:
                下載最新的JDBC的驅(qū)動(dòng)程序。
                MYSQL URL的配置參數(shù)如下:
                jdbc:mysql://54.200.190.80:3306/ccb_ucstar?rewriteBatchedStatements=true

          經(jīng)過(guò)測(cè)試。三十多萬(wàn)的數(shù)據(jù)。70秒內(nèi)搞定!

          posted @ 2013-03-13 20:38 MikyTan 閱讀(2797) | 評(píng)論 (0)編輯 收藏

          主站蜘蛛池模板: 冀州市| 介休市| 兴海县| 韶关市| 揭阳市| 龙江县| 商城县| 乌兰察布市| 广汉市| 西宁市| 泽库县| 苏尼特左旗| 工布江达县| 谢通门县| 申扎县| 抚州市| 苏尼特右旗| 从江县| 雷山县| 廉江市| 交城县| 柏乡县| 稷山县| 察隅县| 余干县| 延庆县| 涟源市| 保定市| 青州市| 白朗县| 休宁县| 康定县| 隆尧县| 唐河县| 个旧市| 南宫市| 启东市| 芜湖市| 封丘县| 英山县| 开远市|