就发送自定义的一个字符串s,求一个简单的例子,GET请求方式向服务器发送数据,并接收返回的数据
public class HttpUtil {
private static final int CONNECT_SUCESS = 200;
public static void sendHttpRequest(final String address,
final HttpCallBackListener listener) {
new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection connection = null;
try {
URL url = new URL(address);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(3000);
connection.setReadTimeout(3000);
connection.setDoOutput(true);
connection.setDoInput(true);
StringBuilder response = new StringBuilder();
int code = connection.getResponseCode();
if (CONNECT_SUCESS == code) {
InputStream in = connection.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(in));
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
}
}
if (listener != null) {
listener.onFinish(response.toString());
}
} catch (Exception e) {
if (listener != null) {
listener.onError(e);
}
} finally {
if (connection != null) {
connection.disconnect();
}
}
}
}).start();
}
public interface HttpCallBackListener {
void onFinish(String response);
void onError(Exception e);
}
}
先别问这么大的问题,你先去把如何写一个web端的服务,客户端如何请求http,搞明白,客户端可以使用volley框架,服务端可能涉及到strust,spring等技术
http://www.cnblogs.com/mengdd/p/3142442.html
http://blog.csdn.net/liuhe688/article/details/6425225
网络建议用OKHttp或者Volley实现