php - 使用json连接android与php和mysql : Not working

标签 php android mysql json

我正在尝试使用 json 连接 android 和 php 以及 mysql。这是我的编码的一部分,用于添加数据。

基于编码

私有(private)静态字符串 url_create_fixture = "http://android_connect/create_product.php";

如果我通过 Android 手机而不是模拟器测试此应用程序,IP 地址应该是什么?

另一个问题,我在手机上测试了这个应用程序,它说 process.com.example.mobile_fixtures 已意外停止工作。请重试。

这是因为 IP 错误还是我的进程有问题?

package com.example.mobile_fixtures;

public class NewFixturesActivity extends Activity {

// Progress Dialog
private ProgressDialog pDialog;

JSONParser jsonParser = new JSONParser();
EditText inputDate;
EditText inputTime;
EditText inputStadium;
EditText inputHome;
EditText inputAway;


// url to create new product
private static String url_create_fixtures = "http://10.1.1.6/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_fixtures);

    // Edit Text
    inputDate = (EditText) findViewById(R.id.inputDate);
    inputTime = (EditText) findViewById(R.id.inputTime);
    inputStadium = (EditText) findViewById(R.id.inputStadium);
    inputHome = (EditText) findViewById(R.id.inputHome);
    inputAway = (EditText) findViewById(R.id.inputAway);

    // 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(NewFixturesActivity.this);
        pDialog.setMessage("Creating Fixtures..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    /**
     * Creating product
     * */
    protected String doInBackground(String... args) {
        String date = inputDate.getText().toString();
        String time = inputTime.getText().toString();
        String stadium = inputStadium.getText().toString();
        String home = inputHome.getText().toString();
        String away = inputAway.getText().toString();


        // Building Parameters
        List<NameValuePair> params = new ArrayList<NameValuePair>();
        params.add(new BasicNameValuePair("date", date));
        params.add(new BasicNameValuePair("time", time));
        params.add(new BasicNameValuePair("stadium", stadium));
        params.add(new BasicNameValuePair("home", home));
        params.add(new BasicNameValuePair("away", away));

        // getting JSON Object
        // Note that create product url accepts POST method
        JSONObject json = jsonParser.makeHttpRequest(url_create_fixtures,
                "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(), AllFixturesActivity.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();
    }

}
}

最佳答案

不要在 doInBackground 中启动新的 UI Activity ,而是将其移动到 postExecute 中。

关于php - 使用json连接android与php和mysql : Not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20073513/

相关文章:

php - 日历自定义 [突出当前月份]

android - 安卓工作室错误 : Incremental out directory should be set to aar output directory

php - 如何在 Laravel 5.3 中存储动态生成的 View ?

android - 如何让webview适合全屏?

android - ScrollView 上的 OnClickListener

MySQL命令行运行脚本两次

php - SQL 语法错误,请检查与您的 MySQL 服务器版本对应的手册,以了解在第 1 行 'ORDER BY category ASC' 附近使用的正确语法

php - 混合 POST 和 GET 时表单操作中的查询字符串?

php - 填充空提交结果 mysql php

php - MySQL:将唯一列分组到它自己的结果数组中