我尝试将json
个对象和少量字符串数据发送到asynctask
以与api
进行通信。我使用以下代码,因为我只能传递String
个值(我从活动传递的所有值都是Strings
)。
我知道如何创建一个对象,我已经创建了它。我想知道如何一次传递JSON
对象和其他字符串。
任何人都可以帮助我。
任何帮助都将受到高度赞赏。
这些是我想传递给asynctask的数据,
活性,
new GetAllDiscountsAsyncTask(getBaseContext(),
new OnTaskCompleted() {
@Override
public void onTaskCompleted(JSONArray responseJson) {
System.out.println("GetAllDiscountsAsyncTask");
Intent i = new Intent(getApplicationContext(),
CardPromotionListActivity.class);
}
}).execute(CartItemEntitys, OrderID, OutletCode,
equiredDate, Total);
Asynctask类
public class GetAllDiscountsAsyncTask extends AsyncTask<String, Integer, JSONArray> {
private OnTaskCompleted listener;
private JSONArray responseJson = null;
private Context contxt;
private Activity activity;
String email;
public GetAllDiscountsAsyncTask(Context context, OnTaskCompleted listener) {
// API = apiURL;
this.contxt = context;
this.listener = listener;
}
// async task to accept string array from context array
@Override
protected JSONArray doInBackground(String... params) {
String path = null;
String response = null;
HashMap<String, String> request = null;
JSONObject requestJson = null;
DefaultHttpClient httpClient = null;
HttpPost httpPost = null;
StringEntity requestString = null;
ResponseHandler<String> responseHandler = null;
Log.i("CardNo", "0");
Log.i("CartItemEntity", params[0]);
Log.i("DiscountCode", "0");
Log.i("OrderID", params[1]);
Log.i("OutletCode", params[2]);
Log.i("PayCode", "0");
Log.i("PhoneNo", "0");
Log.i("RequiredDate", params[3]);
Log.i("Total", params[4]);
try {
path = "http://203.94.69.162:3331/ItemService.svc/GetAllDiscounts";
new URL(path);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
// set the API request
request = new HashMap<String, String>();
request.put(new String("CardNo"), "0");
request.put(new String("CartItemEntity"), params[0]);
request.put(new String("DiscountCode"), "0");
request.put(new String("OrderID"), params[1]);
request.put(new String("OutletCode"), params[2]);
request.put(new String("PayCode"), "0");
request.put(new String("PhoneNo"), "4545");
request.put(new String("RequiredDate"), params[3]);
request.put(new String("Total"), params[4]);
request.entrySet().iterator();
// Store locations in JSON
requestJson = new JSONObject(request);
httpClient = new DefaultHttpClient();
httpPost = new HttpPost(path);
requestString = new StringEntity(requestJson.toString());
// sets the post request as the resulting string
httpPost.setEntity(requestString);
httpPost.setHeader("Content-type", "application/json");
// Handles the response
responseHandler = new BasicResponseHandler();
response = httpClient.execute(httpPost, responseHandler);
responseJson = new JSONArray(response);
System.out.println("*****JARRAY*****" + responseJson.length());
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
responseJson = new JSONArray(response);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return responseJson;
}
@Override
protected void onPostExecute(JSONArray result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
listener.onTaskCompleted(responseJson);
}
}
Json对象,在活动类
中目前我将此转换为String并将其发送到asynctask,但我想在不转换的情况下发送,
JSONObject jsonObj = new JSONObject();
jsonObj.put("MainMenuCode",item.getMainMenuCode());
jsonObj.put("Price", item.getPrice());
jsonObj.put("Quantity", product.getQuantity());
jsonObj.put("SubMenuCode",item.getSubMenuCode());
String a = jsonObj.toString();
CartIddtemEntityList.add(a);
CartItemEntitys = CartIddtemEntityList.toString();
答案 0 :(得分:0)
好的,首先要创建一个新的JSONObject
并让我们称之为jData
:
JSONObject jData = new JSONObject();
然后将您当前的jsonObject
添加到其中:
jData.put("Result",jsonObj);
最后:
HttpPost httppost = new HttpPost("YourUrl");
httppost.setEntity(new StringEntity(jData.toString(), "UTF-8"));