在 Android 中使用 POST 方法后如何获得响应
How to get response after using POST method in Android
url 是:http://localhost
方法类型:post
header : Content-Type:application/json, decode:2
数据:xx
我们如何在 android 中实现这一点??,我如何从中得到回应??
我看到 Httpclient 已被弃用
如有任何帮助,我们将不胜感激
如果您不想使用库,可以使用 HttpURLConnection,否则您也可以使用 Volley 进行网络调用,因为 volley 可以更轻松地管理网络调用!
谢谢!
URL url = new URL("http://yoururl.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("firstParam", paramValue1));
params.add(new BasicNameValuePair("secondParam", paramValue2));
params.add(new BasicNameValuePair("thirdParam", paramValue3));
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
conn.connect();
................................................ .....................
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
尝试使用 HttpUrlConnection
String Url, query;
InputStream inputStream;
HttpURLConnection urlConnection;
byte[] outputBytes;
String ResponseData;
Context context;
try{
URL url = new URL(Url);
urlConnection = (HttpURLConnection) url.openConnection();
outputBytes = query.getBytes("UTF-8");
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.connect();
OutputStream os = urlConnection.getOutputStream();
os.write(outputBytes);
os.flush();
os.close();
inputStream = new BufferedInputStream(urlConnection.getInputStream());
ResponseData = convertStreamToString(inputStream);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
public String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
您可以使用 Retrofit 库,因为 Retrofit 比其他库更快更容易地从 HTTP 请求加载响应。
访问,https://square.github.io/retrofit/
例如,这里示例 Json 给定的对象,
{
"title": "foo",
"body": "bar",
"userId": 1,
"id": 101
}
代码:
public interface APIService {
@POST("/posts")
@FormUrlEncoded
Call<Post> savePost(@Field("title") String title,
@Field("body") String body,
@Field("userId") long userId);
}
url 是:http://localhost 方法类型:post header : Content-Type:application/json, decode:2 数据:xx
我们如何在 android 中实现这一点??,我如何从中得到回应?? 我看到 Httpclient 已被弃用
如有任何帮助,我们将不胜感激
如果您不想使用库,可以使用 HttpURLConnection,否则您也可以使用 Volley 进行网络调用,因为 volley 可以更轻松地管理网络调用! 谢谢!
URL url = new URL("http://yoururl.com");
HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("firstParam", paramValue1));
params.add(new BasicNameValuePair("secondParam", paramValue2));
params.add(new BasicNameValuePair("thirdParam", paramValue3));
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(params));
writer.flush();
writer.close();
os.close();
conn.connect();
................................................ .....................
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException
{
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
return result.toString();
}
尝试使用 HttpUrlConnection
String Url, query;
InputStream inputStream;
HttpURLConnection urlConnection;
byte[] outputBytes;
String ResponseData;
Context context;
try{
URL url = new URL(Url);
urlConnection = (HttpURLConnection) url.openConnection();
outputBytes = query.getBytes("UTF-8");
urlConnection.setRequestMethod("POST");
urlConnection.setDoOutput(true);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.connect();
OutputStream os = urlConnection.getOutputStream();
os.write(outputBytes);
os.flush();
os.close();
inputStream = new BufferedInputStream(urlConnection.getInputStream());
ResponseData = convertStreamToString(inputStream);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
public String convertStreamToString(InputStream is) {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = null;
try {
while ((line = reader.readLine()) != null) {
sb.append((line + "\n"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return sb.toString();
}
您可以使用 Retrofit 库,因为 Retrofit 比其他库更快更容易地从 HTTP 请求加载响应。 访问,https://square.github.io/retrofit/
例如,这里示例 Json 给定的对象,
{
"title": "foo",
"body": "bar",
"userId": 1,
"id": 101
}
代码:
public interface APIService {
@POST("/posts")
@FormUrlEncoded
Call<Post> savePost(@Field("title") String title,
@Field("body") String body,
@Field("userId") long userId);
}