導航:首頁 > 操作系統 > androidhttp獲取json

androidhttp獲取json

發布時間:2024-05-23 07:17:44

1. android怎樣進行單元測試

這里我們拿「android的ScrollView滾動布局獲取數據基礎方法」這個示例中利用HttpUtils工具類獲得json數據為例,利用單元測試進行測試。
首先,在AndroidManifest.xml清單文件中使用滑鼠點擊的方法添加android.test.InstrumentationTestRunner(如果使用代碼容易出錯),之後再添加Instrumentation的Name和Target package,全部自動完成這一過程。具體方法如圖。

然後在AndroidManifest中使用Alt+/自動添加單元測試包,單元測試准備工作完成,方法如圖。

在第二步中targetPackage指定的包名com.example.layout,意思是在com.example.layout包下所有包下的類都可以進行單元測試,所以我們在該包下建立一個測試類MyTest,並繼承AndroidTestCase,寫一個testjson()方法,用於測試android語句是否能正確輸出json數據,具體代碼如下。

4
寫好測試代碼之後,進行如圖方式執行測試代碼。如果出現「綠色」,則測試成功,這樣就完成了單元測試,模擬器只需要在旁邊掛著,不用模擬器,而可以進行多個函數的獨立單元測試,不僅節省了時間,還提高了工作效率。

2. android開發用什麼從伺服器獲取數據

在android中有時候我們不需要用到本機的SQLite資料庫提供數據,更多的時候是從網路上獲取數據,那麼Android怎麼從伺服器端獲取數據呢?有很多種,歸納起來有
一:基於Http協議獲取數據方法。二:基於SAOP協議獲取數據方法,三:忘了-------
那麼我們的這篇文章主要是將關於使用Http協議獲取伺服器端數據,這里我們採取的伺服器端技術為java,框架為Struts2,或者可以有Servlet,又或者可直接從JSP頁面中獲取數據。
那麼,接下來我們便開始這一路程:
首先:編寫伺服器端方法,我這里採用的MVC框架是Struts2,目的很單純,就是為了以後做個完整的商業項目,技術配備為:android+SSH。當然,篇幅有限,我這里就直接用Strtus2而已。
伺服器端:新建WebProject ,選擇Java ee 5.0.
為了給項目添加Struts2的支持,我們必須導入Struts2的一些類庫,如下即可(有些jar包是不必的,但是我們後來擴展可能是要使用到的,就先弄進去):
1: xwork-core-2.2.1.1.jar
2: struts2-core-2.2.1.1.jar
3: commons-logging-1.0.4.jar
4: freemarker-2.3.16.jar
5: ognl-3.0.jar
6: javassist-3.7.ga.jar
7:commons-ileupload.jar
8:commons-io.jar
9:json-lib-2.1-jdk15.jar 處理JSON格式數據要使用到

10:struts2-json-plugin-2.2.1.1.jar 基於struts2的json插件
以上的jar包,需要放在WebRoot/WEB-INF/lib目錄下
然後在web.xml文件中敲下:
View Code
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<!-- 定義Struts2的核心控制器:FilterDispatcher -->
<filter>
<!-- 定義核心Filter的名稱 -->
<filter-name>struts2</filter-name>
<!-- 定義Filter的實現類 -->
<filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

</web-app>
然後編寫struts.xml文件,並放在WebRoot/WEB-INF/lib目錄下:如下代碼:
View Code
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>

<!-- setting encoding,DynamicMethod,language
<constant name="struts.custom.i18n.resources" value="messageResource"></constant>
-->
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>

<!-- add package here extends="struts-default"-->
<package name="dongzi" extends="json-default"> <!--需要將struts-default改為json-default-->
<!-- setting action -->
<action name="login" class="com.dongzi.action.loginAction" method="login">
<result type="json"></result> <!--返回值類型設置為json,不設置返回頁面-->
</action>
</package>
</struts>
配置好後,我們再根據<action>標簽內容來編寫action。方法為method對應的login,類名為loginAction,
注意:包繼承為:json-default ,輸出結果類型為json
如下:
View Code
public class loginAction extends ActionSupport implements
ServletRequestAware,ServletResponseAware {
/**
*
*/
private static final long serialVersionUID = 1L;

HttpServletRequest request;
HttpServletResponse response;

public void setServletRequest(HttpServletRequest request) {
this.request=request;
}

public void setServletResponse(HttpServletResponse response) {
this.response=response;
}

public void login(){
try {
//HttpServletRequest request =ServletActionContext.getRequest();
// HttpServletResponse response=ServletActionContext.getResponse();
this.response.setContentType("text/html;charset=utf-8");
this.response.setCharacterEncoding("UTF-8");
if(this.request.getParameter("username").equals("123456")){
this.response.getWriter().write("真的很奇怪,日本人!");
}else if(this.request.getParameter("username").equals("zhd")){
this.response.getWriter().write("沒有錯,我就是東子哥!");
}else{
this.response.getWriter().write("我就是東子哥!");
}

//將要返回的實體對象進行json處理
// JSONObject json=JSONObject.fromObject(this.getUsername());
//輸出格式如:{"id":1, "username":"zhangsan", "pwd":"123"}
// System.out.println(json);

// this.response.getWriter().write(json.toString());
/**
JSONObject json=new JSONObject();
json.put("login", "login");
response.setContentType("text/html;charset=utf-8");
System.out.println(json);
byte[] jsonBytes = json.toString().getBytes("utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
**/
/**
JSONObject json=new JSONObject();
json.put("login", "login");
byte[] jsonBytes = json.toString().getBytes("utf-8");
response.setContentType("text/html;charset=utf-8");
response.setContentLength(jsonBytes.length);
response.getOutputStream().write(jsonBytes);
response.getOutputStream().flush();
response.getOutputStream().close();
**/

} catch (Exception e) {
e.printStackTrace();
}
// return null;
}
}
運行查看下:http://localhost:8080/PDAServer/login.action?username=123456 當然你可以輸入其他參數的URL

3. android 鎴戠敤httpurlconnection get鏂規硶 浠庢湇鍔″櫒鑾峰彇浜嗕竴涓猨son 鍙鏄鎵撳嵃鍑烘潵鏄涓孌典貢鐮 浠g爜濡備笅

1銆佽╂湇鍔″櫒璁劇疆涓涓嬬紪鐮佹牸寮忥紝tomcat鏈変釜璁劇疆緙栫爜鏍煎紡鐨

2銆佹帴鏀跺悗 杞鐮 渚嬪俷ewString(old.getBytes("gbk"),"utf-8");鍏蜂綋鐪嬩綘鎺ユ敹鍒扮殑鏄浠涔堟牸寮

3銆佽劇疆studio鐨勭紪鐮佹牸寮 鏈濂芥墍鏈夌粺涓 u8鏍煎紡

4. android開發中,如何連接伺服器,從伺服器讀取到數據

伺服器端生成JSON:

使用HttpURLConnection連接,通過JSON格式傳遞對象數據

	URLurl=newURL(urlpath);
HttpURLConnectionconn=(HttpURLConnection)url.openConnection();
InputStreaminStream=conn.getInputStream();
=newByteArrayOutputStream();
byte[]data=newbyte[1024];
intlen=0;
while((len=inStream.read(data))!=-1){
outStream.write(data,0,len);
System.out.println(len);
}
inStream.close();
byte[]rlt=outStream.toByteArray();
returnnewString(rlt);
閱讀全文

與androidhttp獲取json相關的資料

熱點內容
教育局發布的命令能撤銷嗎 瀏覽:908
php獲取http請求 瀏覽:398
上海公寓app哪個好 瀏覽:677
電子軟體的演算法是什麼 瀏覽:654
汽車解壓過幾天能過戶啊 瀏覽:548
淘寶助力清除源碼 瀏覽:35
軟體源碼解析服務 瀏覽:827
命令助手教程 瀏覽:959
x站秘趣導航源碼 瀏覽:751
什麼是好的程序員 瀏覽:59
程序員找老婆的四階段 瀏覽:12
編程趣談入門 瀏覽:351
暗格里的秘密是哪個app看的 瀏覽:276
phpjsondecode失敗 瀏覽:836
php定時刷新頁面 瀏覽:511
二層交換機怎麼連接伺服器 瀏覽:420
生命最後的八分鍾源碼 瀏覽:715
圍棋需要什麼app 瀏覽:899
10款本田crv壓縮機不工作風扇會轉 瀏覽:76
演算法導論的數學基礎 瀏覽:128