Android發(fā)送xml數(shù)據(jù)給服務器的方法
更新時間:2015年09月21日 12:17:35 作者:Ruthless
這篇文章主要介紹了Android發(fā)送xml數(shù)據(jù)給服務器的方法,以實例形式較為詳細的分析了Android發(fā)送XML數(shù)據(jù)及接收XML數(shù)據(jù)的相關技巧,具有一定參考借鑒價值,需要的朋友可以參考下
本文實例講述了Android發(fā)送xml數(shù)據(jù)給服務器的方法。分享給大家供大家參考。具體如下:
一、發(fā)送xml數(shù)據(jù):
public static void main(String[] args) throws Exception {
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><videos><video><title>中國</title></video></videos>";
String path = http://localhost:8083/videoweb/video/manage.do?method=getXML ;
byte[] entity = xml.getBytes("UTF-8");
HttpURLConnection conn = (HttpURLConnection) new URL(path).openConnection();
conn.setConnectTimeout(5000);
conn.setRequestMethod("POST");
conn.setDoOutput(true);
//指定發(fā)送的內(nèi)容類型為xml
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
conn.setRequestProperty("Content-Length", String.valueOf(entity.length));
OutputStream outStream = conn.getOutputStream();
outStream.write(entity);
if(conn.getResponseCode() == 200){
System.out.println("發(fā)送成功");
}else{
System.out.println("發(fā)送失敗");
}
}
二、接受xml數(shù)據(jù):
public ActionForward getXML(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
InputStream inStream = request.getInputStream();
byte[] data = StreamTool.read(inStream);
String xml = new String(data, "UTF-8");
System.out.println(xml);
return mapping.findForward("result");
}
希望本文所述對大家的Android程序設計有所幫助。
相關文章
Android無限循環(huán)RecyclerView的完美實現(xiàn)方案
這篇文章主要介紹了Android無限循環(huán)RecyclerView的完美實現(xiàn)方案,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-06-06
詳解Android應用開發(fā)中Scroller類的屏幕滑動功能運用
這篇文章主要介紹了詳解Android應用開發(fā)中Scroller類的屏幕滑動功能運用,文中包括各種觸摸滑屏手勢相關方法的示例,需要的朋友可以參考下2016-02-02
Android ListView自定義Adapter實現(xiàn)仿QQ界面
這篇文章主要為大家詳細介紹了ListView自定義Adapter實現(xiàn)仿QQ界面,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-10-10
Android自定義ViewGroup打造各種風格的SlidingMenu
這篇文章主要介紹了Android自定義ViewGroup打造各種風格的SlidingMenu的相關資料,需要的朋友可以參考下2016-02-02
Android編程實現(xiàn)通訊錄中聯(lián)系人的讀取,查詢,添加功能示例
這篇文章主要介紹了Android編程實現(xiàn)通訊錄中聯(lián)系人的讀取,查詢,添加功能,涉及Android權限控制及通訊錄相關操作技巧,需要的朋友可以參考下2017-07-07

