它取決於您的服務器端如何實現 正常發送數據需要HTTPPost方法或HTTPPut方法。比較常用的是HTTPPost這有標題,你需要做的是在下面的方式
1-您的JSON對象轉換爲字符串
user.toString();
2-添加有針對性的URL
數據和身體數據
方法
String URL="Enter URL here";
3-添加URL請求
response = dohttpPostWithCode(URL.toString(),user.toString());
響應是字符串[]具有2索引 異用於響應代碼 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();
}
}
}
}
現在它的完成
你需要發送數據的HTTPRequest方法有以下方法GET,POST,PUT,DELETE你如果您需要POST或PUT方法 –