欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android采用GET方法進(jìn)行網(wǎng)絡(luò)傳值

 更新時(shí)間:2017年12月27日 10:50:21   作者:光仔December  
這篇文章主要為大家詳細(xì)介紹了Android采用GET方法進(jìn)行網(wǎng)絡(luò)傳值的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

前兩天學(xué)習(xí)了使用GET方法來(lái)進(jìn)行安卓與WEB的網(wǎng)絡(luò)傳值問(wèn)題。 

今天來(lái)說(shuō)一下大概方法。

WEB應(yīng)用

在這里,我只建立一個(gè)簡(jiǎn)單的Servlet,用來(lái)接收安卓端發(fā)來(lái)的信息。

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("時(shí)長(zhǎng)"+timelength); 
} 
  
public void doPost(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException { 
 doGet(request,response); 
} 
  
} 

 安卓客戶端

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

<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(這里的線程問(wèn)題在前面講過(guò)):

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{ 
    //開(kāi)啟線程 
    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方法(這個(gè)類是用來(lái)處理信息,然后以get方式傳往WEB端)。這里我要說(shuō)一句,我們采用的GET方法,是將需要傳遞給WEB端的數(shù)據(jù)放在URL路徑,然后WEB端進(jìn)行解析得到的,所以我們要在方法中將URL路徑給拼湊完成然后傳給WEB端(里面的IP是我tomcat服務(wù)器本機(jī)的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 { 
  /* 
   * 保存數(shù)據(jù) 
   * title 標(biāo)題 
   * length 時(shí)長(zhǎng) 
   * */ 
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"); 
} 
  /* 
   * 發(fā)送Get請(qǐng)求 
   * path請(qǐng)求路徑 
   * map請(qǐng)求參數(shù) 
   * */ 
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是上面?zhèn)鱽?lái)的“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),那么安卓端就會(huì)顯示“登陸成功”,而且在WEB編輯器的控制臺(tái)會(huì)以System.out.println方式打印出你傳去的信息。 

效果:

 

這里僅僅是一個(gè)傳值的演示,沒(méi)用用到數(shù)據(jù)庫(kù)和輸入輸出流,真正做開(kāi)發(fā)的時(shí)候這些東西是少不了的,所以要學(xué)會(huì)將東西結(jié)合起來(lái)應(yīng)用。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android實(shí)現(xiàn)向本地寫入一個(gè)XML文件和解析XML文件

    Android實(shí)現(xiàn)向本地寫入一個(gè)XML文件和解析XML文件

    這篇文章主要介紹了Android實(shí)現(xiàn)向本地寫入一個(gè)XML文件和解析XML文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-03-03
  • 使用RecyclerView添加Header和Footer的方法

    使用RecyclerView添加Header和Footer的方法

    RecyclerView雖然作為L(zhǎng)istView的替代者有著較好的性能提升,但是ListView的一些常用功能卻沒(méi)有提供,比如我們平時(shí)會(huì)經(jīng)常用到的addHeaderView,addFooterView,既然RecyclerView沒(méi)有提供這個(gè)方法,我們應(yīng)該如何為列表添加頭部和底部呢,接下來(lái)通過(guò)本文給大家介紹
    2016-03-03
  • viewPager+fragment刷新緩存fragment的方法

    viewPager+fragment刷新緩存fragment的方法

    這篇文章主要介紹了viewPager+fragment刷新緩存fragment的方法,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下
    2017-03-03
  • Android LayoutTransiton實(shí)現(xiàn)簡(jiǎn)單的錄制按鈕

    Android LayoutTransiton實(shí)現(xiàn)簡(jiǎn)單的錄制按鈕

    這篇文章主要介紹了Android LayoutTransiton實(shí)現(xiàn)簡(jiǎn)單的錄制按鈕,主要實(shí)現(xiàn)開(kāi)始,暫停,停止和顯示錄制時(shí)間長(zhǎng)度,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-06-06
  • 最新評(píng)論