HttpPost参数不接受10位数字作为字符串

时间:2014-07-23 07:43:56

标签: android http-post

我尝试将数据发布到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)"}

1 个答案:

答案 0 :(得分: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