【问题标题】:HttpPost parameter doesnt accept 10 digits number as StringHttpPost 参数不接受 10 位数字作为字符串
【发布时间】:2014-09-14 06:56:42
【问题描述】:

我尝试将数据发布到 Restful 服务,但执行此操作时出错; restful 服务方法参数是

{
  "name": "sample string 1",
  "surname": "sample string 2",      
  "gsm": "sample string 4",
  "groups": [
    "sample string 1",
    "sample string 2",
    "sample string 3"
  ]
}

我只是像这样添加我的 HttpPost 参数

List<String> SelectedId = new ArrayList<String>();
SelectedId.add("8050");
SelectedId.add("7910");
String phoneNumber = txt_gsm.getText().toString().trim();
String personName = txt_name.getText().toString().trim();
String personSurname = txt_surname.getText().toString().trim();
namevaluepairs.add(new BasicNameValuePair("ad", personName));
namevaluepairs.add(new BasicNameValuePair("soyad", personSurname));
namevaluepairs.add(new BasicNameValuePair("gsm", phoneNumber));
namevaluepairs.add(new BasicNameValuePair("groups",SelectedId.toString()));
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Url + MethodName);
        httppost.setHeader("authToken", Token);
        httppost.setEntity(new UrlEncodedFormEntity(namevaluepairs,"UTF-8"));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        InputStream is = entity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(
                is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        result = sb.toString();
        return result;

但该方法返回该错误

{"message":"An error has occurred.","exceptionMessage":"Input string was not in a correct format.","exceptionType":"System.FormatException","stackTrace":"   at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)\r\n   at System.Number.ParseInt32(String s, NumberStyles style, NumberFormatInfo info)\r\n   at System.Int32.Parse(String s)\r\n   at artimesajMobil.Data.Repository.Persons.PersonRepository.NewPerson(Guid userID, tbl_AltMusteriler person, List`1 grps)\r\n   at artimesajMobil.Business.Services.PersonService.NewPerson(Guid userID, RehberKisi person)\r\n   at artimesajMobil.RestApi.Controllers.PersonController.AddContact(RehberKisi yeniMusteriler)\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass13.<GetExecutor>b__c(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.<>c__DisplayClass5.<ExecuteAsync>b__4()\r\n   at System.Threading.Tasks.TaskHelpers.RunSynchronously[TResult](Func`1 func, CancellationToken cancellationToken)"}

【问题讨论】:

    标签: android http-post


    【解决方案1】:
    final List<String> SelectedId = new ArrayList<String>();
    SelectedId.add("8050");
    SelectedId.add("7910");
    
    final String[] strarray = SelectedId.toArray(SelectedId.size());
    
    namevaluepairs.add(new BasicNameValuePair("groups", Arrays.toString(strarray)));
    

    Reference

    【讨论】:

    • 感谢您的回复,但我仍然遇到同样的错误。但现在我知道我在发布群组数据时遇到问题..
    • 你知道我尝试手动添加参数 namevaluepairs.add(new BasicNameValuePair("groups","8050"));然后效果很好,但是当尝试编写多个 namevaluepairs.add(new BasicNameValuePair("groups","8050,7910"));在我写之前返回相同的错误
    • @cooolstaff 询问后端开发人员,支持哪些格式。只用一个SelectedId 试试我的答案,如果你能解决,请告诉我。
    • 我刚刚添加了类似 namevaluepairs.add(new BasicNameValuePair("groups[]", "8050")); namevaluepairs.add(new BasicNameValuePair("groups[]", "7910"));并且工作得很好非常感谢@VenomVendor 的帮助
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-18
    • 2017-08-11
    • 1970-01-01
    • 2013-05-05
    • 1970-01-01
    • 2017-05-23
    • 1970-01-01
    相关资源
    最近更新 更多