在Android中使用POST方法后如何获得响应
网址是: 方法类型:post 标题:内容类型:application/json,解码:2 数据:xx 我们如何在安卓系统中实现这一点,我如何从中得到回应?? 我看到Httpclient已被弃用在Android中使用POST方法后如何获得响应,android,androidhttpclient,Android,Androidhttpclient,网址是: 方法类型:post 标题:内容类型:application/json,解码:2 数据:xx 我们如何在安卓系统中实现这一点,我如何从中得到回应?? 我看到Httpclient已被弃用 如果您不想使用库,您可以使用HttpURLConnection,否则您也可以使用Volley进行网络呼叫,因为Volley使管理网络呼叫更容易! 谢谢 URL=新URL(“http://yoururl.com"); HttpsURLConnection conn=(HttpsURLConnection)u
如果您不想使用库,您可以使用HttpURLConnection,否则您也可以使用Volley进行网络呼叫,因为Volley使管理网络呼叫更容易! 谢谢
URL=新URL(“http://yoururl.com");
HttpsURLConnection conn=(HttpsURLConnection)url.openConnection();
连接设置读取超时(10000);
连接设置连接超时(15000);
conn.setRequestMethod(“POST”);
conn.setDoInput(真);
连接设置输出(真);
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“firstParam”,paramValue1));
参数添加(新的BasicNameValuePair(“secondParam”,paramValue2));
参数添加(新的BasicNameValuePair(“第三参数”,参数3));
OutputStream os=conn.getOutputStream();
BufferedWriter=新的BufferedWriter(
新的OutputStreamWriter(操作系统,“UTF-8”);
write(getQuery(params));
writer.flush();
writer.close();
os.close();
连接();
"
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();
}
private String getQuery(列表参数)引发UnsupportedEncodingException
{
StringBuilder结果=新建StringBuilder();
布尔值优先=真;
for(NameValuePair对:params)
{
如果(第一)
第一个=假;
其他的
结果。追加(&);
append(URLEncoder.encode(pair.getName(),“UTF-8”);
结果。追加(“=”);
append(URLEncoder.encode(pair.getValue(),“UTF-8”);
}
返回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();
}
您可以使用改型库,因为改型比其他方法更快、更容易从HTTP请求加载响应。 访问, 例如,这里给出了示例Json对象
{
"title": "foo",
"body": "bar",
"userId": 1,
"id": 101
}
代码:
公共接口服务{
@职位(“/职位”)
@FormUrlEncoded
调用savePost(@Field(“title”)字符串title,
@字段(“主体”)字符串主体,
@字段(“用户ID”)长用户ID);
}
使用截击我能举个例子吗,我是android的新手,不知道如何使用HttpURLConnection进行截击,但我建议你先使用HttpURLConnection,如果我的答案对你有帮助,你可以尝试截击和其他lib,请接受我的答案谢谢,这很有帮助,但我是如何添加内容类型的??,我有一个json objectgetQuery将返回包含json的字符串,然后您可以提取它。如果代码对您有帮助,那么您可以将其标记为回答谢谢我的响应将是一个Json对象,虽然它将以Json字符串给出响应,您可以将其转换为jsonObjectIt表示未处理的异常:java.io.IOException in url.openconnection检查我的编辑我添加了try-catch代码来处理异常
{
"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);
}