首先加入注册文件 AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
访问互联网规则
GET 提交
-----------------------------------------------------------------------------------------------------------------------------------------------------
URL url=new URL("http://www.15780.cn/get.php"); //新建URL对象
HttpURLConnection connection = (HttpURLConnection)url.openConnection(); //新建URL链接返回对象
//设置该连接允许读取
connection.setDoInput(true);
//设置该连接允许写入
connection.setDoOutput(true);
//设置超时
connection.setConnectTimeout(1000);
//得到连接的输入流
InputStreamReader isr = new InputStreamReader(connection.getInputStream());
//再次包装为缓冲流
BufferedReader br = new BufferedReader(isr);
//用来存放临时读取的行
String tempResult = null;
//用来保存全部的结果
String result = null;
//不停地读取行,知道结束
while((tempResult = br.readLine()) != null)
{
//将结果保存
result += tempResult + "\n" ;
}
t.setText("取到啥了"+Html.fromHtml(result)); // Html.fromHtml() 将字符串转化成HTML 否则 会显示html代码
br.close();
isr.close();
传递中文字符 message = java.net.URLEncoder.encode("中文字符","utf-8");
HtppGet
-------------------------------------------------------------------------------------------------------------------
List<NameValuePair> qparams = new ArrayList<NameValuePair>();
qparams.add(new BasicNameValuePair("ab","xxxxx"));
qparams.add(new BasicNameValuePair("cd","孙悟空"));
qparams.add(new BasicNameValuePair("e","xxxxxxx"));
URI uri = null;
uri = URIUtils.createURI("http","www.15780.cn", -1, "/get.php", URLEncodedUtils.format(qparams, "UTF-8"),null);
HttpGet httpRequest = new HttpGet(uri);
HttpClient httpClient = new DefaultHttpClient();
//执行请求返回结果
HttpResponse httpResponse = httpClient.execute(httpRequest);
//判断结果状态
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
//得到结果内容
String result = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
t.setText(Html.fromHtml(result));
}
else
{
t.setText("应答错误:"+httpResponse.getStatusLine().toString());
}
HttpPost 提交
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
String url = "http://www.15780.cn/post.php";
//新建POST类型的请求
HttpPost httpRequest = new HttpPost(url);
//新建需要传递参数的数据结构
List<NameValuePair> params = new ArrayList<NameValuePair>();
//新建键值对
BasicNameValuePair pair1 = new BasicNameValuePair("a", "ss是是AAAA");
BasicNameValuePair pair2 = new BasicNameValuePair("b", "BBBBB");
//将数据添加到键值对中
params.add(pair1);
params.add(pair2);
//设置编码方式
HttpEntity entity = new UrlEncodedFormEntity(params, "utf-8");
httpRequest.setEntity(entity);
//新建HTTP客户端
HttpClient httpClient = new DefaultHttpClient();
//执行请求得到响应
HttpResponse httpResponse = httpClient.execute(httpRequest);
//判断响应的状态是否成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
//得到结果字符串
String result = EntityUtils.toString(httpResponse.getEntity(),"UTF-8");
t.setText(Html.fromHtml(result));
}
else
{
t.setText("应答错误:"+httpResponse.getStatusLine().toString());
}
Toast.makeText(getBaseContext(), "POST", Toast.LENGTH_SHORT).show();