当前位置:首页 » 安卓系统 » 安卓开发如何发请求

安卓开发如何发请求

发布时间: 2022-04-29 11:30:43

㈠ Android开发中怎么主动请求权限

自定义属于自己的permission 或属于开发者使用的同一个签名的permission。定义一个permission 就是在menifest文件中添加一个permission标签。

<permission android:description="string resource"
android:icon="drawable resource"
android:label="string resource"
android:name="string"
android:permissionGroup="string"
android:protectionLevel=["normal" | "dangerous" |
"signature" | "signatureOrSystem"] />

android:description :对权限的描述,一般是两句话,第一句话描述这个权限所针对的操作,第二句话告诉用户授予app这个权限会带来的后果
android:label: 对权限的一个简短描述
android:name :权限的唯一标识,一般都是使用 报名加权限名
android:permissionGroup: 权限所属权限组的名称
android:protectionLevel: 权限的等级,
normal 是最低的等级,声明次权限的app,系统会默认授予次权限,不会提示用户
dangerous 权限对应的操作有安全风险,系统在安装声明此类权限的app时会提示用户
signature 权限表明的操作只针对使用同一个证书签名的app开放
signatureOrSystem 与signature类似,只是增加了rom中自带的app的声明

android:name 属性是必须的,其他的可选,未写的系统会指定默认值

1、权限的声明(APP1)

<permission android:name="com.xxx.permission" />

<receiver

android:name="com.example.demo1"

android:permission="com.xxx.permission" >

<intent-filter>

<action android:name="com.test.action" />

</intent-filter>

</receiver>

<activity
android:name=".MainActivity"
android:label="@string/title_activity_main"
android:permission="com.xxx.permission" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

2、权限的使用(APP2)
<uses-permission android:name="com.xxx.permission" />

㈡ 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();
}

㈢ web端的项目怎么向Android端服务器发送请求

可使用Android自带的httpClient实现Android与java web之间的数据的交互。
具体实现代码:
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 发送大量的http请求怎么做最好呢

你应该写清楚应用场景,懒得写代码,只写思路
分两种使用场景吧,第一种,就是无脑post,对服务器造成很大负担(如果这就是你的目的),那你只需要写个timer循环,每隔一秒或者几秒post服务端就行了
第二种,在get到结果后循环post,这个方式比较合理,不需要timer循环,对服务器负荷也小,需要一个hanlderMessage,在get到结果或异常后给hanlderMessage发送消息,handlerMessage再执行一次post即可

㈤ 在安卓开发中,我利用post

一、需要用到的场景在jQuery中使用$.post()就可以方便的发起一个post请求,在android程序中有时也要从服务器获取一些数据,就也必须得使用post请求了。二、需要用到的主要类在android中使用post请求主要要用到的类是HttpPost、HttpResponse

㈥ Android开发中如何发送Get请求

u=wangyi&p=456"; /*建立HTTP Get对象*/ HttpGet httpRequest = new HttpGet(uriAPI); try { /*发送请求并等待响应*/ HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); /*若状态码为200 ok*/ if(httpResponse.getStatusLine().getStatusCode() == 200) { /*读*/ String strResult = EntityUtils.toString(httpResponse.getEntity()); /*去没有用的字符*/ strResult = eregi_replace("(\r\n|\r|\n|\n\r)","",strResult); mTextView1.setText(strResult); } else { mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString()); } } catch (ClientProtocolException e) { mTextView1.setText(e.getMessage().toString()); e.printStackTrace(); } catch (IOException e) { mTextView1.setText(e.getMessage().toString()); e.printStackTrace(); } catch (Exception e) { mTextView1.setText(e.getMessage().toString()); e.printStackTrace(); } 这是在Android开发中常用的发送Get请求的方式。

㈦ 如何在安卓程序开发中向服务器请求xml文件

我也在弄这个问题,还没弄好。。

这个网址感觉有用,给你参考一下:
http://www.cnblogs.com/94007boy/archive/2012/11/27/2790990.html

㈧ android网络请求的几种方式有哪些有什么不同

参考内容如下:
Android应用经常会和服务器端交互,这就需要手机客户端发送网络请求,下面介绍四种常用网络请求方式,我这边是通过Android单元测试来完成这四种方法的,还不清楚Android的单元测试的同学们请看Android开发技巧总结中的Android单元测试的步骤一文。

java.net包中的HttpURLConnection类

Get方式:

// Get方式请求
public static void requestByGet() throws Exception {
String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
// 新建一个URL对象
URL url = new URL(path);
// 打开一个HttpURLConnection连接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 设置连接超时时间
urlConn.setConnectTimeout(5 * 1000);
// 开始连接
urlConn.connect();
// 判断请求是否成功
if (urlConn.getResponseCode() == HTTP_200) {
// 获取返回的数据
byte[] data = readStream(urlConn.getInputStream());
Log.i(TAG_GET, "Get方式请求成功,返回数据如下:");
Log.i(TAG_GET, new String(data, "UTF-8"));
} else {
Log.i(TAG_GET, "Get方式请求失败");
}
// 关闭连接
urlConn.disconnect();
}

Post方式:

// Post方式请求
public static void requestByPost() throws Throwable {
String path = "https://reg.163.com/logins.jsp";
// 请求的参数转换为byte数组
String params = "id=" + URLEncoder.encode("helloworld", "UTF-8")
+ "&pwd=" + URLEncoder.encode("android", "UTF-8");
byte[] postData = params.getBytes();
// 新建一个URL对象
URL url = new URL(path);
// 打开一个HttpURLConnection连接
HttpURLConnection urlConn = (HttpURLConnection) url.openConnection();
// 设置连接超时时间
urlConn.setConnectTimeout(5 * 1000);
// Post请求必须设置允许输出
urlConn.setDoOutput(true);
// Post请求不能使用缓存
urlConn.setUseCaches(false);
// 设置为Post请求
urlConn.setRequestMethod("POST");
urlConn.setInstanceFollowRedirects(true);
// 配置请求Content-Type
urlConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencode");
// 开始连接
urlConn.connect();
// 发送请求参数
DataOutputStream dos = new DataOutputStream(urlConn.getOutputStream());
dos.write(postData);
dos.flush();
dos.close();
// 判断请求是否成功
if (urlConn.getResponseCode() == HTTP_200) {
// 获取返回的数据
byte[] data = readStream(urlConn.getInputStream());
Log.i(TAG_POST, "Post请求方式成功,返回数据如下:");
Log.i(TAG_POST, new String(data, "UTF-8"));
} else {
Log.i(TAG_POST, "Post方式请求失败");
}
}

org.apache.http包中的HttpGet和HttpPost类

Get方式:

// HttpGet方式请求
public static void requestByHttpGet() throws Exception {
String path = "https://reg.163.com/logins.jsp?id=helloworld&pwd=android";
// 新建HttpGet对象
HttpGet httpGet = new HttpGet(path);
// 获取HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 获取HttpResponse实例
HttpResponse httpResp = httpClient.execute(httpGet);
// 判断是够请求成功
if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
// 获取返回的数据
String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
Log.i(TAG_HTTPGET, "HttpGet方式请求成功,返回数据如下:");
Log.i(TAG_HTTPGET, result);
} else {
Log.i(TAG_HTTPGET, "HttpGet方式请求失败");
}
}

Post方式:

// HttpPost方式请求
public static void requestByHttpPost() throws Exception {
String path = "https://reg.163.com/logins.jsp";
// 新建HttpPost对象
HttpPost httpPost = new HttpPost(path);
// Post参数
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", "helloworld"));
params.add(new BasicNameValuePair("pwd", "android"));
// 设置字符集
HttpEntity entity = new UrlEncodedFormEntity(params, HTTP.UTF_8);
// 设置参数实体
httpPost.setEntity(entity);
// 获取HttpClient对象
HttpClient httpClient = new DefaultHttpClient();
// 获取HttpResponse实例
HttpResponse httpResp = httpClient.execute(httpPost);
// 判断是够请求成功
if (httpResp.getStatusLine().getStatusCode() == HTTP_200) {
// 获取返回的数据
String result = EntityUtils.toString(httpResp.getEntity(), "UTF-8");
Log.i(TAG_HTTPGET, "HttpPost方式请求成功,返回数据如下:");
Log.i(TAG_HTTPGET, result);
} else {
Log.i(TAG_HTTPGET, "HttpPost方式请求失败");
}
}

以上是一些部分代码,测试的时候在测试类中运行对应的测试方法即可。

㈨ android的消息推送怎么做

android的消息推送办法:
1、可以使用轮询(Pull)方式
就是用客户端定时向服务器发送相关的信息,一旦服务器有变化就会马上同步消息。但这种方式对服务器的压力是十分大的,而且比较费客户端的流量,就是不断地向服务器发送请求,但是这样开发很简单。
2、使用持久连接(Push)方式
就是客户端和服务器之间建立起一连接,这样就可以实现消息的及时发送,而且这种方式开发难度大,开发周期较长。但是这是一种最常使用的方式,目前主流的消息推送都是通过这种方式做的。
选择消息推送软件,深圳极光就不错。极光截至2020年12月,已有超169万款APP在使用极光提供的服务。而且专注于为开发者提供稳定高效的消息推送、一键认证以及流量变现等服务,助力开发者的运营、增长与变现。

㈩ 安卓开发网络请求是怎么做的异步

安卓限制了发网络请求只能在子线程,不然会报错的!如果你是用asynHttpClient这类框架的话,它会将你的请求放在队列里,通过线程池来发出请求的,也就是说里面会自己开启子线程请求。 如果使用UrHttpConnection或HttpClient的话,就需要自己手动开启子线程进行请求! 下面举两个方法: 第一,new Thread发出请求,handler进行通讯! 第二,asynTask进行异步请求,重写方法进行UI更新!

热点内容
dirt5需要什么配置 发布:2024-05-20 06:02:58 浏览:542
怎么把电脑锁上密码 发布:2024-05-20 05:19:09 浏览:985
安卓为什么连上wifi后没有网络 发布:2024-05-20 05:17:50 浏览:419
安卓usb在设置哪里 发布:2024-05-20 05:03:03 浏览:187
绥化编程 发布:2024-05-20 04:59:44 浏览:991
基本原理和从头计算法 发布:2024-05-20 04:50:32 浏览:30
配置情况指的是什么 发布:2024-05-20 04:48:14 浏览:497
那个程序用来编译源文件 发布:2024-05-20 04:46:45 浏览:551
小程序需要数据库吗 发布:2024-05-20 04:35:14 浏览:338
链接sqlserver 发布:2024-05-20 04:27:53 浏览:210