在将json对象发送到android中的HTTP服务器时导致错误的请求异常

时间:2015-06-15 09:15:18

标签: android json android-asynctask

我正在尝试发送此HTTP服务器但是我得到了一个例外。这是我在asynctask中使用的代码,我觉得我已经错过了为什么它会给出异常的原因,因为当我尝试我的请求时,我在fiddler中取得了成功。如果有人能发现错误,请帮助我。任何帮助将受到高度赞赏。

这是我的要求,

{"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);
    }

}

这是我得到的例外,

enter image description here

1 个答案:

答案 0 :(得分:0)

您必须将响应转换为String,然后从中生成JSON对象