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的更多相关文章

  1. extern "C"——用“C”来规约在C++中用C的方式进行编译和链接

    C++中的extern “C”用法详解     extern "C"表明了一种编译规约,其中extern是关键字属性,“C”表征了编译器链接规范.对于extern "C& ...

  2. CentOS 6.9下KVM虚拟机网络Bridge(网桥)方式与NAT方式详解(转)

    摘要:KVM虚拟机网络配置的两种方式:NAT方式和Bridge方式.Bridge方式的配置原理和步骤.Bridge方式适用于服务器主机的虚拟化.NAT方式适用于桌面主机的虚拟化. NAT的网络结构图: ...

  3. JNDI提供了一种统一的方式,可以用在网络上查找和访问服务

    JNDI提供了一种统一的方式,可以用在网络上查找和访问服务.通过指定一个资源名称,该名称对应于数据库或命名服务中的一个记录,同时返回数据库连接建立所必须的信息. JNDI主要有两部分组成:应用程序编程 ...

  4. CentOS设置虚拟网卡做NAT方式和Bridge方式桥接

    CentOS设置虚拟网卡做NAT方式和Bridge方式桥接 http://www.centoscn.com/CentOS/config/2015/0225/4736.html 摘要:KVM虚拟机网络配 ...

  5. boost::ASIO的同步方式和异步方式

    http://blog.csdn.net/zhuky/article/details/5364574 http://blog.csdn.net/zhuky/article/details/536468 ...

  6. ASP.NET MVC传递Model到视图的多种方式之通用方式的使用

    ASP.NET MVC传递Model到视图的多种方式总结——通用方式的使用 有多种方式可以将数据传递到视图,如下所示: ViewData ViewBag PartialView TempData Vi ...

  7. Mybatis系列全解(七):全息视角看Dao层两种实现方式之传统方式与代理方式

    封面:洛小汐 作者:潘潘 一直以来 他们都说为了生活 便追求所谓成功 顶级薪水.名牌包包 还有学区房 · 不过 总有人丢了生活 仍一无所获 · 我比较随遇而安 有些事懒得明白 平日里问心无愧 感兴趣的 ...

  8. ZeroMQ接口函数之 :zmq_curve – 安全的认证方式和保密方式

    ZeroMQ 官方地址 :http://api.zeromq.org/4-0:zmq_curve zmq_curve(7) ØMQ Manual - ØMQ/4.1.0 Name zmq_curve  ...

  9. C#操作Excel的OLEDB方式与COM方式比较

    2013-03-15 13:42:54      作者:有理想的码农   在对Excel进行读写操作时,使用微软自身提供的解决方案,有两种(第三方方式除外),分别是OLEDB方式和调用COM组件的方式 ...

随机推荐

  1. echart 图表 在.net中生成图片的方法

    经过中午近两个小时的努力,终于可以实现了:echart 图表 在.net中生成图片 以下源代码: 前台页面: <!DOCTYPE html><html><head> ...

  2. (easy)LeetCode 231.Power of Two

    Given an integer, write a function to determine if it is a power of two. Credits:Special thanks to @ ...

  3. (easy)LeetCode 204.Count Primes

    Description: Count the number of prime numbers less than a non-negative number, n. Credits:Special t ...

  4. 图标的使用————JAVA——Swing

    public class MyImageIcon extends JFrame{    public MyImageIcon()    {    JFrame jf=new JFrame();     ...

  5. Regional Changchun Online--Ponds

    网址:http://acm.hdu.edu.cn/showproblem.php?pid=5438 Ponds Time Limit: 1500/1000 MS (Java/Others)    Me ...

  6. Log4j等级测试

    一.结论: 1./**debug.info.warn.error.fatal由低到高*/ 2.注意:log.error(message,e)不会打印异常堆栈信息. 二.测试过程 1.代码 packag ...

  7. 十步让 WebForm项目 变为 Mvc项目

    1.创建一个项目名为 App_Asp 的 Asp.NET 空 Web 应用程序2.添加全局应用程序类 Global.asax3.富文本打开 Global,修改 Inherits 为 App_Asp_G ...

  8. css 使用background背景实现border边框效果

    css中,我们一般使用border给html元素设置边框,但也可以使用background背景来模拟css边框效果,本文章向大家介绍css 使用background背景实现border边框效果,需要的 ...

  9. FZU Problem 2136 取糖果

     Problem 2136 取糖果 Time Limit: 1000 mSec    Memory Limit : 32768 KB  Problem Description 有N个袋子放成一排,每个 ...

  10. 关于AIR新浪登录测试

    /** *由于在应用申请中,我设置的域名属于新浪云,因此在本地测试的话肯定不能成功的,有个办法就是直接在新浪云那边授权成功后,将token的值直接使用post或者get方法传递过来,直接在本地 *lo ...