0
A
回答
2
私有類LoginTask延伸的AsyncTask { ProgressDialog pdLoading =新ProgressDialog(LoginActivity.this);
@Override
protected void onPreExecute() {
super.onPreExecute();
pdLoading.setMessage("Logging In ...");
pdLoading.show();
}
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL(Constants.SERVER_URL);
HashMap<String, String> parameters = new HashMap<String, String>();
parameters.put("method", "login");
parameters.put("email", "[email protected]");
parameters.put("password", "customer123");
parameters.put("device_token", "123456");
parameters.put("device_type", "android");
parameters.put("timezone", "Asia/Kolkata");
Set set = parameters.entrySet();
Iterator i = set.iterator();
StringBuilder postData = new StringBuilder();
for (Map.Entry<String, String> param : parameters.entrySet()) {
if (postData.length() != 0) {
postData.append('&');
}
postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
System.setProperty("http.keepAlive", "false");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
conn.setUseCaches(false);
conn.setConnectTimeout(30000);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.getOutputStream().write(postDataBytes);
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
StringBuilder builder = new StringBuilder();
for (String line = null; (line = reader.readLine()) != null;) {
builder.append(line).append("\n");
}
reader.close();
conn.disconnect();
Log.e(TAG, builder.toString());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pdLoading.dismiss();
}
}
+0
這意味着這是答案張貼問題? –
相關問題
- 1. 無法在Android中使用HttpURLConnection發送POST請求
- 2. HttpURLConnection握手和請求發送
- 3. POST請求發送json數據java HttpUrlConnection
- 4. 如何利用HttpURLConnection類發送POST請求使用JSON數據
- 5. 如何使用HttpURLConnection發出oauth2請求
- 6. 使用HttpURLConnection的Android POST請求
- 7. 使用HttpUrlConnection向服務器發送請求
- 8. 如何在java中使用HttpURLConnection發送多部分POST請求?
- 9. 使用Java發送帶有令牌的GET請求HttpUrlConnection
- 10. 使用HttpURLConnection發送包含PUT請求的文件
- 11. 如何使用HttpUrlConnection發送POST請求中的requestparameter
- 12. 從Android的發送JSON對象HttpURLConnection的POST請求
- 13. android HttpUrlConnection發送帖子和參數獲取請求
- 14. Android 4.4.2 Kitkat無法通過HttpURLConnection發送請求
- 15. 使用iOS發送請求 - 使用304 HTTPCode發送請求
- 16. 使用HttpURLConnection從Android發送HTTP POST
- 17. 使用HTTPUrlConnection在URL中發送值Android
- 18. Android HttpGet請求不發送
- 19. 發送POST請求的Android
- 20. Android:發送服務請求
- 21. 從Android發送JSON請求
- 22. Facebook發送請求不發送請求
- 23. 用Android發送HTTP POST請求
- 24. HttpURLConnection:發送10,000個請求給出「連接重置」
- 25. 如何在HTTP中發送PUT,DELETE HTTP請求中的HttpURLConnection
- 26. Java:在發送之前顯示HttpURLConnection的請求
- 27. 在HttpURLConnection中發送PUT,刪除HTTP請求
- 28. HttpURLConnection的POST請求 - 不能將數據發送到服務器
- 29. 爲什麼我需要getInputStream爲HttpUrlConnection發送請求?
- 30. 得到400響應httpurlconnection發送post請求
google +您的問題+我覺得很幸運。 – njzk2
我投票結束這個問題作爲題外話,因爲這不是谷歌 – njzk2