我正在使用HttpURLConnection
类将Post请求发送到我的服务器但是它无法正常工作,我使用DefaultHttpClient
&它的工作正常,但在android org.apache.http.impl.client.DefaultHttpClient
中已被弃用,这就是我使用HttpURLConnection
类的原因。我不知道发送JSON
对象或其他东西的方式有什么不妥。
以下是我的代码实现
URL url = new URL(linkPath);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(10000);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
OutputStream out = new BufferedOutputStream(urlConnection.getOutputStream());
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out, "UTF-8"));
writer.write(URLEncoder.encode(jsonObject.toString(), "UTF-8"));
writer.close();
out.close();
urlConnection.disconnect();