【Android开发】采用GET方法进行网络传值

简介:

前两天学习了使用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 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);
}
 
}


 

安卓客户端

在这里,我要建立一个输入框界面,让用户吧数据输入进去,然后我再将数据通过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 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端的数据放在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方式打印出你传去的信息。

 

效果:

 

这里仅仅是一个传值的演示,没用用到数据库和输入输出流,真正做开发的时候这些东西是少不了的,所以要学会将东西结合起来应用。

相关文章
|
2天前
|
安全 Android开发 iOS开发
探索安卓与iOS开发的差异:平台特性与用户体验的深度对比
在移动应用开发的广阔天地中,安卓和iOS两大平台各占半壁江山。本文旨在通过数据驱动的分析方法,深入探讨这两大操作系统在开发环境、用户界面设计及市场表现等方面的差异。引用最新的行业报告和科研数据,结合技术专家的观点,本文将提供对开发者和市场分析师均有价值的洞见。
|
5天前
|
Java 开发工具 Android开发
探索Android与iOS开发的差异:平台选择对项目成功的影响
在移动应用开发的广阔天地中,Android和iOS两大平台各自占据着半壁江山。本文将深入探讨这两个平台在开发过程中的关键差异点,包括编程语言、开发工具、用户界面设计、性能优化以及市场覆盖等方面。通过对这些关键因素的比较分析,旨在为开发者提供一个清晰的指南,帮助他们根据项目需求和目标受众做出明智的平台选择。
|
5天前
|
编解码 Android开发 iOS开发
深入探索Android与iOS开发的差异与挑战
【6月更文挑战第24天】在移动应用开发的广阔舞台上,Android和iOS两大操作系统扮演着主角。它们各自拥有独特的开发环境、工具集、用户基础及市场策略。本文将深度剖析这两个平台的开发差异,并探讨开发者面临的挑战,旨在为即将踏入或已在移动开发领域奋斗的开发者提供一份实用指南。
27 13
|
5天前
|
缓存 网络协议 安全
Android网络面试题之Http基础和Http1.0的特点
**HTTP基础:GET和POST关键差异在于参数传递方式(GET在URL,POST在请求体),安全性(POST更安全),数据大小限制(POST无限制,GET有限制),速度(GET较快)及用途(GET用于获取,POST用于提交)。面试中常强调POST的安全性、数据量、数据类型支持及速度。HTTP 1.0引入了POST和HEAD方法,支持多种数据格式和缓存,但每个请求需新建TCP连接。**
20 5
|
3天前
|
安全 网络协议 算法
Android网络基础面试题之HTTPS的工作流程和原理
HTTPS简述 HTTPS基于TCP 443端口,通过CA证书确保服务器身份,使用DH算法协商对称密钥进行加密通信。流程包括TCP握手、证书验证(公钥解密,哈希对比)和数据加密传输(随机数加密,预主密钥,对称加密)。特点是安全但慢,易受特定攻击,且依赖可信的CA。每次请求可能复用Session ID以减少握手。
13 2
|
4天前
|
监控 Android开发 iOS开发
探索Android与iOS开发的差异:平台、工具和用户体验的比较
【6月更文挑战第25天】在移动应用开发的广阔天地中,Android和iOS两大平台各领风骚,它们在开发环境、工具选择及用户体验设计上展现出独特的风貌。本文将深入探讨这两个操作系统在技术实现、市场定位和用户交互方面的关键差异,旨在为开发者提供一个全景式的视图,帮助他们在面对项目决策时能够更加明智地选择适合自己项目需求的平台。
|
7天前
|
XML Java 开发工具
Android Studio开发Android TV
【6月更文挑战第19天】
|
4天前
|
缓存 测试技术 Shell
详细解读Android开发命令行完全攻略
详细解读Android开发命令行完全攻略
|
4天前
|
缓存 网络协议 Android开发
Android网络面试题之Http1.1和Http2.0
HTTP/1.1 引入持久连接和管道机制提升效率,支持分块传输编码和更多请求方式如PUT、PATCH。Host字段指定服务器域名,RANGE用于断点续传。HTTP/2变为二进制协议,实现多工处理,头信息压缩和服务器推送,减少延迟并优化资源加载。HTTP不断发展,从早期的简单传输到后来的高效交互。
17 0
Android网络面试题之Http1.1和Http2.0
|
8天前
|
Java 开发工具 Android开发
安卓与iOS开发差异解析
【6月更文挑战第21天】本文旨在深入探讨安卓和iOS两大移动操作系统在应用开发过程中的主要差异。通过对比分析,揭示各自的设计哲学、编程语言选择、用户界面构建、性能优化策略以及发布流程的异同。文章将提供开发者视角下的实用信息,帮助他们更好地理解各自平台的特点和挑战,从而做出更明智的开发决策。

热门文章

最新文章