Android采用GET方法进行网络传值

所属分类: 软件编程 / Android 阅读数: 43
收藏 0 赞 0 分享

前两天学习了使用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方式打印出你传去的信息。 

效果:

 

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android异常 java.lang.IllegalStateException解决方法

这篇文章主要介绍了Android异常 java.lang.IllegalStateException解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Split()字符串分割特殊用法案例详解

本文通过案例的形式给大家详细介绍了android中split()字符串分割特殊用法的知识,非常不错具有参考借鉴价值,感兴趣的朋友参考下
收藏 0 赞 0 分享

Android仿新浪微博启动界面或登陆界面(1)

这篇文章主要为大家详细介绍了Android仿新浪微博启动界面或登陆界面的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android仿新浪微博oauth2.0授权界面实现代码(2)

这篇文章主要为大家详细介绍了Android仿新浪微博oauth2.0授权界面实现代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发中使用sqlite实现新闻收藏和取消收藏的功能

本篇文章主要介绍了sqlite实现新闻收藏和取消收藏功能,主要涉及到oracle数据库方面的内容,对于Android开发sqlite实现收藏和取消功能感兴趣的朋友可以参考下本文
收藏 0 赞 0 分享

Android仿新浪微博分页管理界面(3)

这篇文章主要为大家详细介绍了Android仿新浪微博分页管理界面,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android UI自定义ListView实现下拉刷新和加载更多效果

这篇文章主要介绍了Android UI自定义ListView实现下拉刷新和加载更多效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android—基于微信开放平台v3SDK开发(微信支付填坑)

这篇文章主要介绍了Android—基于微信开放平台v3SDK开发(微信支付填坑),具有一定的参考价值,有需要的可以了解一下。
收藏 0 赞 0 分享

Android仿新浪微博自定义ListView下拉刷新(4)

这篇文章主要为大家详细介绍了Android仿新浪微博自定义ListView下拉刷新,重点介绍了Adapter的详细代码,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android控件之使用ListView实现时间轴效果

这篇文章主要介绍了Android基础控件之使用ListView实现时间轴效果的相关资料,本文是以查看物流信息为例,给大家介绍了listview时间轴的实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多