国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android采用GET方法進行網絡傳值

2019-10-22 18:18:23
字體:
來源:轉載
供稿:網友

前兩天學習了使用GET方法來進行安卓與WEB的網絡傳值問題。 

今天來說一下大概方法。

WEB應用

在這里,我只建立一個簡單的Servlet,用來接收安卓端發來的信息。

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     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); }   } 

 安卓客戶端

在這里,我要建立一個輸入框界面,讓用戶吧數據輸入進去,然后我再將數據通過get方式提交。 
XML界面(兩個輸入框,一個按鈕):

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"   xmlns:tools="http://schemas.android.com/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      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端的數據放在URL路徑,然后WEB端進行解析得到的,所以我們要在方法中將URL路徑給拼湊完成然后傳給WEB端(里面的IP是我tomcat服務器本機的ip)。

package com.example.service;   import java.net.HttpURLConnection; import java.net.URL; import java.net.URLEncoder; import java.util.HashMap; import java.util.Map;   public class NewsService {   /*    * 保存數據    * title 標題    * length 時長    * */ public static boolean save(String title, String length) throws Exception{ String path="http://10.20.124.72: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"); }   /*    * 發送Get請求    * path請求路徑    * map請求參數    * */ private static boolean sendGETRequest(String path, Map<String, String> map,String ecoding) throws Exception{ /*將路徑拼成http://10.20.124.72: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是上面傳來的“UTF-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.openConnection(); conn.setConnectTimeout(5000); conn.setRequestMethod("GET"); if(conn.getResponseCode() == 200){ return true; } return false; }   } 

上面如果傳到WEB端是成功的(即conn.getResponseCode() = 200),那么安卓端就會顯示“登陸成功”,而且在WEB編輯器的控制臺會以System.out.println方式打印出你傳去的信息。 

效果:

 Android,GET,網絡傳值

這里僅僅是一個傳值的演示,沒用用到數據庫和輸入輸出流,真正做開發的時候這些東西是少不了的,所以要學會將東西結合起來應用。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 讷河市| 五莲县| 清水河县| 县级市| 松原市| 承德市| 怀安县| 兴仁县| 兖州市| 台山市| 高陵县| 嘉兴市| 濮阳市| 汤阴县| 寿光市| 芜湖市| 建湖县| 卢湾区| 贵德县| 陕西省| 阳信县| 洛阳市| 乳源| 监利县| 绿春县| 绥棱县| 渝中区| 普安县| 海南省| 五指山市| 马公市| 张掖市| 湟中县| 襄汾县| 南汇区| 南岸区| 彝良县| 东阿县| 京山县| 黑水县| 都匀市|