Получить исключение при вызове асинхронной задачи и перейти по URL-адресу для ответа API в Android

Я сделал одно действие в том, что я называю AsyncTask,В этом AsyncTask Я сделал запрос по URL. Но это дает мне ошибку, что недопустимое исключение аргумента, и не может получить ответ. Мой код, как показано ниже: main.java

public class TrustedBuyingDetailsActivity extends Activity implements OnClickListener {
    TextView buyerName;
    TextView productName;
    TextView quantity;
    TextView ammount;
    TextView total;
    Button submit;
    Button cancel;
    private ProgressDialog pDialog;
    String placeOrderurl;
    String suff_blnc;

    // rAW.....
    String URL = "http://tp.epagestore.in/app_api/order.php?key=251113Zy103013&order_id=154&name=&total_amount=800&shipping_address=&shipping_city=&shipping_country=&product[0][quantity]=1&product[0][product_id]=236&product[0][product_desc]=re&product[0][total_amount]=200.0000&product[0][complete_order_time]=2&product[0][payee_key]=251113bm111519&customer_id=16";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_trusted_product_detail);

        buyerName = (TextView) findViewById(R.id.tv_buyer_name);
        productName = (TextView) findViewById(R.id.tv_pro_name);
        quantity = (TextView) findViewById(R.id.tv_qty);
        ammount = (TextView) findViewById(R.id.tv_amt);
        total = (TextView) findViewById(R.id.tv_total);
        submit = (Button) findViewById(R.id.btn_submit);
        cancel = (Button) findViewById(R.id.btn_cancle);

        buyerName.setText(Pref.getValue(TrustedBuyingDetailsActivity.this, Const.PREF_TRUST_CUSTOMER_NAME, ""));
        System.out.println("::::::::::::::;email::::::;" + Pref.getValue(TrustedBuyingDetailsActivity.this, Const.PREF_TRUST_CUSTOMER_NAME, ""));
        submit.setOnClickListener(this);
        cancel.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btn_submit:
            new GetTrustedPlaceOrder().execute();
            System.out.println("::::::::::::PLACE ORDER API CALLED::::::::::");
            break;

        case R.id.btn_cancle:

            break;

        }
    }

    // GETTING ORDER ID AND PALCE ORDER CONFIRAMATION CALL.....FOR WEBvIEW OR
    // WHAT...........!!!
    private class GetTrustedPlaceOrder extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog

            pDialog = new ProgressDialog(TrustedBuyingDetailsActivity.this);
            pDialog.setCancelable(false);
            pDialog.show();
            System.out.println("==========inside preexecute===================");

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            BackendAPIService sh = new BackendAPIService();
            String key = Pref.getValue(TrustedBuyingDetailsActivity.this, Const.PREF_TRUST_KEY, "");
            String or_id = Pref.getValue(TrustedBuyingDetailsActivity.this, Const.PREF_ORDER_ID, "");
            String total = Pref.getValue(TrustedBuyingDetailsActivity.this, Const.PREF_TOTAL, "");
            String qty = Pref.getValue(TrustedBuyingDetailsActivity.this, Const.PREF_QTY, "");
            String pro_id = Pref.getValue(TrustedBuyingDetailsActivity.this, Const.PREF_PRODUCT_ID, "");
            String cust_id = Pref.getValue(TrustedBuyingDetailsActivity.this, Const.PREF_TRUST_CUSTOMER_ID, "");
            String payeeKey = Pref.getValue(TrustedBuyingDetailsActivity.this, Const.PREF_PAYEE_KEY, "");
            String complteOrderTime = Pref.getValue(TrustedBuyingDetailsActivity.this, Const.PREF_COMPLETE_ORDER_TIME, "");
            String desc = Pref.getValue(TrustedBuyingDetailsActivity.this, Const.PREF_PRO_DESC, "");
            String name = Pref.getValue(TrustedBuyingDetailsActivity.this, Const.PREF_PRO_NAME, "");
            placeOrderurl = "http://tp.epagestore.in/app_api/order.php?key="+ "&order_id=" + or_id + "&name=" + name + "&total_amount=" + total
                    + "&shipping_address=&shipping_city=&shipping_country=&product[0][quantity]=4&product[0][product_id]=" + pro_id + "&product[0][product_desc]=" + desc
                    + "&product[0][total_amount]=" + total + "&product[0][complete_order_time]=" + complteOrderTime + "&product[0][payee_key]=" + payeeKey + "&customer_id=" + cust_id;
            System.out.println(":::::::::::::::::::MY TRUSTED PAYER PLACE ORDER URL:::::::::;;" + placeOrderurl);
            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(placeOrderurl, BackendAPIService.GET);

            Log.d("Response: ", "> " + jsonStr);
            System.out.println("=============MY RESPONSE FOR PLACEORDER>>==========" + jsonStr);

            if (jsonStr != null) {
                try {

                    JSONObject jsonObj = new JSONObject(jsonStr);
                    if (jsonObj.has(Const.TAG_RESULT)) {
                        suff_blnc = jsonObj.getString(Const.TAG_SUFF_BALANCE);
                        System.out.println(":::::::::::::MY SUFF BALANCE::::::::::;;;" + suff_blnc);

                    }

                }

                catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);

            // Dismiss the progress dialog
            if (pDialog.isShowing())
                pDialog.dismiss();

        }
    }

}

Logcat

04-16 09:16:57.932: I/System.out(5340): ::::::::::::PLACE ORDER API CALLED::::::::::
04-16 09:16:58.032: I/System.out(5340): :::::::::::::::::::MY TRUSTED PAYER PLACE ORDER URL:::::::::;;http://tp.epagestore.in/app_api/order.php?key=&order_id=1185&name=Mouse&total_amount=4&shipping_address=&shipping_city=&shipping_country=&product[0][quantity]=4&product[0][product_id]=1&product[0][product_desc]=optical mouse&product[0][total_amount]=4&product[0][complete_order_time]=&product[0][payee_key]=261113zc030425&customer_id=34
04-16 09:16:58.212: I/Choreographer(5340): Skipped 66 frames!  The application may be doing too much work on its main thread.
04-16 09:16:58.532: W/dalvikvm(5340): threadid=12: thread exiting with uncaught exception (group=0xb1aa5ba8)
04-16 09:16:58.582: E/AndroidRuntime(5340): FATAL EXCEPTION: AsyncTask #2
04-16 09:16:58.582: E/AndroidRuntime(5340): Process: com.example.yehki, PID: 5340
04-16 09:16:58.582: E/AndroidRuntime(5340): java.lang.RuntimeException: An error occured while executing doInBackground()
04-16 09:16:58.582: E/AndroidRuntime(5340):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-16 09:16:58.582: E/AndroidRuntime(5340):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-16 09:16:58.582: E/AndroidRuntime(5340):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-16 09:16:58.582: E/AndroidRuntime(5340):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-16 09:16:58.582: E/AndroidRuntime(5340):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-16 09:16:58.582: E/AndroidRuntime(5340):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-16 09:16:58.582: E/AndroidRuntime(5340):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-16 09:16:58.582: E/AndroidRuntime(5340):     at java.lang.Thread.run(Thread.java:841)
04-16 09:16:58.582: E/AndroidRuntime(5340): Caused by: java.lang.IllegalArgumentException: Illegal character in query at index 218: http://tp.epagestore.in/app_api/order.php?key=&order_id=1185&name=Mouse&total_amount=4&shipping_address=&shipping_city=&shipping_country=&product[0][quantity]=4&product[0][product_id]=1&product[0][product_desc]=optical mouse&product[0][total_amount]=4&product[0][complete_order_time]=&product[0][payee_key]=261113zc030425&customer_id=34
04-16 09:16:58.582: E/AndroidRuntime(5340):     at java.net.URI.create(URI.java:727)
04-16 09:16:58.582: E/AndroidRuntime(5340):     at org.apache.http.client.methods.HttpGet.<init>(HttpGet.java:75)
04-16 09:16:58.582: E/AndroidRuntime(5340):     at com.epe.yehki.backend.BackendAPIService.makeServiceCall(BackendAPIService.java:92)
04-16 09:16:58.582: E/AndroidRuntime(5340):     at com.epe.yehki.backend.BackendAPIService.makeServiceCall(BackendAPIService.java:57)
04-16 09:16:58.582: E/AndroidRuntime(5340):     at com.epe.yehki.ui.TrustedBuyingDetailsActivity$GetTrustedPlaceOrder.doInBackground(TrustedBuyingDetailsActivity.java:109)
04-16 09:16:58.582: E/AndroidRuntime(5340):     at com.epe.yehki.ui.TrustedBuyingDetailsActivity$GetTrustedPlaceOrder.doInBackground(TrustedBuyingDetailsActivity.java:1)
04-16 09:16:58.582: E/AndroidRuntime(5340):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-16 09:16:58.582: E/AndroidRuntime(5340):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
04-16 09:16:58.582: E/AndroidRuntime(5340):     ... 4 more

2 ответа

Вы должны закодировать свой URI string,

Не принимает spaces вместо этого замените его %20,

Вы должны использовать что-то вроде этого, чтобы избежать этой ошибки:

String query = URLEncoder.encode("key="+ "&order_id=" + or_id + "&name=" + name + "&total_amount=" + total
                + "&shipping_address=&shipping_city=&shipping_country=&product[0][quantity]=4&product[0][product_id]=" + pro_id + "&product[0][product_desc]=" + desc
                + "&product[0][total_amount]=" + total + "&product[0][complete_order_time]=" + complteOrderTime + "&product[0][payee_key]=" + payeeKey + "&customer_id=" + cust_id, "utf-8");
String url = "http://tp.epagestore.in/app_api/order.php?" + query;
Другие вопросы по тегам