在向服务器发送订单时遇到问题

时间:2014-01-22 15:01:20

标签: android

所以我在一些代码上苦苦挣扎,任何帮助都会受到极大的关注,我在发布问题之前尝试自己完成大部分的工作,所以我向你保证我不是在寻找有人为我做的工作我......只是需要帮助才能让它发挥作用。

我有一个简单的购物者应用程序,我试图让它将订单发送到数据库,我检查它是否通过visual studio检查数据库发送。

问题是,我的应用程序有效,但视觉工作室注册中没有任何内容显示它已收到订单。我认为我的代码可能存在问题,而且发送订单有困难。

如果有任何帮助,这就是我的任务要求我为此步骤做的事情:

  

现在我们需要处理Checkout活动,并将订单提交到   服务器。转到CheckoutActivity类并添加一个名为的新方法   checkoutOrder()。在这个方法中,你应该定义一个AsyncTask和   然后执行它。 (注意:可以调用方法getApplication()   直接,你已经在一个活动中,所以你不需要一个   “activity”对象作为参数)。

以下是我在结账活动中的代码:

package uk.ac.uk.st265.shopper;

    import java.io.ByteArrayOutputStream;
    import java.io.InputStream;
    import java.net.HttpURLConnection;
    import java.net.URL;

    import org.json.JSONObject;

    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.TextView;
    import android.support.v4.app.NavUtils;

    public class CheckoutActivity extends Activity {


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_checkout);
            // Show the Up button in the action bar.
            setupActionBar();
        }

        /**
         * Set up the {@link android.app.ActionBar}.
         */
        private void setupActionBar() {

            getActionBar().setDisplayHomeAsUpEnabled(true);

        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.checkout, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
            case android.R.id.home:
                // This ID represents the Home or Up button. In the case of this
                // activity, the Up button is shown. Use NavUtils to allow users
                // to navigate up one level in the application structure. For
                // more details, see the Navigation pattern on Android Design:
                //
                // http://developer.android.com/design/patterns/navigation.html#up-vs-back

                NavUtils.navigateUpFromSameTask(this);
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        protected void checkoutOrder() {
            String url = String.format(MainActivity.WEBSERVER_PUTORDER,
                    ((ShopperApp) getApplication()).getOrderString());

            // start an async task to submit the order
            AsyncTask<String, Void, JSONObject> task = new AsyncTask<String, Void, JSONObject>() {

                @Override
                protected JSONObject doInBackground(String... params) {
                    // Extract as JSON
                    JSONObject obj = null;
                    ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
                    try {
                        // the first parameter is the URL we should use
                        URL u = new URL(params[0]);
                        // establish the connection
                        HttpURLConnection conn = (HttpURLConnection) u
                                .openConnection();
                        conn.connect();
                        if (conn.getResponseCode() != 200) // it is not http ok
                                                            // there is a problem on
                                                            // the server
                            return null;
                        InputStream is = conn.getInputStream();

                        // read the stream
                        byte[] buffer = new byte[1024];

                        while (is.read(buffer) != -1)
                            bytestream.write(buffer);

                        // extract as JSON
                        String jsonStr = new String(bytestream.toByteArray());

                        JSONObject Object = new JSONObject(jsonStr);

                        //obj = new JSONObject(jsonStr);

                        return obj;


                    } catch (Throwable t) {
                        t.printStackTrace();
                    }
                    return obj;

                }

                @Override
                protected void onPostExecute(JSONObject result) {
                    TextView resultText = (TextView)findViewById(R.id.text_results);
                    if (result != null) {
                        resultText.setText("Thank you for shopping with us! \n Your order has been placed.");
                        ((ShopperApp)getApplication()).cart.clear();
                    } else {
                        resultText.setText("There seems to be a problem with your order.");
                    }
                }

        };
    }
    }

1 个答案:

答案 0 :(得分:0)

我看到你创建了AsyncTask,但你从未执行过它:

task.execute();

请参阅documentation on execute()。您可以在页面顶部找到代码示例。