android中的错误 - 将数据从android发布到php sql

时间:2018-06-10 07:53:32

标签: java php android

我使用以下代码通过php发布从android到sql数据库的数据     import java.util.ArrayList;     import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.example.androidhive.R;

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 = "https://api.androidhive.info/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;
        }

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

    }
}

我在 pdialog.show()收到错误。 该应用程序正好在此行崩溃。 我正在使用异步任务将数据发布到指定的URL。 我应该改变什么? 您能否请更改代码并重新发布?

logcat的:

  

06-10 13:40:26.141 4942-4942 /? E /窗口管理器:   android.view.WindowLeaked:Activity   videosharing.com.myapplication.NewProductActivity已泄露窗口   最初添加的DecorView @ cb5a3b0 []                                                       在android.view.ViewRootImpl。(ViewRootImpl.java:417)                                                       在android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:331)                                                       在android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)                                                       在android.app.Dialog.show(Dialog.java:316)                                                       在videosharing.com.myapplication.NewProductActivity $ CreateNewProduct.onPreExecute(NewProductActivity.java:78)                                                       在android.os.AsyncTask.executeOnExecutor(AsyncTask.java:613)                                                       在android.os.AsyncTask.execute(AsyncTask.java:560)                                                       在videosharing.com.myapplication.NewProductActivity $ 1.onClick(NewProductActivity.java:58)                                                       在android.view.View.performClick(View.java:5610)                                                       在android.view.View $ PerformClick.run(View.java:22265)                                                       在android.os.Handler.handleCallback(Handler.java:751)                                                       在android.os.Handler.dispatchMessage(Handler.java:95)                                                       在android.os.Looper.loop(Looper.java:154)                                                       在android.app.ActivityThread.main(ActivityThread.java:6077)                                                       at java.lang.reflect.Method.invoke(Native Method)                                                       在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                                                       在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

1 个答案:

答案 0 :(得分:0)

jsonParser.makeHttpRequest()返回null可能是因为您有https://个网址。