朱杰兵blog

          jonhney'blog
          posts - 140, comments - 1, trackbacks - 0, articles - 0

          通過HttpClient請求webService 

          由于服務端是用webService開發的,android要調用webService服務獲取數據,這里采用的是通過HttpClient發送post請求,獲取webService數據。
           
          服務端使用的webService框架是axis2,請求數據之前,要封裝一個xml格式,再通過post請求,獲取服務端數據。
          請求的xml格式如下所示: 
          1 <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"xmlns:sam="http://user.service.xxx.com">
          2    <soap:Header/>
          3    <soap:Body>
          4       <sam:getUserInfo>
          5      <sam:userName>sunlightcs</sam:userName>
          6       </sam:getUserInfo>
          7    </soap:Body>
          8 </soap:Envelope>
          其中:getUserInfo是方法名,userName是參數名,當然,還可以加多個參數。
           
           
          下面的代碼是向webService發送請求,獲取數據,返回的數據是xml形式的,android只要解析xml數據,就可以獲得想要的數據了。 

          01 import java.io.IOException;
          02 import java.io.OutputStream;
          03 import java.io.OutputStreamWriter;
          04 import java.io.Writer;
          05  
          06 import org.apache.http.HttpResponse;
          07 import org.apache.http.client.HttpClient;
          08 import org.apache.http.client.methods.HttpPost;
          09 import org.apache.http.entity.ContentProducer;
          10 import org.apache.http.entity.EntityTemplate;
          11 import org.apache.http.impl.client.DefaultHttpClient;
          12 import org.apache.http.util.EntityUtils;
          13  
          14  
          15 public class ClientTest {
          16  
          17     public static void main(String[] args) {
          18         ClientTest.httpClientPost();
          19     }
          20      
          21     private static void httpClientPost() {
          22         HttpClient client = new DefaultHttpClient();
          23         HttpPost post = newHttpPost("http://localhost:8080/xxx/services/userService");
          24          
          25         try {
          26             ContentProducer cp = new ContentProducer() {
          27                 public void writeTo(OutputStream outstream) throwsIOException {
          28                     Writer writer = new OutputStreamWriter(outstream,"UTF-8");
          29                      
          30                     /**
          31                      * 獲取請求的xml格式數據
          32                      */
          33                     String requestXml = getRequestXml();
          34                     writer.write(requestXml);
          35                     writer.flush();
          36                 }
          37             };
          38  
          39             post.setEntity(new EntityTemplate(cp));
          40             HttpResponse response = client.execute(post);
          41              
          42         //打印返回的xml數據
          43             System.out.println(EntityUtils.toString(response.getEntity()));
          44         catch (IOException e) {
          45             e.printStackTrace();
          46         }
          47     }
          48      
          49      
          50     private static String getRequestXml(){
          51         StringBuilder sb = new StringBuilder("<soap:Envelope xmlns:soap=\"http://www.w3.org/2003/05/soap-envelope\" xmlns:sam=\"http://user.service.xxx.com\">");
          52         sb.append("<soap:Header/>");
          53         sb.append("<soap:Body>");
          54         sb.append("<sam:getUserInfo>");
          55         sb.append("<sam:userName>sunlightcs</sam:userName>");
          56         sb.append("</sam:getUserInfo>");
          57         sb.append("</soap:Body>");
          58         sb.append("</soap:Envelope>");
          59          
          60         return sb.toString();
          61     }
          62  
          63 }

          返回的數據格式如下: 
          1 <?xml version='1.0' encoding='UTF-8'?>
          2 <soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
          3     <soapenv:Body>
          4         <ns:getUserInfoResponse xmlns:ns="http://user.service.xxx.com">
          5             <ns:return>xxx</ns:return>
          6         </ns:getUserInfoResponse>
          7     </soapenv:Body>
          8 </soapenv:Envelope>
          其中,<ns:return>內的"xxx"可以是json數據,android只需解析標簽<ns:return>里的json數據即可。 
          轉載 http://www.juziku.com/wiki/3919.htm

          posted @ 2017-05-24 16:10 朱杰兵 閱讀(1219) | 評論 (0)編輯 收藏

          netstat -pan|grep 7001
          找到進程號,kill -9殺死
          打開啟動路徑
          nohup ./startWeblogic.sh &
          tail -f nohup.out看啟動日志
          ---------------------------
          csh   服務端運行
          ps -ef | grep weblogic
          kiss -9 sid(左邊id)
          (
          查看后臺web進程 
          #ps -ef|grep java 如: 
               root 123456 2346546 
              root 1346464    64646464 
          殺后臺進程 :#kill -9 1346464 
          )
          cd 到。。。bin/startWebLogic.sh &    注意大小寫

          posted @ 2017-04-26 19:53 朱杰兵 閱讀(95) | 評論 (0)編輯 收藏

          java.lang.ClassCastException: weblogic.xml.jaxp.RegistryDocumentBuilderFactory

          解決辦法

          刪掉war包中的xml-apis.jar就可以了

          posted @ 2017-04-14 16:06 朱杰兵 閱讀(104) | 評論 (0)編輯 收藏

          右鍵pom文件 run as --> maven build,  goals填入相應命令,點run

          打包
          goals 輸入 clean package

          安裝jar到倉庫
          goals 輸入 clean install

          測試
          goals 輸入 clean test

          編譯
          goals 輸入 compile

          清除
          goals輸入 clean

           clean  清除編譯,compile  編譯,test  編譯并測試,install 打包并發送到本地倉庫,package 只是打成jar包,并不會發送到本地倉庫

          posted @ 2017-04-13 14:16 朱杰兵 閱讀(180) | 評論 (0)編輯 收藏

          posted @ 2017-04-12 17:50 朱杰兵 閱讀(2477) | 評論 (0)編輯 收藏

          public enum MessageLevel {
              LOW {
                  @Override
                  public String getDesc() {
                      return "低";
                              
                  }

                  @Override
                  public String getCode() {
                      return "L";
                  }

                  @Override
                  public String getIcon() {
                      return "medal_bronze_1.png";
                  }

              },
              HEIGH {

                  @Override
                  public String getDesc() {
                      return "高";
                  }

                  @Override
                  public String getCode() {
                      return "H";
                  }

                  @Override
                  public String getIcon() {
                      return "medal_gold_1.png";
                  }

              },
              NORMAL {

                  @Override
                  public String getDesc() {
                      return "中";
                  }

                  @Override
                  public String getCode() {
                      return "N";
                  }

                  @Override
                  public String getIcon() {
                      return "medal_silver_1.png";
                  }

              };
              
              public abstract String getDesc();

              public abstract String getCode();

              public abstract String getIcon();
          }

          1. public static void main(String[] args)  
          2.     {  
          3.         System.out.println(MessageLevel.LOW.getDesc());  
          4.         System.out.println(MessageLevel.LOW.getCode());
          5.         System.out.println(MessageLevel.LOW.getIcon());
          6.     } 
          -----------------------------------------------------------------------------------------------
          1. public enum Operation   
          2. {  
          3.     PLUS  
          4.     {  
          5.         public double eval(double x,double y)  
          6.         {  
          7.             return x+y;  
          8.         }  
          9.     },  
          10.     MINUS  
          11.     {  
          12.         public double eval(double x,double y)  
          13.         {  
          14.             return x-y;  
          15.         }  
          16.     },  
          17.     TIMES  
          18.     {  
          19.         public double eval(double x,double y)  
          20.         {  
          21.             return x*y;  
          22.         }  
          23.     },  
          24.     DIVIDE  
          25.     {  
          26.         public double eval(double x,double y)  
          27.         {  
          28.             return x/y;  
          29.         }  
          30.     };  
          31.     //為枚舉類定義一個抽象方法,這個抽象方法由不同的枚舉值提供不同的實現。  
          32.     public abstract double eval(double x,double y);  
          33.     public static void main(String[] args)  
          34.     {  
          35.         System.out.println(Operation.PLUS.eval(3,4));  
          36.         System.out.println(Operation.MINUS.eval(5,4));  
          37.         System.out.println(Operation.TIMES.eval(5,4));  
          38.         System.out.println(Operation.DIVIDE.eval(5,4));  
          39.     }  

          posted @ 2017-04-07 11:19 朱杰兵 閱讀(92) | 評論 (0)編輯 收藏

          classpath

          首先 classpath是指編譯過后的WEB-INF文件夾下的的classes目錄

          • 對于maven的所有項目, 配置文件一般放在resources目錄下, 當編譯之后會自動復制到classes目錄下
          • 非maven的所有項目, 一般放在src目錄下, 編譯之后也會自動復制到classes目錄下面.
          • 所有的web-app項目, 例如web.xml, spring的配置文件等等,是放在webapp/WEB-INF下面的,
            如果想要引用resources或者src目錄下的配置文件, 就在在配置文件的路徑前加上classpath:, 例如MyBatis配置文件的引用.
            <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  <property name="dataSource" ref="dataSource"/>  <property name="configLocation" value="classpath:MybatisConfiguration.xml"/>  <property name="mapperLocations" value="classpath*:com/tenlee/mapper/UserMapper.xml"/> </bean>
          • 如果不加的的話,那么都要把配置文件放在WEB-INF/目錄下面, 但這樣不能單獨運行java類進行調試了,必須要啟動整個webapp, 比如這樣
            <init-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/configs/mvc-dispatcher.xml</param-value> </init-param>

          classpath classpath* 區別:

          • classpath:只會到你的classes路徑中查找找文件
          • classpath* :不僅包含classes路徑,還包括jar文件classes路徑進行查找
          • classpath:classpath*:的區別在于,前者只會從第一個classpath中加載,而后者會從所有的classpath中加載 如果要加載的資源,不在當前ClassLoader的路徑里,那么用classpath:前綴是找不到的,這種情況下就需要使用classpath*:前綴.
          • 另一種情況下,在多個classpath中存在同名資源,都需要加載,那么用classpath:只會加載第一個,這種情況下也需要用classpath*:前綴.
          • 可想而知,用classpath*:需要遍歷所有的classpath,所以加載速度是很慢的,因此,在規劃的時候,應該盡可能規劃好資源文件所在的路徑,盡量避免使用classpath*

          posted @ 2017-04-06 14:32 朱杰兵 閱讀(185) | 評論 (0)編輯 收藏

               摘要: 如果你經常需要在Eclipse里打開相關資源文件所在的文件夾,比較麻煩,要右鍵,屬性,在Location一欄中把所在的文件夾拷貝一下,然后再去資源管理器里輸入這個路徑,回車,然后打開它,比較麻煩。下載地址:http://download.csdn.net/download/lang791534167/8585091eclipse3.6以下的版本將下載的jar包復制到plugins目錄下3.6以上包...  閱讀全文

          posted @ 2017-04-05 09:32 朱杰兵 閱讀(113) | 評論 (0)編輯 收藏

               摘要: 解決方法: 步驟一: 從http://maven.oschina.net/content/groups/public/org/apache/maven/archetypes/maven-archetype-quickstart/ 下載最新版maven-archetype-quickstart-1.1.jar 步驟二: 命令行到下載目錄下執行 mvn install...  閱讀全文

          posted @ 2017-03-31 11:13 朱杰兵 閱讀(2219) | 評論 (0)編輯 收藏

          properties文件默認應該顯示為unicode編碼,如果安裝propertiesEditor插件后可顯示為中文

          如果沒有安裝插件,但顯示中文,則程序調用屬性文件會出現亂碼問題,這樣就需要手動來將中文轉為unicode

          直接使用JDK自帶的工具:native2ascii.exe

          運行cmd,直接用命令的形式,找到文件對應的目錄,輸入以下命令,

          命令格式:native2ascii -encoding UTF-8 源文件名.properties 目標文件名.properties

          就可以將含有中文的源文件轉為unicode編碼的目標文件

          posted @ 2017-03-29 14:11 朱杰兵 閱讀(125) | 評論 (0)編輯 收藏

          僅列出標題
          共14頁: 上一頁 1 2 3 4 5 6 7 8 9 下一頁 Last 
          主站蜘蛛池模板: 崇左市| 长子县| 宜阳县| 绥芬河市| 日土县| 五指山市| 高清| 义乌市| 石台县| 日照市| 即墨市| 金堂县| 全南县| 长岛县| 莱芜市| 瑞昌市| 汝城县| 小金县| 都匀市| 盐边县| 济宁市| 肥东县| 嵊州市| 靖宇县| 文成县| 新巴尔虎右旗| 昔阳县| 五常市| 永宁县| 康马县| 青州市| 微博| 宜阳县| 合水县| 南雄市| 饶阳县| 西平县| 静乐县| 平陆县| 龙泉市| 绍兴县|