导航:首页 > 操作系统 > android网络请求post

android网络请求post

发布时间:2022-09-14 04:04:32

android post网络数据的请求

public static String HttpPostMethod(String get_url,
List<NameValuePair> params) {
String result = "";
HttpParams basicHttpParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(basicHttpParams, 15 * 1000);
HttpConnectionParams.setSoTimeout(basicHttpParams, 15 * 1000);
HttpClient htc = new DefaultHttpClient(basicHttpParams);
HttpResponse httpResponse = null;
try {
HttpPost httpPost = new HttpPost(get_url);
// response = htc.execute(request);
// 设置httpPost请求参数
// 创建参数队列
httpPost.setHeader("Content-Type", "application/x-www-form-urlencoded");//json
httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
// httpResponse = new DefaultHttpClient().execute(httpPost);
httpResponse = htc.execute(httpPost);
// System.out.println(httpResponse.getStatusLine().getStatusCode());
Log.d("zh1", get_url+" " + httpResponse.getStatusLine().getStatusCode());
if (httpResponse.getStatusLine().getStatusCode() == 200) {
// 第三步,使用getEntity方法活得返回结果
result = EntityUtils.toString(httpResponse.getEntity(), HTTP.UTF_8);
if(result==null||result.equals("")){
return null;
}
}else{
return null;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = null;
} catch (IOException e) {
e.printStackTrace();
result = null;
}
return result;
}

Ⅱ Android开发POST请求怎么使用

网页链接

这个博客表述的比较详细的了,一般请求的话直接使用okhttp3这个库就可以了。如果有大量接口的话还可以跟Retrofit2一起结合来使用。望采纳。

Ⅲ android 怎么发送post请求并接收二进制数据

可使用android自带的httpclient框架实现向服务器发起get或post请求,以下为完整的示例代码:
1. GET 方式传递参数
//先将参数放入List,再对参数进行URL编码
List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "数据")); //增加参数1
params.add(new BasicNameValuePair("param2", "value2"));//增加参数2
String param = URLEncodedUtils.format(params, "UTF-8");//对参数编码
String baseUrl = "服务器接口完整URL";
HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//将URL与参数拼接
HttpClient httpClient = new DefaultHttpClient();
try {
HttpResponse response = httpClient.execute(getMethod); //发起GET请求
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//获取服务器响应内容
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

2. POST方式 方式传递参数
//和GET方式一样,先将参数放入List
params = new LinkedList<BasicNameValuePair>();
params.add(new BasicNameValuePair("param1", "Post方法"));//增加参数1
params.add(new BasicNameValuePair("param2", "第二个参数"));//增加参数2
try {
HttpPost postMethod = new HttpPost(baseUrl);//创建一个post请求
postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中
HttpResponse response = httpClient.execute(postMethod); //执行POST方法
Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码
Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //获取响应内容
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Ⅳ 关于android post请求的问题

这个是网络请求不能用handler,必须新开一个线程,你可以在执行结果的位置用handler发送一个message,msg.obj携带结果。handler.sendmessage()。
handler需要在activity这样写:
Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
switch(msg.what){
case 1:
//在这里刷新iew
break;

};
}

};

Ⅳ android中post是什么意思

一、需要用到的场景 在jQuery中使用$.post()就可以方便的发起一个post请求,在android程序中有时也要从服务器获取一些数据,就也必须得使用post请求了。 二、需要用到的主要类 在android中使用post请求主要要用到的类是HttpPost、HttpResponse、EntityUtils 三、主要思路 1、创建HttpPost实例,设置需要请求服务器的url。 2、为创建的HttpPost实例设置参数,参数设置时使用键值对的方式用到NameValuePair类。 3、发起post请求获取返回实例HttpResponse 4、使用EntityUtils对返回值的实体进行处理(可以取得返回的字符串,也可以取得返回的byte数组) 代码也比较简单,注释也加上了,就直接贴出来了 [java] package com.justsy.url; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.protocol.HTTP; import org.apache.http.util.EntityUtils; import android.app.Activity; import android.os.Bundle; public class HttpURLActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); System.out.println("start url..."); String url = "192.168.2.112:8080/JustsyApp/Applet"; // 第一步,创建HttpPost对象 HttpPost httpPost = new HttpPost(url); // 设置HTTP POST请求参数必须用NameValuePair对象 List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("action", "downloadAndroidApp")); params.add(new BasicNameValuePair("packageId", "89dcb664-50a7-4bf2-aeed-49c08af6a58a")); params.add(new BasicNameValuePair("uuid", "test_ok1")); HttpResponse httpResponse = null; try { // 设置httpPost请求参数 httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); httpResponse = new DefaultHttpClient().execute(httpPost); //System.out.println(httpResponse.getStatusLine().getStatusCode()); if (httpResponse.getStatusLine().getStatusCode() == 200) { // 第三步,使用getEntity方法活得返回结果 String result = EntityUtils.toString(httpResponse.getEntity()); System.out.println("result:" + result); T.displayToast(HttpURLActivity.this, "result:" + result); } } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } System.out.println("end url..."); setContentView(R.layout.main); } } ADD:使用HttpURLConnection 进行post请求 [java] String path = "192.168.2.115:8080/android-web-server/httpConnectServlet.do?PackageID=89dcb664-50a7-4bf2-aeed-49c08af6a58a"; URL url = new URL(path); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setConnectTimeout(5000); System.out.println(conn.getResponseCode()); ============================================================================================================================ 通过get和post方式向服务器发送请求 首先说一下get和post的区别 get请求方式是将提交的参数拼接在url地址后面,例如/index.jsp?num=23&jjj=888; 但是这种形式对于那种比较隐私的参数是不适合的,而且参数的大小也是有限制的,一般是1K左右吧,对于上传文件 就不是很适合。 post请求方式是将参数放在消息体内将其发送到服务器,所以对大小没有限制,对于隐私的内容也比较合适。 如下Post请求 POST /LoginCheck HTTP/1.1 Accept: text/html, application/xhtml+xml, */* Referer: 192.168.2.1/login.asp Accept-Language: zh-CN User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0; BOIE9;ZHCN) Content-Type: application/x-www-form-urlencoded Accept-Encoding: gzip, deflate Host: 192.168.2.1 Content-Length: 39 Connection: Keep-Alive Cache-Control: no-cache Cookie: language=en Username=admin&checkEn=0&Password=admin //参数位置 在android中用get方式向服务器提交请求: 在android模拟器中访问本机中的tomcat服务器时,注意:不能写localhost,因为模拟器是一个单独的手机系统,所以要写真是的IP地址。 否则无法访问到服务器。 //要访问的服务器地址,下面的代码是要向服务器提交用户名和密码,提交时中文先要经过URLEncoder编码,因为模拟器默认的编码格式是utf-8 //而tomcat内部默认的编码格式是ISO8859-1,所以先将参数进行编码,再向服务器提交。 private String address = "192.168.2.101:80/server/loginServlet"; public boolean get(String username, String password) throws Exception { username = URLEncoder.encode(username);// 中文数据需要经过URL编码 password = URLEncoder.encode(password); String params = "username=" + username + "&password=" + password; //将参数拼接在URl地址后面 URL url = new URL(address + "?" + params); //通过url地址打开连接 HttpURLConnection conn = (HttpURLConnection) url.openConnection(); //设置超时时间 conn.setConnectTimeout(3000); //设置请求方式 conn.setRequestMethod("GET"); //如果返回的状态码是200,则一切Ok,连接成功。 return conn.getResponseCode() == 200; } 在android中通过post方式提交数据。 public boolean post(String username, String password) throws Exception { username = URLEncoder.encode(username);// 中文数据需要经过URL编码 password = URLEncoder.encode(password); String params = "username=" + username + "&password=" + password; byte[] data = params.getBytes(); URL url = new URL(address); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setConnectTimeout(3000); //这是请求方式为POST conn.setRequestMethod("POST"); //设置post请求必要的请求头 conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");// 请求头, 必须设置 conn.setRequestProperty("Content-Length", data.length + "");// 注意是字节长度, 不是字符长度 conn.setDoOutput(true);// 准备写出 conn.getOutputStream().write(data);// 写出数据 return conn.getResponseCode() == 200; }

Ⅵ android get/post如何实现多参数请求

可使用android自带的httpclient框架实现。


  1. GET 方式传递参数

//先将参数放入List,再对参数进行URL编码

List<BasicNameValuePair> params = new LinkedList<BasicNameValuePair>();

params.add(new BasicNameValuePair("param1", "数据")); //增加参数1

params.add(new BasicNameValuePair("param2", "value2"));//增加参数2

String param = URLEncodedUtils.format(params, "UTF-8");//对参数编码


String baseUrl = "服务器接口完整URL";


HttpGet getMethod = new HttpGet(baseUrl + "?" + param);//将URL与参数拼接

HttpClient httpClient = new DefaultHttpClient();


try {

HttpResponse response = httpClient.execute(getMethod); //发起GET请求

Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码

Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"));//获取服务器响应内容

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}


2.POST方式 方式传递参数

//和GET方式一样,先将参数放入List

params = new LinkedList<BasicNameValuePair>();

params.add(new BasicNameValuePair("param1", "Post方法"));//增加参数1

params.add(new BasicNameValuePair("param2", "第二个参数"));//增加参数2

try {

HttpPost postMethod = new HttpPost(baseUrl);//创建一个post请求

postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8")); //将参数填入POST Entity中

HttpResponse response = httpClient.execute(postMethod); //执行POST方法

Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode()); //获取响应码

Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8")); //获取响应内容

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

Ⅶ android post请求和get请求的区别

Get请求与Post请求的区别
Get是向服务器发索取数据的一种请求,而Post是向服务器提交数据的一种请求
Get是获取信息,而不是修改信息,类似数据库查询功能一样,数据不会被修改
Get请求的参数会跟在url后进行传递,请求的数据会附在URL之后,以?分割URL和传输数据,参数之间以&相连,%XX中的XX为该符号以16进制表示的ASCII,如果数据是英文字母/数字,原样发送,如果是空格,转换为+,如果是中文/其他字符,则直接把字符串用BASE64加密
Get传输的数据有大小限制,因为GET是通过URL提交数据,那么GET可提交的数据量就跟URL的长度有直接关系了,不同的浏览器对URL的长度的限制是不同的。
GET请求的数据会被浏览器缓存起来,用户名和密码将明文出现在URL上,其他人可以查到历史浏览记录,数据不太安全。在服务器端,用Request.QueryString来获取Get方式提交来的数据
Post请求则作为http消息的实际内容发送给web服务器,数据放置在HTML Header内提交,Post没有限制提交的数据。Post比Get安全,当数据是中文或者不敏感的数据,则用get,因为使用get,参数会显示在地址,对于敏感数据和不是中文字符的数据,则用post
POST表示可能修改变服务器上的资源的请求,在服务器端,用Post方式提交的数据只能用Request.Form来获取

Ⅷ Android的post请求该怎么使用

post的参数包括:url(你请求的网站),date(发送的数据),header(所用的浏览器)这些都是常用的参数,更多的去查一下吧,我记得中软国际某个视频有讲解。

Ⅸ android中怎么发送post请求

<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=".MainActivity" >

<TextView
android:id="@+id/lblPostResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/butPost"
android:layout_centerHorizontal="true"
android:text="提交结果" />

<Button
android:id="@+id/butPost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
android:text="提交测试" />

阅读全文

与android网络请求post相关的资料

热点内容
安卓手机连车载的叫什么 浏览:223
怎么让自己的手机键盘变得好看app 浏览:53
能看qq的文件夹 浏览:515
android二维码生成代码 浏览:567
焦炉气压缩机 浏览:402
imap接收邮件服务器地址 浏览:291
小乔肖恩解压密码 浏览:645
php网页网盘源码 浏览:181
签到任务源码 浏览:814
母亲节的文案怎么写app 浏览:984
加密协议aes找不到 浏览:250
java服务器端开发源码 浏览:551
编译器编译运行快捷键 浏览:333
住房app怎么快速选房 浏览:174
怎么在电脑上编译成功 浏览:214
单片机可调时钟设计方案 浏览:192
qq文件夹密码忘记怎么找回 浏览:683
php扩展插件 浏览:608
解压视频厕所抽纸 浏览:952
app减脂怎么用 浏览:452