有沒有一種推薦的方式在Android做發佈請求?因爲我用HttpPost
和HttpClient
之前執行POST請求,但這些類在API級別現在已經過時22.有沒有推薦的方式在android中發佈post請求?
0
A
回答
0
是的,他們已被棄用。您可以使用Google Dev推薦的Volley。
Volley提供以下好處: 自動調度網絡請求。 多個併發網絡連接。 標準HTTP緩存一致性的透明磁盤和內存響應緩存。 支持請求優先級。 取消請求API。您可以取消單個請求,也可以設置要取消的請求的範圍或範圍。 易於定製,例如,重試和退避。 強大的排序使得使用從網絡異步獲取的數據可以輕鬆正確地填充您的UI。
排球是非常容易使用:
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
0
您可以使用HttpURLConnection
public String makeRequest(String pageURL, String params)
{
String result = null;
String finalURL =pageURL;
Logger.i("postURL", finalURL);
Logger.i("data", params);
try {
URL url = new URL(finalURL);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.setRequestMethod("POST");
urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
urlConnection.setRequestProperty("Accept", "application/json");
OutputStream os = urlConnection.getOutputStream();
os.write(params.getBytes("UTF-8"));
os.close();
int HttpResultCode =urlConnection.getResponseCode();
if(HttpResultCode ==HttpURLConnection.HTTP_OK){
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
result = convertStreamToString(in);
Logger.i("API POST RESPONSE",result);
}else{
Logger.e("Error in response ", "HTTP Error Code "+HttpResultCode +" : "+urlConnection.getResponseMessage());
}
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
然後你流轉換爲字符串
private String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append(line).append('\n');
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
創建您的JSON
JSONObject j=new JSONObject();
try {
j.put("name","hello");
j.put("email","[email protected]");
} catch (JSONException e) {
e.printStackTrace();
}
//Call the method
makeRequest("www.url.com",j.toString());
0
只是爲了展示另一種可能的庫,可以幫助你:OkHttp
,從他們的webseite一個例子後:
public static final MediaType JSON
= MediaType.parse("application/json; charset=utf-8");
OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
RequestBody body = RequestBody.create(JSON, json);
Request request = new Request.Builder()
.url(url)
.post(body)
.build();
Response response = client.newCall(request).execute();
return response.body().string();
}
或Retrofit。
你基本上創建包含其餘的API和參數你調用註釋的接口,可以接收解析JSON模式是這樣的:
public interface MyService {
@POST("/api")
void createTask(@Body CustomObject o, Callback<CustomObject > cb);
}
您還可以設置他們兩個在一起,這裏是一個指導,幫助我很多:https://futurestud.io/blog/retrofit-getting-started-and-android-client/
即使這不是谷歌文檔推薦的官方方式,這些都是不錯的庫,值得一看。
相關問題
- 1. Scrapy沒有發佈POST請求
- 2. POST請求沒有在Swift中發送
- 3. HTTP請求在沒有正文的esp8266中發佈POST
- 4. 有沒有簡單的方法使用Node.js發送POST請求?
- 5. AVaudioRecorder發佈請求iOS沒有發送
- 6. 沒有發佈Ajax請求的參數
- 7. POST請求沒有通過
- 8. 快速發佈POST請求沒有什麼
- 9. Django錯誤發佈請求:'模塊'對象沒有屬性'POST'
- 10. 在中間件中發佈POST請求
- 11. REST PUT方法的推薦/有效請求負載是什麼?
- 12. ReactJS - 有沒有推薦的遊戲方式(2D Tile Maps)
- 13. 是否有可能在Flask中發佈POST請求?
- 14. 如何發送沒有請求數據的cURL POST在PHP中?
- 15. 如何在ios中發送沒有正文的json POST請求?
- 16. 在android中沒有得到http post請求的響應正文?
- 17. 發佈請求沒有迴應
- 18. 發送POST請求的Android
- 19. 有沒有快速的方式發送帖子請求?
- 20. 使用沒有urlencode參數的Httppost的Android發佈請求
- 21. 如何使沒有請求正文的OKHTTP發佈請求?
- 22. HTTP POST請求沒有工作在3G
- 23. 排氣沒有發送帶有參數的發佈請求。
- 24. Android方法不推薦使用AsyncHttpClient的onSuccess()方法。有沒有替代方案?
- 25. 有沒有人有發佈JSON請求的更新示例?
- 26. 在Android中發送JSON POST請求
- 27. 發佈請求有效,但Put請求沒有發送角度2
- 28. 控制發出HTTP請求的推薦方法?
- 29. 將HTTP請求重定向到HTTPS的推薦方式
- 30. 以Java或其他方式發送GET和POST請求而沒有響應
您可以使用Volley或UrlConnection管理器 – Madhu