android以application/json流的方式提交数据
2015-12-01 11:02
423 查看
网上已经很多post提交数据的文章了, 但以application/json流的方式提交, 却很少提及, 在GOOGLE大神的指点下, 找到
把上面的这个方法放到一个util下, 直接用就好了, jsoncontent是一个JSON字符串, 我是以:
JSONObject postdata = new JSONObject();
postdata.put("name","abc");
String jsoncontent = postdata.toString();
加进去的.
@SuppressWarnings("deprecation") public static JSONObject postData(String jsoncontent,String urlstr) { JSONObject jsonobj = null; try { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(urlstr); //添加http头信息 , 主要是application/json声明 httppost.addHeader("Content-Type", "application/json"); //StringEntity就是以字符串输出到流 HttpEntity he = new StringEntity(jsoncontent,HTTP.UTF_8); httppost.setEntity(he); //如果是以参数传, 就是这个 // List<NameValuePair> params = new ArrayList<NameValuePair>(); // params.add(new BasicNameValuePair("paramname", "post data")); // httppost.setEntity(new UrlEncodedFormEntity(param, HTTP.UTF_8)); HttpResponse response; response = httpclient.execute(httppost); //检验状态码200表示成功 int code = response.getStatusLine().getStatusCode(); if (code == 200) { String returnjson = EntityUtils.toString(response.getEntity());//返回json格式 jsonobj = new JSONObject(returnjson); } } catch (ClientProtocolException e) { } catch (IOException e) { } catch (Exception e) { }
把上面的这个方法放到一个util下, 直接用就好了, jsoncontent是一个JSON字符串, 我是以:
JSONObject postdata = new JSONObject();
postdata.put("name","abc");
String jsoncontent = postdata.toString();
加进去的.
相关文章推荐
- android应用启动流程
- Android studio 如何打jar包
- android:persistent
- android 屏幕单击位置测试
- Android调用系统裁剪图片模糊问题
- Android中大小两种字体对齐 -- 实现上或者下对齐的解决方法
- android用户头像截取
- Android中ListView的简单使用
- android 拍照预览
- Android基础-EditText键盘的显示与隐藏
- Android DiskLruCache 摘录
- android下的事件分发
- android gradle compile(一)
- Android的Canvas、Bitmap、Drawable和Paint
- android 设置系统屏幕亮度
- MTK android Driver:UART介绍
- Android-DialogFragment对话框之登录
- GitHub上史上最全的Android开源项目分类汇总
- android 加载等待Dialog
- Android之checkbox使用