Android JSONPARSER致命异常:AsyncTask

时间:2014-04-08 19:09:17

标签: php android mysql json android-asynctask

当我尝试使用下面的代码连接Mysql DataBase时出现此错误。目标是将新产品添加到数据库中。

 04-08 14:13:10.440: E/AndroidRuntime(1568): FATAL EXCEPTION: AsyncTask #2
    04-08 14:13:10.440: E/AndroidRuntime(1568): Process: com.example.androidmanifest, PID: 1568
    04-08 14:13:10.440: E/AndroidRuntime(1568): java.lang.RuntimeException: An error occured while executing doInBackground()
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at java.lang.Thread.run(Thread.java:841)
    04-08 14:13:10.440: E/AndroidRuntime(1568): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at android.os.Handler.<init>(Handler.java:200)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at android.os.Handler.<init>(Handler.java:114)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at android.app.Dialog.<init>(Dialog.java:109)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at android.app.AlertDialog.<init>(AlertDialog.java:114)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at android.app.AlertDialog.<init>(AlertDialog.java:98)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at com.example.androidmanifest.load.NewProduct.onPreExecute(NewProduct.java:46)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at android.os.AsyncTask.execute(AsyncTask.java:535)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at com.example.androidmanifest.AddProductActivity$CreateNewProduct.doInBackground(AddProductActivity.java:89)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at com.example.androidmanifest.AddProductActivity$CreateNewProduct.doInBackground(AddProductActivity.java:1)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    04-08 14:13:10.440: E/AndroidRuntime(1568):     ... 4 more
04-08 14:13:13.440: I/Choreographer(1568): Skipped 41 frames!  The application may be doing too much work on its main thread.
04-08 14:13:14.380: E/WindowManager(1568): android.view.WindowLeaked: Activity com.example.androidmanifest.AddProductActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b4d56a20 V.E..... R.....ID 0,0-304,96} that was originally added here
04-08 14:13:14.380: E/WindowManager(1568):  at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
04-08 14:13:14.380: E/WindowManager(1568):  at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
04-08 14:13:14.380: E/WindowManager(1568):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
04-08 14:13:14.380: E/WindowManager(1568):  at android.app.Dialog.show(Dialog.java:286)
04-08 14:13:14.380: E/WindowManager(1568):  at com.example.androidmanifest.AddProductActivity$CreateNewProduct.onPreExecute(AddProductActivity.java:76)
04-08 14:13:14.380: E/WindowManager(1568):  at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
04-08 14:13:14.380: E/WindowManager(1568):  at android.os.AsyncTask.execute(AsyncTask.java:535)
04-08 14:13:14.380: E/WindowManager(1568):  at com.example.androidmanifest.AddProductActivity$1.onClick(AddProductActivity.java:56)
04-08 14:13:14.380: E/WindowManager(1568):  at android.view.View.performClick(View.java:4438)
04-08 14:13:14.380: E/WindowManager(1568):  at android.view.View$PerformClick.run(View.java:18422)
04-08 14:13:14.380: E/WindowManager(1568):  at android.os.Handler.handleCallback(Handler.java:733)
04-08 14:13:14.380: E/WindowManager(1568):  at android.os.Handler.dispatchMessage(Handler.java:95)
04-08 14:13:14.380: E/WindowManager(1568):  at android.os.Looper.loop(Looper.java:136)
04-08 14:13:14.380: E/WindowManager(1568):  at android.app.ActivityThread.main(ActivityThread.java:5017)
04-08 14:13:14.380: E/WindowManager(1568):  at java.lang.reflect.Method.invokeNative(Native Method)
04-08 14:13:14.380: E/WindowManager(1568):  at java.lang.reflect.Method.invoke(Method.java:515)
04-08 14:13:14.380: E/WindowManager(1568):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-08 14:13:14.380: E/WindowManager(1568):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-08 14:13:14.380: E/WindowManager(1568):  at dalvik.system.NativeStart.main(Native Method)

这是我的活动课:

public class AddProductActivity extends Activity {

    // Progress Dialog
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();
    EditText inputName;
    EditText inputPrice;
    EditText inputDesc;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_product);

        // Edit Text
        inputName = (EditText) findViewById(R.id.inputName);
        inputPrice = (EditText) findViewById(R.id.inputPrice);
        inputDesc = (EditText) findViewById(R.id.inputDesc);

        // Create button
        Button btnCreateProduct = (Button) findViewById(R.id.btnCreateProduct);

        // button click event
        btnCreateProduct.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                // creating new product in background thread
                new CreateNewProduct().execute();
            }
        });
    }

    /**
     * Background Async Task to Create new product
     * */
    class CreateNewProduct extends AsyncTask<String, String, String> {

        /**
         * Before starting background thread Show Progress Dialog
         * */
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AddProductActivity.this);
            pDialog.setMessage("Creating Product..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        /**
         * Creating product
         * */
        protected String doInBackground(String... args) {
            String name = inputName.getText().toString();
            String price = inputPrice.getText().toString();
            String description = inputDesc.getText().toString();

            // Building Parameters
            NewProduct newProduct = new NewProduct(AddProductActivity.this, name, Integer.parseInt(price), description);
            newProduct.execute();

            return null;
        }

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

    }
}

添加新产品类:

public class NewProduct extends AsyncTask<String, String, String> {
     private String url_all_products = "http://127.0.0.1/Android/create_product.php";
     private static final String TAG_SUCCESS = "success";
    private ProgressDialog pDialog;
     private Context context;
     JSONParser jParser = new JSONParser();
    private ArrayList<String> points = new ArrayList<String>();
    private String name;
    private int price;
    private String desc;
    public ArrayList<String> getPoints() {
        return points;
    }
    /**
     * Before starting background thread Show Progress Dialog
     * */
     public NewProduct(Context context, String name, int  price, String desc){
         this.context = context;
         this.name=name;
         this.price=price;
         this.desc = desc;

     }
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(context);
        pDialog.setMessage("Add New products. Please wait...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    /**
     * getting All products from url
     * */
    protected String doInBackground(String... args) {
        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("name", name));  
        params.add(new BasicNameValuePair("price", price+""));
        params.add(new BasicNameValuePair("description", desc));  
        // getting JSON string from URL
        JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);

        // check log cat fro response
        Log.d("Create Response", json.toString());

        // check for success tag
        try {
            int success = json.getInt(TAG_SUCCESS);

            if (success == 1) {
                // successfully created product
                Intent i = new Intent(context.getApplicationContext(), MainScreenActivity.class);
                context.startActivity(i);

                // closing this screen
//                context.finish();
            } else {
                // failed to create product
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
//        Log.i("count: ", count+"");
        return null;
    }
    /**
     * After completing background task Dismiss the progress dialog
     * **/
    protected void onPostExecute(String file_url) {
        // dismiss the dialog after getting all products
        pDialog.dismiss();
    }

}

JsonParser类:

public class JSONParser {
     static InputStream is = null;
        static JSONObject jObj = null;
        static String json = "";
        // constructor
        public JSONParser() {
        }
        // function get json from url
        // by making HTTP POST or GET method
        public JSONObject makeHttpRequest(String url, String method,
                List<NameValuePair> params) {
            // Making HTTP request
            try {
                // check for request method
                if(method == "POST"){
                    // request method is POST
                    // defaultHttpClient
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(url);
                    httpPost.setEntity(new UrlEncodedFormEntity(params));
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
                }else if(method == "GET"){
                    // request method is GET
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    String paramString = URLEncodedUtils.format(params, "utf-8");
                    url += "?" + paramString;
                    HttpGet httpGet = new HttpGet(url);
                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
                }           
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "iso-8859-1"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                json = sb.toString();
            } catch (Exception e) {
                Log.e("Buffer Error", "Error converting result " + e.toString());
            }
            // try parse the string to a JSON object
            try {
                jObj = new JSONObject(json);
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }
            // return JSON String
            return jObj;
        }
}

php文件:

<?php
$response = array();

if (isset($_REQUEST['name']) && isset($_REQUEST['price']) && isset($_REQUEST['description'])) {

    $name = $_REQUEST['name'];
    $price = $_REQUEST['price'];
    $description = $_REQUEST['description'];

    require_once __DIR__ . '/db_connect.php';

     $db = new DB_CONNECT();

     $result = mysql_query("INSERT INTO products(name, price, description) VALUES('$name', '$price', '$description')");

    if ($result) {
        $response["success"] = 1;
        $response["message"] = "Product successfully created.";

        echo json_encode($response);
    } else {
        $response["success"] = 0;
        $response["message"] = "Oops! An error occurred.";

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

    echo json_encode($response);
}
?>

1 个答案:

答案 0 :(得分:1)

您获得的运行时异常是

  

无法在未调用的线程内创建处理程序   Looper.prepare()

当您在background thread中执行需要在UI thread中执行的操作时,通常会得到此信息。 Reference

在您的情况下,您正在AsyncTask(CreateNewProduct)中执行AsyncTask(NewProduct)。您只能从UI线程执行AsyncTask。在NewProduct中,您尝试在后台线程中创建进度对话框,但需要UI线程。

请参阅AsyncTask文档中的线程规则部分。引用它:

  
      
  • 必须在UI线程上加载AsyncTask类。这是从JELLY_BEAN开始自动完成的。
  •   
  • 必须在UI线程上创建任务实例。
  •   

所以我建议只在一个AsyncTask中进行。您可以在onPostExecute或UI线程中执行第二个AsyncTask,但如果只能从一个线程执行,那么这将是多余的。

AsyncTask within an AsyncTask