我已经阅读了上面的答案并创建了一个实用类来简化HTTP请求。希望它能对你有所帮助。
方法调用
HashMap<String, String> params = new HashMap<String, String>();
params.put("email","me@example.com");
params.put("password","12345");
String url = "http://www.example.com";
HttpUtility.newRequest(url,HttpUtility.METHOD_POST,params, new HttpUtility.Callback() {
@Override
public void OnSuccess(String response) {
System.out.println("Server OnSuccess response="+response);
}
@Override
public void OnError(int status_code, String message) {
System.out.println("Server OnError status_code="+status_code+" message="+message);
}
});
实用类
import java.io.*;
import java.net.*;
import java.util.HashMap;
import java.util.Map;
import static java.net.HttpURLConnection.HTTP_OK;
public class HttpUtility {
public static final int METHOD_GET = 0;
public static final int METHOD_POST = 1;
public interface Callback {
public void OnSuccess(String response);
public void OnError(int status_code, String message);
}
public static void newRequest(String web_url, int method, HashMap < String, String > params, Callback callback) {
new Thread(new Runnable() {
@Override
public void run() {
try {
String url = web_url;
if (method == METHOD_GET && params != null) {
for (Map.Entry < String, String > item: params.entrySet()) {
String key = URLEncoder.encode(item.getKey(), "UTF-8");
String value = URLEncoder.encode(item.getValue(), "UTF-8");
if (!url.contains("?")) {
url += "?" + key + "=" + value;
} else {
url += "&" + key + "=" + value;
}
}
}
HttpURLConnection urlConnection = (HttpURLConnection) new URL(url).openConnection();
urlConnection.setUseCaches(false);
urlConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
urlConnection.setRequestProperty("charset", "utf-8");
if (method == METHOD_GET) {
urlConnection.setRequestMethod("GET");
} else if (method == METHOD_POST) {
urlConnection.setDoOutput(true);
urlConnection.setRequestMethod("POST");
}
if (method == METHOD_POST && params != null) {
StringBuilder postData = new StringBuilder();
for (Map.Entry < String, String > item: params.entrySet()) {
if (postData.length() != 0) postData.append('&');
postData.append(URLEncoder.encode(item.getKey(), "UTF-8"));
postData.append('=');
postData.append(URLEncoder.encode(String.valueOf(item.getValue()), "UTF-8"));
}
byte[] postDataBytes = postData.toString().getBytes("UTF-8");
urlConnection.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
urlConnection.getOutputStream().write(postDataBytes);
}
int responseCode = urlConnection.getResponseCode();
if (responseCode == HTTP_OK && callback != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
callback.OnSuccess(response.toString());
reader.close();
} else if (callback != null) {
callback.OnError(responseCode, urlConnection.getResponseMessage());
}
urlConnection.disconnect();
} catch (IOException e) {
e.printStackTrace();
if (callback != null) {
callback.OnError(500, e.getLocalizedMessage());
}
}
}
}).start();
}
}
http://example.com/index.php
) - dacwe