我有一个应用程序,我正在尝试使用http帖子将数据发送到Web服务。用户数据是字符串int和double的混合。在应用程序中,所有都表示为字符串,因为当我使用AsyncTask来运行网络调用时(因此它不在主线程上),params数组的类型为String。
我遇到的问题是服务器有时需要一个int。例如,compID是服务器期望的int。使用http帖子时,我使用NameValuePair。这只会接受字符串。如何将int或double传递给http帖子?
在我的活动中。
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
我的帖子方法
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
}
}
在我的post方法中,一些值现在设置为null。我想知道的是如何在NameValuePair中使compID成为一个int。 compID作为String来自活动,但服务器需要int。
答案 0 :(得分:11)
您可以这样做:
nameValuePairs.add(new BasicNameValuePair("TXType",Integer.toString (2)));
TxType是键,2是值
答案 1 :(得分:0)
将String解析为Integer,就像这样
int result = Integer.parseInt(compID);
但是你必须确保你的compID可以被解析为Integer。如果它包含非数字的符号,则此操作将失败。要确保compID是可解析的,您可以使用此
if(compID.matches("(\\d+)"){
result = Integer.parseInt(compId); }
答案 2 :(得分:0)
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();