Android - 使用HTTP通訊的範例
由於下列範例是網路上的資源,已經忘記出處,以下作為分享使用。
放在Android端,連線都透過此class執行
public class HTTPconnect {
public String sendPostRequest(String requestURL, HashMap<String, String> postDataParams)
{
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();/**設定connection物件**/
/**HttpURLConnection是基於HTTP協定的,其底層通過socket通信實現。如果不設置超時(timeout),在網路異常的情況下,可能會導致程式卡住而不繼續往下執行。**/
conn.setReadTimeout(15000);/**單位:毫秒**/
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");/**選用傳遞方法 >> POST (這樣表單資料傳送過程中不會被明文顯示)**/
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK)
{
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
response = br.readLine();
}
else
{
response="Error Create";
}
}
catch (Exception e)
{
e.printStackTrace();
}
return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet())
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
return result.toString();
}
}
留言
張貼留言