Android访问WEBAPI,传递json
public static String sendHttpRequestByPost(final String address,String encode,MatterReflectDTO dto){
String result = "";
DefaultHttpClient defaultHttpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(address);
String jsonString = dto.toString();
try {
StringEntity stringEntity = new StringEntity(new GsonBuilder().create().toJson(dto));
System.out.println(stringEntity.toString());
httpPost.setEntity(stringEntity);
httpPost.setHeader("Accept", "application/json");
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Accept-Encoding", "gzip");
HttpResponse httpResponse = defaultHttpClient.execute(httpPost);
System.out.println("请求结果:"+httpResponse.getStatusLine().getStatusCode());
int code = httpResponse.getStatusLine().getStatusCode();
if(code ==200){
InputStream is = httpResponse.getEntity().getContent();
result = "true";
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
服务器端使用的是.net暂时就不贴出来了,需要注意的是并不是通过参数传递的。
更多相关文章
- Android拨打电话 安卓拨打电话权限
- 【Android网络编程】获取网络图片,具有缓存功能
- HTTP 工具类 封装 For android
- Android(安卓)mvp 架构的自述
- 解析Android的 消息传递机制Handler
- 屏蔽电信流氓广告造成的诡异的问题--Android(安卓)WebView 长时
- Android(安卓)mvp 架构的自述
- flutter插件开发学习之旅(7)-------双平台参数传递
- android中Intent传值与Bundle传值详解