复习URLHttpConnection方式GET,POST方式链接网络解析uri
xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.xh.tx.postget.MainActivity" > <EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_username"
android:hint="请输入密码" /> <Button
android:id="@+id/bt_get"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/et_password"
android:text="GET提交"
/> <Button
android:id="@+id/bt_post"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/bt_get"
android:text="POST提交"
/> </RelativeLayout>
NetUtils:
package com.xh.tx.netUtils; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.net.URLEncoder; public class NetUtils
{
public static String getSubmit(String username,String password,String uri)
{
uri = uri +"?username=" + username + "&password=" + password; HttpURLConnection conn = getHttpURLConnection(uri);
// http://localhost:8080/TestServlet?username=zs&password=123 try {
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setRequestMethod("GET"); conn.connect(); //连接 连接的时候是否要传递参数过去 //先判断一下状态是否为200,如果为200则将in流转换为字符串
if(conn.getResponseCode() == 200)
{
String content = getStringFromInputStream(conn.getInputStream()); return content;
} } catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return null;
} private static String getStringFromInputStream(InputStream inputStream) throws IOException
{
byte[] buffer = new byte[1024];
ByteArrayOutputStream bytearray = new ByteArrayOutputStream();
int len = 0; while((len = inputStream.read(buffer, 0, 1024)) != -1)
{
bytearray.write(buffer);
} // String content = new String(bytearray.toByteArray(),"GBK"); return bytearray.toString();
} public static String postSubmit(String username,String password, String uri)
{
HttpURLConnection conn = getHttpURLConnection(uri); try {
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setRequestMethod("POST");
//如果你要兼容2.3版本,那么你必须添加一下这句话
conn.setDoInput(true); //参数传递
OutputStream out = conn.getOutputStream(); conn.connect();
out.write(("username="+username + "&password=" + password).getBytes()); if(conn.getResponseCode() == 200)
{
String content = getStringFromInputStream(conn.getInputStream());
return content;
}
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} return null;
} public static HttpURLConnection getHttpURLConnection(String uri)
{
try {
URL url = new URL(uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); return conn;
} catch (IOException e) {
e.printStackTrace();
}
return null; }
}
MainActivity:
package com.xh.tx.postget; import com.xh.tx.netUtils.NetUtils; import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { EditText et_username;
EditText et_password; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password); findViewById(R.id.bt_get).setOnClickListener(this);
findViewById(R.id.bt_post).setOnClickListener(this); } @Override
public void onClick(View v)
{
final String username = et_username.getText().toString();
final String password = et_password.getText().toString(); switch (v.getId()) {
case R.id.bt_get:
new Thread(new Runnable()
{
@Override
public void run()
{
final String status = NetUtils.getSubmit(username, password,"http://10.0.2.2:8080/baidu/LoginServelt"); runOnUiThread(new Runnable() {
@Override
public void run()
{
Toast.makeText(MainActivity.this, "返回的状态为:" + status, 0).show();
}
});
}
}).start();
break;
case R.id.bt_post:
new Thread(new Runnable()
{
@Override
public void run()
{
final String status = NetUtils.postSubmit(username, password,"http://10.0.2.2:8080/baidu/LoginServelt"); runOnUiThread(new Runnable() {
@Override
public void run()
{
Toast.makeText(MainActivity.this, "返回的状态为:" + status, 0).show();
}
});
}
}).start();
break; default:
break;
}
}
}
复习URLHttpConnection方式GET,POST方式链接网络解析uri的更多相关文章
- extern "C"——用“C”来规约在C++中用C的方式进行编译和链接
C++中的extern “C”用法详解 extern "C"表明了一种编译规约,其中extern是关键字属性,“C”表征了编译器链接规范.对于extern "C& ...
- CentOS 6.9下KVM虚拟机网络Bridge(网桥)方式与NAT方式详解(转)
摘要:KVM虚拟机网络配置的两种方式:NAT方式和Bridge方式.Bridge方式的配置原理和步骤.Bridge方式适用于服务器主机的虚拟化.NAT方式适用于桌面主机的虚拟化. NAT的网络结构图: ...
- JNDI提供了一种统一的方式,可以用在网络上查找和访问服务
JNDI提供了一种统一的方式,可以用在网络上查找和访问服务.通过指定一个资源名称,该名称对应于数据库或命名服务中的一个记录,同时返回数据库连接建立所必须的信息. JNDI主要有两部分组成:应用程序编程 ...
- CentOS设置虚拟网卡做NAT方式和Bridge方式桥接
CentOS设置虚拟网卡做NAT方式和Bridge方式桥接 http://www.centoscn.com/CentOS/config/2015/0225/4736.html 摘要:KVM虚拟机网络配 ...
- boost::ASIO的同步方式和异步方式
http://blog.csdn.net/zhuky/article/details/5364574 http://blog.csdn.net/zhuky/article/details/536468 ...
- ASP.NET MVC传递Model到视图的多种方式之通用方式的使用
ASP.NET MVC传递Model到视图的多种方式总结——通用方式的使用 有多种方式可以将数据传递到视图,如下所示: ViewData ViewBag PartialView TempData Vi ...
- Mybatis系列全解(七):全息视角看Dao层两种实现方式之传统方式与代理方式
封面:洛小汐 作者:潘潘 一直以来 他们都说为了生活 便追求所谓成功 顶级薪水.名牌包包 还有学区房 · 不过 总有人丢了生活 仍一无所获 · 我比较随遇而安 有些事懒得明白 平日里问心无愧 感兴趣的 ...
- ZeroMQ接口函数之 :zmq_curve – 安全的认证方式和保密方式
ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_curve zmq_curve(7) ØMQ Manual - ØMQ/4.1.0 Name zmq_curve ...
- C#操作Excel的OLEDB方式与COM方式比较
2013-03-15 13:42:54 作者:有理想的码农 在对Excel进行读写操作时,使用微软自身提供的解决方案,有两种(第三方方式除外),分别是OLEDB方式和调用COM组件的方式 ...
随机推荐
- nyoj 73 比大小
点击打开链接 比大小 时间限制:3000 ms | 内存限制:65535 KB 难度:2 描述 给你两个很大的数,你能不能判断出他们两个数的大小呢? 比如123456789123456789要大于 ...
- (easy)LeetCode 237.Delete Node in a Linked List
Write a function to delete a node (except the tail) in a singly linked list, given only access to th ...
- js获取上一页、当前页及域名url方法,JS反回上一页的方法
<html> <head> <title>js获取上一页url,js获取前一页地址,javascripts获取上一页url,javascript获取前一页地址< ...
- html5—— 应用程序缓存
使用 HTML5,通过创建 cache manifest 文件,可以轻松地创建 web 应用的离线版本. 什么是应用程序缓存(Application Cache)? HTML5 引入了应用程序缓存,这 ...
- [翻译]你不会想知道的kobject,kset,和ktypes
---------------------------------------------------------------------------------------------------- ...
- Github 恶搞教程(一起『玩坏』自己的 Github 吧)
最近在伯乐在线读到一篇趣文,<如何在 Github『正确』做贡献>,里面各种能人恶搞 Github 的『Public contributions』,下面截取几个小伙伴的战绩: 顺藤摸瓜,发 ...
- 计数排序(Count Sort )与插入排序(Insert Sort)
计数排序法:计数数组适用于当前数组密集的情况.例如(2,3,5,4,2,3,3,2,5,4) 方法:先找出最大值最小值,之后统计每个数出现的次数,根据次数从小到大往数组里添加 计数排序法是一种不需要比 ...
- Dll学习(二)__declspec用法详解
__declspec用于指定所给定类型的实例的与Microsoft相关的存储方式.其它的有关存储方式的修饰符如static与extern等是C和 C++语言的ANSI规范,而__declspec是一种 ...
- ASP.NET MVC 中使用 AjaxFileUpload 插件时,上传图片后不能显示(预览)
AjaxFileUpload 插件是一个很简洁很好用的上传文件的插件,可以实现异步上传功能,但是在 ASP.NET MVC中使用时,会出现上传图片后不能正确的显示的问题,经过仔细排查,终于找到原因,解 ...
- NOIP2003 传染病防治
描述 研究表明,这种传染病的传播具有两种很特殊的性质:第一是它的传播途径是树型的,一个人X只可能被某个特定的人Y感染,只要Y不得病,或者是XY之间的传播途径被切断,则X就不会得病. 第二是,这种疾病的 ...