




下載本文檔
版權說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權,請進行舉報或認領
文檔簡介
【移動應用開發(fā)技術】Android中怎么利用GET方法實現(xiàn)網(wǎng)絡傳值
本篇文章給大家分享的是有關Android中怎么利用GET方法實現(xiàn)網(wǎng)絡傳值,在下覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著在下一起來看看吧。WEB應用在這里,我只建立一個簡單的Servlet,用來接收安卓端發(fā)來的信息。package
deu.hpu.servlet;
import
java.io.IOException;
import
java.io.PrintWriter;
import
javax.servlet.ServletException;
import
javax.servlet.http.HttpServlet;
import
javax.servlet.http.HttpServletRequest;
import
javax.servlet.http.HttpServletResponse;
public
class
ManagerServlet
extends
HttpServlet
{
public
void
doGet(HttpServletRequest
request,
HttpServletResponse
response)
throws
ServletException,
IOException
{
String
title=request.getParameter("title");
title=new
String(title.getBytes("ISO8859-1"),"UTF-8");
String
timelength=request.getParameter("timelength");
timelength=new
String(timelength.getBytes("ISO8859-1"),"UTF-8");
System.out.println("視頻名稱"+title);
System.out.println("時長"+timelength);
}
public
void
doPost(HttpServletRequest
request,
HttpServletResponse
response)
throws
ServletException,
IOException
{
doGet(request,response);
}
}
安卓客戶端在這里,我要建立一個輸入框界面,讓用戶吧數(shù)據(jù)輸入進去,然后我再將數(shù)據(jù)通過get方式提交。
XML界面(兩個輸入框,一個按鈕):<LinearLayout
xmlns:android="/apk/res/android"
xmlns:tools="/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context="com.example.newsmanager.MainActivity"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/title"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/title"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/timelength"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:id="@+id/timelength"/>"
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:onClick="save"
android:text="@string/button"
/>
</LinearLayout>之后我要在Activity里將界面的編輯框里面的值傳到WEB端
主Activity(這里的線程問題在前面講過):package
com.example.newsmanager;
import
com.example.service.NewsService;
import
android.app.Activity;
import
android.os.Bundle;
import
android.view.View;
import
android.widget.EditText;
import
android.widget.Toast;
public
class
MainActivity
extends
Activity
{
private
EditText
titletext;
private
EditText
lengthtext;
@Override
protected
void
onCreate(Bundle
savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
titletext=(EditText)
findViewById(R.id.title);
lengthtext=(EditText)
findViewById(R.id.timelength);
}
boolean
flag;
public
void
save(View
view)
throws
Exception{
//開啟線程
new
Thread(new
Runnable()
{
String
title=titletext.getText().toString();
String
length=lengthtext.getText().toString();
@Override
public
void
run()
{
boolean
result;
try
{
result
=
NewsService.save(title,length);
if(result){
//返回主線程顯示
runOnUiThread(new
Runnable()
{
@Override
public
void
run()
{
Toast.makeText(getApplicationContext(),
R.string.success,
1).show();
}
});
}else{
runOnUiThread(new
Runnable()
{
@Override
public
void
run()
{
Toast.makeText(getApplicationContext(),
R.string.error,
1).show();
}
});
}
}
catch
(Exception
e)
{
//
TODO
Auto-generated
catch
block
e.printStackTrace();
}
}
}).start();
}
}上面代碼中的NewsService類以及save方法(這個類是用來處理信息,然后以get方式傳往WEB端)。這里我要說一句,我們采用的GET方法,是將需要傳遞給WEB端的數(shù)據(jù)放在URL路徑,然后WEB端進行解析得到的,所以我們要在方法中將URL路徑給拼湊完成然后傳給WEB端(里面的IP是我tomcat服務器本機的ip)。package
com.example.service;
import
.HttpURLConnection;
import
.URL;
import
.URLEncoder;
import
java.util.HashMap;
import
java.util.Map;
public
class
NewsService
{
/*
*
保存數(shù)據(jù)
*
title
標題
*
length
時長
*
*/
public
static
boolean
save(String
title,
String
length)
throws
Exception{
String
path="2:8080/videonews/ManagerServlet";
Map<String,String>
map=new
HashMap<String,String>();
map.put("title",
title);
map.put("timelength",
length);
return
sendGETRequest(path,map,"UTF-8");
}
/*
*
發(fā)送Get請求
*
path請求路徑
*
map請求參數(shù)
*
*/
private
static
boolean
sendGETRequest(String
path,
Map<String,
String>
map,String
ecoding)
throws
Exception{
/*將路徑拼成2:8080/videonews/ManagerServlet?title=XXX&timelength=90*/
StringBuilder
url=new
StringBuilder(path);
url.append("?");
//map迭代器Entry<Key,
Value>
for(Map.Entry<String,
String>
entry:map.entrySet()){
url.append(entry.getKey()).append("=");
//ecoding是上面?zhèn)鱽淼摹癠TF-8”,為了防止中文亂碼
url.append(URLEncoder.encode(entry.getValue(),
ecoding));
url.append("&");
}
url.deleteCharAt(url.length()-1);
URL
url2=new
URL(url.toString());
HttpURLConnection
conn=(HttpURLConnection)
url2.openCo
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經(jīng)權益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
- 6. 下載文件中如有侵權或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 基于2025年的主題公園二期建設社會穩(wěn)定風險評估與風險評估策略實施
- 成果鑒定報告2025:新能源汽車動力電池性能評測
- 2025年醫(yī)療大數(shù)據(jù)在慢性病管理中的智能健康管理報告
- 醫(yī)療大數(shù)據(jù)隱私保護合規(guī)技術實施效果評估與優(yōu)化報告
- 世界糧食日宣傳周主題班會課件
- 新解讀《GB-T 39131-2020人工晶體材料術語》
- 新解讀《GB-T 38811-2020金屬材料 殘余應力 聲束控制法》
- 2025年中考地理二輪復習:讀圖、識圖、用圖解析版
- 2025年水噴射真空泵項目立項申請報告模板
- 2026年人教版高考英語一輪總復習綜合模擬檢測試卷及答案(十二)
- 高考狀元的高考復習每日學習計劃安排及高考字音字形大全
- 危急重癥的救治
- 婦科宮頸癌術后尿潴留的原因分析及護理-課件
- 模具及模具鉗工操作培訓
- 第四屆編校大賽試題和答案含編輯校對專題培訓課件
- T-GDNS 004-2023 醫(yī)療機構信息系統(tǒng)等級保護定級工作指南
- 醫(yī)學檢驗簡歷模板
- 中醫(yī)艾灸療法
- GB/T 3452.1-2005液壓氣動用O形橡膠密封圈第1部分:尺寸系列及公差
- GB 4706.13-2004家用和類似用途電器的安全制冷器具、冰淇淋機和制冰機的特殊要求
- 小學數(shù)學概念全部歸納
評論
0/150
提交評論