开发者社区> 问答> 正文

android post json 数据失败 急求啊!!!!!

用eclipse java写了一个post方法可以post成功,但是用android 用同样的方法上传失败,搞了两三天了,急求啊!!!!!!post 方法public static void appadd(String weburl,String json) {

  try {
        //鍒涘缓杩炴帴
        URL url = new URL(weburl);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoOutput(true);
        connection.setDoInput(true);
        connection.setRequestMethod("POST");
        connection.setUseCaches(false);
        connection.setInstanceFollowRedirects(true);
        connection.setRequestProperty("Content-Type",
                "application/json");
        connection.connect();
        //POST璇锋眰
        DataOutputStream out = new DataOutputStream(
                connection.getOutputStream());
        out.writeBytes(json);
        out.flush();
        out.close();

        //璇诲彇鍝嶅簲
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                connection.getInputStream()));
        String lines;
        StringBuffer sb = new StringBuffer("");
        while ((lines = reader.readLine()) != null) {
            lines = new String(lines.getBytes(), "utf-8");
            sb.append(lines);
        }
        System.out.println("result---------->"+sb.toString());
        reader.close();
        connection.disconnect();
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

    客户端返回的错误提示:result---------->与“string.this[int]”最匹配的重载方法具有一些无效参数

    .net服务端接受数据解析的代码

    try
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://ss.yn15.com:8800/Interface/EditRFID");//第一个接口地址
           // HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://ss.yn15.com:8800/api/InterfaceRFID/Edit");//第二个接口地址

            request.Method = "POST";
            //request.ContentType = "application/x-www-form-urlencoded";//第一个接口使用
            request.ContentType = "application/json";// application/json 第一个、第二个接口都可使用使用

            byte[] buffer = Encoding.UTF8.GetBytes("{\"list\": {\"updated\": [{\"ApprovePerson\": \"\",\"ApproveState\": \"\",\"FZStatus\": \"178\", \"LeaveType\": \"209\",\"ProjectCode\": \"201306030001\",\"RFIDPerson\": \"\", \"RFID_id\": \"S0019\",\"SheepStatus\": \"134\",\"bas_pen_id\": \"000040\",\"birthday\": \"2015-09-29\",\"breed\": \"285\",\"breed_count\": \"2\",\"chip\": \"982123472852295\",\"createtime\": \"2015-08-26\",\"female_RFID\": \"\",\"grade\": \"\",\"introduce_source\": \"118\",\"introduce_time\": \"2015-9-27\",\"introduce_weight\": 3.76,\"isdead\": \"\",\"isenable\": true,\"isout\": \"\",\"isweekout\": \"\",\"male_RFID\": \"E47353\",\"mother_breednum\": \"0\",\"note\": \"\",\"purpose\": \"122\",\"changeStatus\": \"modify\",\"rfidStatus\": \"\",\"sex\": \"3334\",\"weaningdate\": \"2015-08-27\",\"weaningday\": 0,\"weaningweight\": 1.0 }],\"inserted\": [],\"deleted\": [],\"_changed\": true}}");


            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                string result = reader.ReadToEnd();
                string asb;
                // XmlDocument xx = new XmlDocument();
                //  xx.LoadXml(result);
                //return xx;
            }
        }
        catch (Exception ex)
        {
            ex = new Exception("postXMLByResponseError:" + ex.Message);
            throw ex;
        }

展开
收起
杨冬芳 2016-07-06 14:09:34 2343 0
1 条回答
写回答
取消 提交回答
  • IT从业

    android里面做数据操作是要异步的。建议你用相关的sdk,进行快速的解析和重构

    2019-07-17 19:52:03
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载