當前位置:首頁 » 安卓系統 » 安卓開發如何發請求

安卓開發如何發請求

發布時間: 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更新!

熱點內容
雲伺服器選擇什麼系統 發布:2024-05-09 01:55:51 瀏覽:966
mel腳本編程全攻略 發布:2024-05-09 01:54:43 瀏覽:478
如何在機房安裝ntp伺服器 發布:2024-05-09 01:13:57 瀏覽:205
ideajavaidea 發布:2024-05-09 01:02:14 瀏覽:964
oas存儲 發布:2024-05-09 00:57:49 瀏覽:800
android點擊彈出菜單 發布:2024-05-09 00:56:52 瀏覽:98
大家對雲伺服器認知度 發布:2024-05-09 00:46:00 瀏覽:659
思科視頻會議如何配置 發布:2024-05-09 00:45:59 瀏覽:669
centos安裝ftp伺服器配置 發布:2024-05-09 00:45:06 瀏覽:81
幕布電腦版伺服器連接失敗怎麼整 發布:2024-05-09 00:38:21 瀏覽:723