3

私はAndroidアプリケーションを開発するのが初めてです。ID を WAMP サーバーに保存する必要があります。コードを実行しようとすると、エミュレーターに「残念ながらアプリが停止しました」というメッセージが表示され、Android から PHP にデータを送信できません。

過去2日間、これを修正しようとしています。アクティビティをマニフェスト ファイルに追加しました。ここに私の .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.
4

3 に答える 3

0

行が原因でエラーメッセージが表示されます

    return doInBackground();

doInBackground() メソッドを再帰的に呼び出しているため、スレッドに大きな負荷がかかります。

いくつかの文字列を返してみてください(あなたの場合、nullを返すだけです;)

于 2013-04-21T09:38:39.227 に答える