它取决于您的服务器端如何实现 通常发送数据需要 HTTPPost 方法或 HTTPPut 方法。更常用的是具有标头数据和正文数据的 HTTPPost 方法
您需要按以下方式进行
1-将您的 json 对象转换为字符串
user.toString();
2-添加目标网址
String URL="Enter URL here";
3-将url添加到请求中
response = dohttpPostWithCode(URL.toString(),user.toString());
response 是 String [ ],它有 2 个索引 i- 用于响应代码 ii- 用于数据
4-处理数据的方法
public String[] dohttpPostWithCode(String url,String postParameters ) throws Exception {
URL weburl = new URL(url);
URI uri = new URI(weburl.getProtocol(), weburl.getUserInfo(), weburl.getHost(), weburl.getPort(), weburl.getPath(), weburl.getQuery(), weburl.getRef());
BufferedReader in = null;
String[] result = new String[2];
try {
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 20000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 20000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
HttpURLConnection httpURL=(HttpURLConnection) weburl.openConnection();
httpURL.setDoOutput(true);
httpURL.setDoInput(true);
httpURL.setRequestMethod("POST");
HttpClient client =new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(uri);
//httpPost.addHeader("language","en");
httpPost.addHeader("Content-Type", "application/json");
// StringEntity entity = new StringEntity(postParameters, HTTP.UTF_8);
httpPost.setEntity(new StringEntity(postParameters));
// httpPost.setEntity(entity);
// httpPost.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = client.execute(httpPost);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String line = "";
String NL = System.getProperty("line.separator");
while ((line = in.readLine()) != null) {
sb.append(line + NL);
}
in.close();
/* String result = sb.toString();
return result;*/
result[0] = response.getStatusLine().getStatusCode()+"";
result[1] = sb.toString();
return result;
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
现在它完成了