-1
我試圖發送這個HTTP服務器,但我得到一個異常。這是我在asynctask中使用的代碼,我覺得我已經錯過了爲什麼它會給出例外,因爲當我嘗試我的請求時,我在小提琴手中獲得成功。如果有人可以發現錯誤,請幫助我。任何幫助將不勝感激。當發送json對象到HTTP服務器時出現錯誤的請求異常android
這是我的要求,
{"CartItemEntity":[{"MainMenuCode":"CHBA","SubMenuCode":"AL","Quantity":1,"Price":1560}],"DiscountCode":"0","PayCode":"0","OrderID":"6592","OutletCode":"AT","RequiredDate":"15/6/2015","Total":"1638.0","CardNo":"0","PhoneNo":1234}
這是的AsyncTask類
public class GetAllDiscountsAsyncTask extends
AsyncTask<JSONObject, Integer, JSONArray> {
private OnTaskCompleted listener;
private JSONArray responseJson = null;
private Context contxt;
public GetAllDiscountsAsyncTask(Context context, OnTaskCompleted listener) {
// API = apiURL;
this.contxt = context;
this.listener = listener;
}
JSONObject jData;
@Override
protected JSONArray doInBackground(JSONObject... jData) {
String parameterPass = jData.toString();
Log.i("doInBackground", parameterPass);
String path = null;
String response = null;
DefaultHttpClient httpClient = null;
ResponseHandler<String> responseHandler = null;
try {
path = "xxxxxxxxxxxxxxxxxxxxxxx";
new URL(path);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(path);
httppost.setEntity(new StringEntity(jData.toString(), "UTF-8"));
httppost.setHeader("Content-type", "application/json");
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) {
super.onPostExecute(result);
listener.onTaskCompleted(responseJson);
}
}
這是例外,我得到的,
您確定它是JSONArray而不是JSONObject嗎? –
@Sachin Gadagi我已經通過添加我的請求更新了我的問題,這是一個json對象嗎? –
我想你應該看到從fiddler發出請求時發送的確切請求,並且看看在java端是否一切都一樣。 –