Android利用HttpClient发送Json到后台

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/u012931864/article/details/50937347
                HttpPost httppost = new HttpPost(params[0]);

                httppost.addHeader("Content-Type", "application/json;charset=utf-8");

                httppost.setEntity(new StringEntity(str,"utf-8"));

                HttpResponse response;

                response = MyApplication.getInstance().getHttpclient().execute(httppost);

                //检验状态码,如果成功接收数据
                int code = response.getStatusLine().getStatusCode();

                if (code == 200) {
                    HttpEntity entity = response.getEntity();

                    InputStream is = entity.getContent();

                    BufferedReader reader = new BufferedReader(new InputStreamReader(is));

                    String s = reader.readLine();
                    return s;
                }
                else return "error";

            }  catch (Exception e) {
                e.printStackTrace();
            }

            return "error";


展开阅读全文

没有更多推荐了,返回首页