Android how to send int and double as namevaluepair as http post

I have an application in which I am trying to send data to a web service using an http message. User data is a mixture of int and double strings. In the application, everything is presented as strings, since I use AsyncTask to start a network call (so that it is not in the main thread), the params array is of type String.

The problem is that the server sometimes expects an int. for example, compID is the int that the server is expecting. When using http post, I use NameValuePair. This will only accept strings. How can I pass an int or double to an http message?

In my work.

String[] params = new String[]{tagCompany, tagId, tagPerson, OUT, null, null,null, null, null, null}; AsyncPostData apd = new AsyncPostData(); apd.execute(params); private class AsyncPostData extends AsyncTask<String, Void, Void> { ProgressDialog progressDialog; @Override protected void onPreExecute() { progressDialog= ProgressDialog.show(NfcscannerActivity.this, "Connecting to Server"," Posting data...", true); }; @Override protected Void doInBackground(String... params) { nfcscannerapplication.loginWebservice.postData(params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7], params[8], params[9]); return null; } @Override protected void onPostExecute(Void result) { super.onPostExecute(result); if(progressDialog != null) progressDialog.dismiss(); } }//end of AsyncPostData 

.

My sending method

 public void postData( String compID, String tagID, String clientID, String carerID, String phoneScanned, String phoneSent, String TXType, String phoneType, String latitude, String longitude) { // Create a new HttpClient and Post Header HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://cfweb.yourofficeanywhere.co.uk:88/roadrunner.asmx/PostTransaction"); try { // Add your data List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); nameValuePairs.add(new BasicNameValuePair("CompanyID", compID)); nameValuePairs.add(new BasicNameValuePair("TagID", tagID)); nameValuePairs.add(new BasicNameValuePair("ClientID", clientID)); nameValuePairs.add(new BasicNameValuePair("CarerID", carerID)); nameValuePairs.add(new BasicNameValuePair("PhoneScanned", "2010-10-16 16:30 000")); nameValuePairs.add(new BasicNameValuePair("PhoneSent", "2010-10-16 16:32 000")); nameValuePairs.add(new BasicNameValuePair("TXType", "2")); nameValuePairs.add(new BasicNameValuePair("PhoneType", "2")); nameValuePairs.add(new BasicNameValuePair("Latitude", latitude)); nameValuePairs.add(new BasicNameValuePair("Longitude", longitude)); httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); // Execute HTTP Post Request HttpResponse response = httpclient.execute(httppost); Log.e(TAG, "response of post = " + response.toString()); } catch (ClientProtocolException e) { // TODO Auto-generated catch block } catch (IOException e) { // TODO Auto-generated catch block } } 

In my post method, some values ​​are now null. I would like to know how to make compID int in NameValuePair. compID comes from the action as String, but the server expects an int.

+4
source share
3 answers

You can do something like this:

 nameValuePairs.add(new BasicNameValuePair("TXType",Integer.toString (2))); 

TxType is the key, and 2 is the value

+10
source

Just parse your string with a whole series

 int result = Integer.parseInt(compID); 

But you must be sure that your compID can be parsed by Integer. If it contains characters that are not numbers, this operation will fail. To make sure compID is parsable, you can use this

 if(compID.matches("(\\d+)"){ result = Integer.parseInt(compId); } 
0
source

It has been a long time since this question was asked, but I want to help those who land on this page. As in the argument above, you cannot use doubles or longs or ints in namevaluepair, but you can use JSONObject , it can handle these primitives successfully, whereas you have a “Bad request” response with the namevaluepair , I wrote a sample below.

  JSONObject juo = new JSONObject(); juo.put("City", txtCity.getText().toString().trim().toString()); juo.put("ClassNumber", Long.parseLong(txtClassNumber.getText().toString() .trim())); juo.put("EmailAddress", txtEmail.getText().toString().trim() .toString()); juo.put("ID", Long.parseLong(userid)); juo.put("RealName", txtRealName.getText().toString().trim()); juo.put("RealSurname", txtRealSurname.getText().toString() .trim()); juo.put("School", txtSchool.getText().toString().trim()); juo.put("UserId", Long.parseLong(userid)); juo.put("UserName", username); juo.put("UserProfileImage", profileImageUrl); StringEntity entityForPost = new StringEntity(juo.toString()); hpost.setHeader("content-type", "application/json"); // hpost.setEntity(new UrlEncodedFormEntity(UpdateParams)); hpost.setEntity(entityForPost); HttpResponse hres = hclient.execute(hpost); StatusLine status = hres.getStatusLine(); 
0
source

Source: https://habr.com/ru/post/1440253/


All Articles