【问题标题】:can't send data from android to PHP无法将数据从 android 发送到 PHP
【发布时间】:2013-04-21 09:27:36
【问题描述】:

我是开发 android 应用程序的新手。我需要将 ID 存储在 WAMP 服务器中。当我尝试运行我的代码时,模拟器显示“不幸的是我的应用程序已停止”消息,我无法将数据从 android 发送到 PHP。

过去两天我正在尝试解决此问题。我将我的活动添加到清单文件中。 这是我的 .java 文件:

public class MainActivity extends Activity {
    private ProgressDialog pDialog;

    JSONParser jsonParser = new JSONParser();
    EditText inputid;

    private static String url_sample = "http://localhost/android_connect/sample.php";
    // JSON Node names
    private static final String TAG_SUCCESS = "success";

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Edit Text
        inputid = (EditText) findViewById(R.id.editText1);

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                // creating new product in background thread
                new add().execute();
            }
        });
    }

    class add extends AsyncTask<String, String,String> {
      /**
       * Before starting background thread Show Progress Dialog
       * */
       @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("your Registration is processing..wait for few sec..");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();
        }

        protected String doInBackground(String... args) {
            String id = inputid.getText().toString();

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

            // getting JSON Object
            // Note that create product url accepts POST method
            JSONObject json = jsonParser.makeHttpRequest(url_sample, "POST", params);
            // check log cat for 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 = getIntent();
                    setResult(100,i);

                    // closing this screen
                    finish();
                } else {
                    // failed to create product
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
            return doInBackground();
        }

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

PHP代码是:

<?php
$response = array();
if (isset($_POST['id']))
{
    $userid = $_POST['id'];
    require_once __DIR__ . '/db_connect.php';
    $db = new DB_CONNECT();
    $result = mysql_query("INSERT INTO id(ID) VALUES('$userid')");
    echo $userid;
    if ($result) 
    {
        $response["success"] = 1;
        $response["message"] = " Registered successfully";
        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);
}
?>

PHP 和 Android 编码都没有错误..logcat 显示错误消息..有些是

04-09 17:06:02.552: I/Choreographer(10719): Skipped 40 frames!  The application may be doing too much work on its main thread.

【问题讨论】:

  • 尝试用 10.0.2.2 替换 localhost
  • 堆栈跟踪中应该有一些东西如果它崩溃了

标签: php android mysql


【解决方案1】:

您似乎在递归调用您的代码,在doInBackground(...) 中您再次调用doInBackground,并且没有参数(可能给您一个NoSuchMethodException),根据您的规范,您必须返回一个字符串,但是鉴于您没有使用结果,您不妨返回 null(或将规范更改为 Void)。

此外,您没有看到堆栈跟踪的原因是您可能正在过滤 log 语句,而 e.printStackTrace() 不使用日志语句。

编辑: 请用 Log.e("MyActivityNameHere", e.toString()) 而不是 e.prinStackTrace() 来查看异常

【讨论】:

    【解决方案2】:

    由于该行,您收到错误消息

        return doInBackground();
    

    您正在递归调用 doInBackground() 方法,这给线程带来了繁重的工作负载。

    尝试返回一些字符串(在你的情况下,只返回 null;)

    【讨论】:

    【解决方案3】:

    如果你使用的是 localhost,那么你应该像这样调用 url

          private static String url_sample = "http://10.0.2.2/android_connect/sample.php";
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-05
      • 1970-01-01
      • 1970-01-01
      • 2021-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      相关资源
      最近更新 更多