问题描述
- android post json 数据失败 急求啊!!!!! 5C
- 用eclipse java写了一个post方法可以post成功,但是用android 用同样的方法上传失败,搞了两三天了,急求啊!!!!!!
post 方法
public static void appadd(String weburlString 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; }
解决方案
android Json数据分装Post请求
php 获取 POST JSON 数据
android 用json格式post数据
解决方案二:
android里面做数据操作是要异步的。
解决方案三:
建议你用相关的sdk,进行快速的解析和重构
时间: 2024-11-03 11:56:48