来自MySql表的Android应用数据

时间:2013-11-06 06:21:00

标签: java android mysql android-intent

我正在开发和Android应用程序...其中..i将我的应用程序与MySql表连接,​​其中表具有1-10的单独结果...所以当值用户得到结果在1-10之间时相应的结果必须从表格中获取并显示在文本视图中。

但我得到的消息就像#不幸的是应用程序停止了#39;我在这里发布代码和logcat请检查并回复...

mainactivity.java

    public class FirstResult extends Activity

    {

            String pid;
            TextView txtName;

        // Progress Dialog
            private ProgressDialog pDialog;

            // JSON parser class
            JSONParser jsonParser = new JSONParser();


            // single product url
               private static final String url_product_detials = "http://iascpl.com/app/get_product_details.php";


            // JSON Node names
                private static final String TAG_SUCCESS = "success";
                private static final String TAG_PRODUCT = "product";
                //private static final String TAG_PID = "pid";
                //private static final String TAG_NUMBER = "number";
                //private static final String TAG_PRICE = "price";
                private static final String TAG_DESCRIPTION = "description";


        @Override
        protected void onCreate(Bundle savedInstanceState) 

        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.firstresult_xm);




            TextView txt1 = (TextView) findViewById (R.id.textView2);
            txt1.setText(getIntent().getStringExtra("name10"));

            pid = txt1.getText().toString();


            new GetProductDetails().execute();
        }






        /**
         * Background Async Task to Get complete product details
         * */
        class GetProductDetails extends AsyncTask<String, String, String> {

            /**
             * Before starting background thread Show Progress Dialog
             * */
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pDialog = new ProgressDialog(FirstResult.this);
                pDialog.setMessage("Loading product details. Please wait...");
                pDialog.setIndeterminate(false);
                pDialog.setCancelable(true);
                pDialog.show();
            }

            /**
             * Getting product details in background thread
             * */
            protected String doInBackground(String... args) 


            {

        {

            TextView txt1 = (TextView) findViewById (R.id.textView2);
            txt1.setText(getIntent().getStringExtra("name10"));
            pid = txt1.getText().toString();



                        // Building Parameters
                        List<NameValuePair> params = new ArrayList<NameValuePair>();
                        params.add(new BasicNameValuePair("pid", pid));


                        // getting product details by making HTTP request
                        // Note that product details url will use GET request
                        JSONObject json = jsonParser.makeHttpRequest(
                                url_product_detials, "GET", params);

                        // check your log for json response
                        Log.d("Single Product Details", json.toString());

                        // json success tag

                        try {
                        int success = json.getInt(TAG_SUCCESS);
                        if (success == 1) {
                            // successfully received product details
                            JSONArray productObj = json
                                    .getJSONArray(TAG_PRODUCT); // JSON Array

                            // get first product object from JSON Array
                            final JSONObject product = productObj.getJSONObject(0);

                            // product with this pid found
                            // Edit Text
                            txtName = (TextView) findViewById(R.id.textView3);


                            runOnUiThread(new Runnable() {  
                                @Override
                                public void run() 
                                {
                                    // TODO Auto-generated method stub
                                    try {
                                        txtName.setText(product.getString(TAG_DESCRIPTION));
                                    } catch (JSONException e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            });

                        }else{
                            // product with pid not found
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }




            return null;
        }


        /**
         * After completing background task Dismiss the progress dialog
         * **/
        protected void onPostExecute(String file_url) {
            // dismiss the dialog once got all details
            pDialog.dismiss();
        }
    }



}

php代码:

<?php

/*
 * Following code will get single product details
 * A product is identified by product id (pid)
 */ 
// array for JSON response

$response = array();   

// include db connect class
require_once __DIR__ . '/db_connect.php';   

// connecting to db
$db = new DB_CONNECT();

// check for post data
if (isset($_GET["pid"])) {
    $pid = $_GET['pid'];

    // get a product from products table
    $result = mysql_query("SELECT *FROM prediction WHERE pid = $pid");

    if (!empty($result)) {
        // check for empty result
        if (mysql_num_rows($result) > 0) {

            $result = mysql_fetch_array($result);

            $product = array();
            $product["pid"] = $result["pid"];
            $product["number"] = $result["number"];
           // $product["price"] = $result["price"];
            $product["description"] = $result["description"];
            $product["created_at"] = $result["created_at"];
            $product["updated_at"] = $result["updated_at"];              

            // success
            $response["success"] = 1;

            // user node
            $response["product"] = array();

            array_push($response["product"], $product);

            // echoing JSON response
            echo json_encode($response);
        } else {
            // no product found
            $response["success"] = 0;
            $response["message"] = "No product found";

            // echo no users JSON
            echo json_encode($response);
        }
    } else {
        // no product found
        $response["success"] = 0;
        $response["message"] = "No product found";

        // echo no users JSON
        echo json_encode($response);
    }
} else {
    // required field is missing
    $response["success"] = 0;
    $response["message"] = "Required field(s) is missing";

    // echoing JSON response
    echo json_encode($response);
}
?>

logcat的

11-06 05:53:45.590: I/Choreographer(1583): Skipped 46 frames!  The application may be doing too much work on its main thread.
11-06 05:53:45.921: I/Choreographer(1583): Skipped 64 frames!  The application may be doing too much work on its main thread.
11-06 05:53:46.341: I/Choreographer(1583): Skipped 65 frames!  The application may be doing too much work on its main thread.
11-06 05:53:46.610: I/Choreographer(1583): Skipped 61 frames!  The application may be doing too much work on its main thread.
11-06 05:53:47.070: I/Choreographer(1583): Skipped 90 frames!  The application may be doing too much work on its main thread.
11-06 05:53:47.522: I/Choreographer(1583): Skipped 99 frames!  The application may be doing too much work on its main thread.
11-06 05:53:49.491: I/Choreographer(1583): Skipped 297 frames!  The application may be doing too much work on its main thread.
11-06 05:53:49.710: I/Choreographer(1583): Skipped 32 frames!  The application may be doing too much work on its main thread.
11-06 05:53:50.810: I/Choreographer(1583): Skipped 121 frames!  The application may be doing too much work on its main thread.
11-06 05:53:51.200: I/Choreographer(1583): Skipped 100 frames!  The application may be doing too much work on its main thread.
11-06 05:53:51.591: I/Choreographer(1583): Skipped 69 frames!  The application may be doing too much work on its main thread.
11-06 05:53:52.075: I/Choreographer(1583): Skipped 92 frames!  The application may be doing too much work on its main thread.
11-06 05:53:52.692: I/Choreographer(1583): Skipped 158 frames!  The application may be doing too much work on its main thread.
11-06 05:53:53.005: I/Choreographer(1583): Skipped 56 frames!  The application may be doing too much work on its main thread.
11-06 05:53:53.402: I/Choreographer(1583): Skipped 95 frames!  The application may be doing too much work on its main thread.
11-06 05:53:54.300: I/Choreographer(1583): Skipped 220 frames!  The application may be doing too much work on its main thread.
11-06 05:53:54.890: I/Choreographer(1583): Skipped 111 frames!  The application may be doing too much work on its main thread.
11-06 05:53:55.780: I/Choreographer(1583): Skipped 208 frames!  The application may be doing too much work on its main thread.
11-06 05:53:55.970: I/Choreographer(1583): Skipped 46 frames!  The application may be doing too much work on its main thread.
11-06 05:53:56.162: I/Choreographer(1583): Skipped 49 frames!  The application may be doing too much work on its main thread.
11-06 05:53:56.360: I/Choreographer(1583): Skipped 37 frames!  The application may be doing too much work on its main thread.
11-06 05:53:56.561: I/Choreographer(1583): Skipped 39 frames!  The application may be doing too much work on its main thread.
11-06 05:53:56.819: I/Choreographer(1583): Skipped 65 frames!  The application may be doing too much work on its main thread.
11-06 05:53:57.132: I/Choreographer(1583): Skipped 39 frames!  The application may be doing too much work on its main thread.
11-06 05:53:57.270: I/Choreographer(1583): Skipped 36 frames!  The application may be doing too much work on its main thread.
11-06 05:53:57.560: I/Choreographer(1583): Skipped 31 frames!  The application may be doing too much work on its main thread.
11-06 05:53:58.000: I/Choreographer(1583): Skipped 42 frames!  The application may be doing too much work on its main thread.
11-06 05:53:58.311: I/Choreographer(1583): Skipped 36 frames!  The application may be doing too much work on its main thread.
11-06 05:53:58.510: I/Choreographer(1583): Skipped 43 frames!  The application may be doing too much work on its main thread.
11-06 05:53:59.310: I/Choreographer(1583): Skipped 32 frames!  The application may be doing too much work on its main thread.
11-06 05:53:59.610: I/Choreographer(1583): Skipped 38 frames!  The application may be doing too much work on its main thread.
11-06 05:54:00.170: I/Choreographer(1583): Skipped 34 frames!  The application may be doing too much work on its main thread.
11-06 05:54:00.380: I/Choreographer(1583): Skipped 39 frames!  The application may be doing too much work on its main thread.
11-06 05:54:00.580: I/Choreographer(1583): Skipped 49 frames!  The application may be doing too much work on its main thread.
11-06 05:54:00.930: I/Choreographer(1583): Skipped 73 frames!  The application may be doing too much work on its main thread.
11-06 05:54:01.283: I/Choreographer(1583): Skipped 35 frames!  The application may be doing too much work on its main thread.
11-06 05:54:01.460: I/Choreographer(1583): Skipped 36 frames!  The application may be doing too much work on its main thread.
11-06 05:54:01.590: I/Choreographer(1583): Skipped 32 frames!  The application may be doing too much work on its main thread.
11-06 05:54:02.050: I/Choreographer(1583): Skipped 36 frames!  The application may be doing too much work on its main thread.
11-06 05:54:02.349: I/Choreographer(1583): Skipped 30 frames!  The application may be doing too much work on its main thread.
11-06 05:54:02.679: I/Choreographer(1583): Skipped 55 frames!  The application may be doing too much work on its main thread.
11-06 05:54:02.960: I/Choreographer(1583): Skipped 69 frames!  The application may be doing too much work on its main thread.
11-06 05:54:03.130: I/Choreographer(1583): Skipped 32 frames!  The application may be doing too much work on its main thread.
11-06 05:54:03.630: I/Choreographer(1583): Skipped 50 frames!  The application may be doing too much work on its main thread.
11-06 05:54:03.950: I/Choreographer(1583): Skipped 31 frames!  The application may be doing too much work on its main thread.
11-06 05:54:04.480: W/dalvikvm(1583): threadid=12: thread exiting with uncaught exception (group=0x40a71930)
11-06 05:54:04.540: I/Choreographer(1583): Skipped 35 frames!  The application may be doing too much work on its main thread.
11-06 05:54:05.290: I/Choreographer(1583): Skipped 52 frames!  The application may be doing too much work on its main thread.
11-06 05:54:05.460: I/Choreographer(1583): Skipped 35 frames!  The application may be doing too much work on its main thread.
11-06 05:54:05.736: I/Choreographer(1583): Skipped 69 frames!  The application may be doing too much work on its main thread.
11-06 05:54:05.942: I/Choreographer(1583): Skipped 52 frames!  The application may be doing too much work on its main thread.
11-06 05:54:06.070: I/Choreographer(1583): Skipped 32 frames!  The application may be doing too much work on its main thread.
11-06 05:54:06.601: I/Choreographer(1583): Skipped 33 frames!  The application may be doing too much work on its main thread.
11-06 05:54:06.830: I/Choreographer(1583): Skipped 30 frames!  The application may be doing too much work on its main thread.
11-06 05:54:07.000: I/Choreographer(1583): Skipped 42 frames!  The application may be doing too much work on its main thread.
11-06 05:54:07.140: I/Choreographer(1583): Skipped 35 frames!  The application may be doing too much work on its main thread.
11-06 05:54:07.260: I/Choreographer(1583): Skipped 30 frames!  The application may be doing too much work on its main thread.
11-06 05:54:07.990: I/Choreographer(1583): Skipped 42 frames!  The application may be doing too much work on its main thread.
11-06 05:54:09.140: I/Choreographer(1583): Skipped 33 frames!  The application may be doing too much work on its main thread.
11-06 05:54:10.570: I/Choreographer(1583): Skipped 42 frames!  The application may be doing too much work on its main thread.
11-06 05:54:10.980: I/Choreographer(1583): Skipped 36 frames!  The application may be doing too much work on its main thread.
11-06 05:54:11.160: I/Choreographer(1583): Skipped 30 frames!  The application may be doing too much work on its main thread.
11-06 05:54:11.290: I/Choreographer(1583): Skipped 35 frames!  The application may be doing too much work on its main thread.
11-06 05:54:12.060: I/Choreographer(1583): Skipped 42 frames!  The application may be doing too much work on its main thread.
11-06 05:54:13.600: E/AndroidRuntime(1583): FATAL EXCEPTION: AsyncTask #2
11-06 05:54:13.600: E/AndroidRuntime(1583): java.lang.RuntimeException: An error occured while executing doInBackground()
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at java.lang.Thread.run(Thread.java:856)
11-06 05:54:13.600: E/AndroidRuntime(1583): Caused by: android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.view.View.requestLayout(View.java:15473)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.view.View.requestLayout(View.java:15473)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.view.View.requestLayout(View.java:15473)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.view.View.requestLayout(View.java:15473)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.view.View.requestLayout(View.java:15473)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.widget.ScrollView.requestLayout(ScrollView.java:1440)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.view.View.requestLayout(View.java:15473)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:318)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.view.View.requestLayout(View.java:15473)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.widget.TextView.checkForRelayout(TextView.java:6452)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.widget.TextView.setText(TextView.java:3696)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.widget.TextView.setText(TextView.java:3554)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.widget.TextView.setText(TextView.java:3529)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at com.example.numero.FirstResult$GetProductDetails.doInBackground(FirstResult.java:109)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at com.example.numero.FirstResult$GetProductDetails.doInBackground(FirstResult.java:1)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-06 05:54:13.600: E/AndroidRuntime(1583):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
11-06 05:54:13.600: E/AndroidRuntime(1583):     ... 4 more
11-06 05:54:13.683: I/Choreographer(1583): Skipped 38 frames!  The application may be doing too much work on its main thread.
11-06 05:54:13.930: I/Choreographer(1583): Skipped 50 frames!  The application may be doing too much work on its main thread.
11-06 05:54:14.540: I/Choreographer(1583): Skipped 41 frames!  The application may be doing too much work on its main thread.
11-06 05:54:14.660: I/Choreographer(1583): Skipped 30 frames!  The application may be doing too much work on its main thread.
11-06 05:54:14.910: I/Choreographer(1583): Skipped 31 frames!  The application may be doing too much work on its main thread.
11-06 05:54:15.971: I/Choreographer(1583): Skipped 30 frames!  The application may be doing too much work on its main thread.
11-06 05:54:16.300: I/Choreographer(1583): Skipped 35 frames!  The application may be doing too much work on its main thread.
11-06 05:54:17.550: I/Choreographer(1583): Skipped 39 frames!  The application may be doing too much work on its main thread.
11-06 05:54:17.750: I/Choreographer(1583): Skipped 50 frames!  The application may be doing too much work on its main thread.
11-06 05:54:18.203: I/Choreographer(1583): Skipped 117 frames!  The application may be doing too much work on its main thread.
11-06 05:54:18.441: I/Choreographer(1583): Skipped 61 frames!  The application may be doing too much work on its main thread.
11-06 05:54:19.310: I/Choreographer(1583): Skipped 223 frames!  The application may be doing too much work on its main thread.
11-06 05:54:21.160: I/Choreographer(1583): Skipped 472 frames!  The application may be doing too much work on its main thread.
11-06 05:54:22.531: I/Choreographer(1583): Skipped 356 frames!  The application may be doing too much work on its main thread.
11-06 05:54:23.190: I/Choreographer(1583): Skipped 111 frames!  The application may be doing too much work on its main thread.
11-06 05:54:23.580: E/WindowManager(1583): Activity com.example.numero.FirstResult has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40e4b358 V.E..... R.....ID 0,0-480,144} that was originally added here
11-06 05:54:23.580: E/WindowManager(1583): android.view.WindowLeaked: Activity com.example.numero.FirstResult has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40e4b358 V.E..... R.....ID 0,0-480,144} that was originally added here
11-06 05:54:23.580: E/WindowManager(1583):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
11-06 05:54:23.580: E/WindowManager(1583):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
11-06 05:54:23.580: E/WindowManager(1583):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-06 05:54:23.580: E/WindowManager(1583):  at android.app.Dialog.show(Dialog.java:281)
11-06 05:54:23.580: E/WindowManager(1583):  at com.example.numero.FirstResult$GetProductDetails.onPreExecute(FirstResult.java:85)
11-06 05:54:23.580: E/WindowManager(1583):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
11-06 05:54:23.580: E/WindowManager(1583):  at android.os.AsyncTask.execute(AsyncTask.java:534)
11-06 05:54:23.580: E/WindowManager(1583):  at com.example.numero.FirstResult.onCreate(FirstResult.java:62)
11-06 05:54:23.580: E/WindowManager(1583):  at android.app.Activity.performCreate(Activity.java:5104)
11-06 05:54:23.580: E/WindowManager(1583):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-06 05:54:23.580: E/WindowManager(1583):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
11-06 05:54:23.580: E/WindowManager(1583):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
11-06 05:54:23.580: E/WindowManager(1583):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-06 05:54:23.580: E/WindowManager(1583):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
11-06 05:54:23.580: E/WindowManager(1583):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 05:54:23.580: E/WindowManager(1583):  at android.os.Looper.loop(Looper.java:137)
11-06 05:54:23.580: E/WindowManager(1583):  at android.app.ActivityThread.main(ActivityThread.java:5041)
11-06 05:54:23.580: E/WindowManager(1583):  at java.lang.reflect.Method.invokeNative(Native Method)
11-06 05:54:23.580: E/WindowManager(1583):  at java.lang.reflect.Method.invoke(Method.java:511)
11-06 05:54:23.580: E/WindowManager(1583):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
11-06 05:54:23.580: E/WindowManager(1583):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
11-06 05:54:23.580: E/WindowManager(1583):  at dalvik.system.NativeStart.main(Native Method)
11-06 05:54:23.600: I/Choreographer(1583): Skipped 42 frames!  The application may be doing too much work on its main thread.
11-06 05:54:50.233: I/Process(1583): Sending signal. PID: 1583 SIG: 9
11-06 05:54:56.100: I/Choreographer(1623): Skipped 55 frames!  The application may be doing too much work on its main thread.
11-06 05:54:56.520: D/gralloc_goldfish(1623): Emulator without GPU emulation detected.

1 个答案:

答案 0 :(得分:1)

您无法使用线程内的txt1.setText(getIntent().getStringExtra("name10"));txtName.setText(product.getString(TAG_DESCRIPTION));修改文本,因为您只能修改UI线程中的视图。改为使用处理程序,让他更改文本。

试试这个:

runOnUiThread(new Runnable() {  
                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    txtName.setText(product.getString(TAG_DESCRIPTION));
                }