2013-04-21 59 views
3

我是新來開發android應用程序。我需要將ID存儲在WAMP服務器中。當我嘗試運行我的代碼時,模擬器顯示「不幸myapp已停止」消息,我無法將數據從android發送到PHP。無法將數據從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的coding..logcat沒有錯誤顯示錯誤messages..some是

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

嘗試remplace本地主機與10.0。 2.2 – Pierrickouw 2013-04-21 09:29:18

+0

在stacktrace中應該有東西,如果它崩潰 – 2013-04-21 09:32:25

回答

0

你得到,因爲線

return doInBackground(); 

您呼叫的doInBackground()方法的錯誤消息遞歸,這給線程繁重的工作量。

試圖返回一些字符串(在你的情況,只是返回null;)

+0

謝謝。我再次logcat拋出相同的error.pls幫助 – 2013-04-21 09:47:28

+0

得到它..如果你使用本地主機,那麼你應該打電話像這樣的網址「http://10.0.2.2/android_connect/sample.php」 – Antony 2013-04-21 09:51:58

+0

謝謝。我有:) – 2013-04-21 10:02:02

2

你似乎在遞歸地調用你的代碼,在doInBackground(...)中,你再次調用doInBackground,也沒有參數(可能會給你一個NoSuchMethodException),根據你的規範,你必須返回一個字符串,但看到你不使用結果,你可能會返回一個null(或將規格更改爲Void)。

此外,您沒有看到堆棧跟蹤的原因是您可能正在篩選log語句,而e。 printStackTrace()不使用日誌語句。

編輯: 請使用 Log.e("MyActivityNameHere", e.toString())代替e.prinStackTrace()看到異常

+0

我糾正.again它會拋出錯誤..任何幫助嗎? – 2013-04-21 09:48:20

+0

請參閱我的編輯 – 2013-04-21 09:57:27

0

如果您正在使用本地主機,那麼你應該調用的URL這樣

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