次のコードを使用してアイテムを mysql データベースに追加しています。ボタンをクリックすると、データをサーバーにアップロードする非同期タスクが開始されます。しかし、実行すると、「このアプリケーションはメイン スレッドで作業が多すぎる" から LogCat.How can i fix this code problem,thanks.
public class NewProductActivity extends Activity {
// Progress Dialog
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
EditText inputName;
EditText inputPrice;
EditText inputDesc;
// url to create new product
private static String url_create_product = "http://localhost/android_connect/create_product.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
@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(NewProductActivity.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
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
params.add(new BasicNameValuePair("price", price));
params.add(new BasicNameValuePair("description", description));
// getting JSON Object
// Note that create product url accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", 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(getApplicationContext(), AllProductsActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create product
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
これは LogCat の出力です。
05-08 12:51:26.761: I/Choreographer(762): Skipped 31 frames! The application may be doing too much work on its main thread.
05-08 12:51:28.632: W/System.err(762): org.apache.http.conn.HttpHostConnectException: "Connection to localhost refused
05-08 12:51:28.672: W/System.err(762): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
05-08 12:51:28.672: W/System.err(762): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
05-08 12:51:28.712: W/System.err(762): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
05-08 12:51:28.712: W/System.err(762): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
05-08 12:51:28.832: W/System.err(762): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
05-08 12:51:28.902: W/System.err(762): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
05-08 12:51:29.012: W/System.err(762): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
05-08 12:51:29.072: W/System.err(762): at com.example.androidhive.JSONParser.makeHttpRequest(JSONParser.java:51)
05-08 12:51:29.072: W/System.err(762): at com.example.androidhive.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:97)
05-08 12:51:29.172: W/System.err(762): at com.example.androidhive.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:1)
05-08 12:51:29.252: W/System.err(762): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-08 12:51:29.312: W/System.err(762): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-08 12:51:29.322: W/System.err(762): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-08 12:51:29.342: W/System.err(762): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-08 12:51:29.342: W/System.err(762): at java.lang.Thread.run(Thread.java:856)
05-08 12:51:29.372: W/System.err(762): Caused by: java.net.ConnectException: failed to connect to /127.0.0.1 (port 80): connect failed: ECONNREFUSED (Connection refused)
05-08 12:51:29.442: W/System.err(762): at libcore.io.IoBridge.connect(IoBridge.java:114)
05-08 12:51:29.493: W/System.err(762): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
05-08 12:51:29.552: W/System.err(762): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
05-08 12:51:29.552: W/System.err(762): at java.net.Socket.connect(Socket.java:842)
05-08 12:51:29.572: W/System.err(762): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
05-08 12:51:29.652: W/System.err(762): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
05-08 12:51:29.692: W/System.err(762): ... 14 more
05-08 12:51:29.763: W/System.err(762): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
05-08 12:51:29.872: W/System.err(762): at libcore.io.Posix.connect(Native Method)
05-08 12:51:29.872: W/System.err(762): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
05-08 12:51:29.892: W/System.err(762): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
05-08 12:51:29.992: W/System.err(762): at libcore.io.IoBridge.connect(IoBridge.java:112)
05-08 12:51:29.992: W/System.err(762): ... 19 more
05-08 12:51:30.102: E/Buffer Error(762): Error converting result java.lang.NullPointerException: lock == null
05-08 12:51:30.212: E/JSON Parser(762): Error parsing data org.json.JSONException: End of input at character 0 of
05-08 12:51:30.302: W/dalvikvm(762): threadid=14: thread exiting with uncaught exception (group=0x40a71930)
05-08 12:51:30.602: E/AndroidRuntime(762): FATAL EXCEPTION: AsyncTask #4
05-08 12:51:30.602: E/AndroidRuntime(762): java.lang.RuntimeException: An error occured while executing doInBackground()
05-08 12:51:30.602: E/AndroidRuntime(762): at android.os.AsyncTask$3.done(AsyncTask.java:299)
05-08 12:51:30.602: E/AndroidRuntime(762): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
05-08 12:51:30.602: E/AndroidRuntime(762): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
05-08 12:51:30.602: E/AndroidRuntime(762): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
05-08 12:51:30.602: E/AndroidRuntime(762): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
05-08 12:51:30.602: E/AndroidRuntime(762): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
05-08 12:51:30.602: E/AndroidRuntime(762): at java.lang.Thread.run(Thread.java:856)
05-08 12:51:30.602: E/AndroidRuntime(762): Caused by: java.lang.NullPointerException
05-08 12:51:30.602: E/AndroidRuntime(762): at com.example.androidhive.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:101)
05-08 12:51:30.602: E/AndroidRuntime(762): at com.example.androidhive.NewProductActivity$CreateNewProduct.doInBackground(NewProductActivity.java:1)
05-08 12:51:30.602: E/AndroidRuntime(762): at android.os.AsyncTask$2.call(AsyncTask.java:287)
05-08 12:51:30.602: E/AndroidRuntime(762): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
05-08 12:51:30.602: E/AndroidRuntime(762): ... 3 more
05-08 12:51:32.542: I/Choreographer(762): Skipped 395 frames! The application may be doing too much work on its main thread.
05-08 12:51:32.752: I/Choreographer(762): Skipped 54 frames! The application may be doing too much work on its main thread.
05-08 12:51:33.892: I/Choreographer(762): Skipped 295 frames! The application may be doing too much work on its main thread.
05-08 12:51:34.132: I/Choreographer(762): Skipped 58 frames! The application may be doing too much work on its main thread.
05-08 12:51:35.762: E/WindowManager(762): Activity com.example.androidhive.NewProductActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d3d5e0 V.E..... R.....ID 0,0-329,175} that was originally added here
05-08 12:51:35.762: E/WindowManager(762): android.view.WindowLeaked: Activity com.example.androidhive.NewProductActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d3d5e0 V.E..... R.....ID 0,0-329,175} that was originally added here
05-08 12:51:35.762: E/WindowManager(762): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
05-08 12:51:35.762: E/WindowManager(762): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
05-08 12:51:35.762: E/WindowManager(762): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
05-08 12:51:35.762: E/WindowManager(762): at android.app.Dialog.show(Dialog.java:281)
05-08 12:51:35.762: E/WindowManager(762): at com.example.androidhive.NewProductActivity$CreateNewProduct.onPreExecute(NewProductActivity.java:78)
05-08 12:51:35.762: E/WindowManager(762): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
05-08 12:51:35.762: E/WindowManager(762): at android.os.AsyncTask.execute(AsyncTask.java:534)
05-08 12:51:35.762: E/WindowManager(762): at com.example.androidhive.NewProductActivity$1.onClick(NewProductActivity.java:57)
05-08 12:51:35.762: E/WindowManager(762): at android.view.View.performClick(View.java:4204)
05-08 12:51:35.762: E/WindowManager(762): at android.view.View$PerformClick.run(View.java:17355)
05-08 12:51:35.762: E/WindowManager(762): at android.os.Handler.handleCallback(Handler.java:725)
05-08 12:51:35.762: E/WindowManager(762): at android.os.Handler.dispatchMessage(Handler.java:92)
05-08 12:51:35.762: E/WindowManager(762): at android.os.Looper.loop(Looper.java:137)
05-08 12:51:35.762: E/WindowManager(762): at android.app.ActivityThread.main(ActivityThread.java:5041)
05-08 12:51:35.762: E/WindowManager(762): at java.lang.reflect.Method.invokeNative(Native Method)
05-08 12:51:35.762: E/WindowManager(762): at java.lang.reflect.Method.invoke(Method.java:511)
05-08 12:51:35.762: E/WindowManager(762): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-08 12:51:35.762: E/WindowManager(762): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-08 12:51:35.762: E/WindowManager(762): at dalvik.system.NativeStart.main(Native Method)
05-08 12:51:35.812: I/Choreographer(762): Skipped 172 frames! The application may be doing too much work on its main thread.
05-08 12:51:36.172: I/Process(762): Sending signal. PID: 762 SIG: 9