2013-04-22 21 views
0

我是新來android應用程序開發。我做了一個編碼,用於存儲來自android.wamp服務器數據庫的數據。我工作得很好。但是我沒有從PHP獲得成功消息給android.i,不知道我做了什麼錯誤。 這裏是我的編碼 java文件消息框不顯示在Android模擬器

package com.example.loga; 


import android.os.Bundle; 

import android.app.Activity; 
import android.app.AlertDialog; 

import android.view.Menu; 
import android.widget.Button; 


import java.util.ArrayList; 
import java.util.List; 
import org.apache.http.NameValuePair; 
import org.apache.http.message.BasicNameValuePair; 
import org.json.JSONObject; 



import android.app.ProgressDialog; 


import android.os.AsyncTask; 
import android.util.Log; 
import android.view.View; 
import android.widget.EditText; 


public class MainActivity1 extends Activity { 

    private ProgressDialog pDialog; 

    JSONParser jsonParser = new JSONParser(); 
     EditText inputuserid; 
     EditText inputserverid; 

     private static String url_register = "http://10.0.2.2/android_connect/register.php"; 
     // JSON Node names 
     private static final String TAG_SUCCESS = "success"; 





    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_activity1); 
     // Edit Text 
     inputuserid = (EditText) findViewById(R.id.editText1); 
     inputserverid= (EditText) findViewById(R.id.editText2); 
     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(); 
      } 
     }); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main_activity1, menu); 
     return true; 
    } 
    class add extends AsyncTask<String, String,String> { 

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

     /** 
     * Creating product 
     * */ 
     protected String doInBackground(String... args) { 
      String userid = inputuserid.getText().toString(); 
      String serverid = inputserverid.getText().toString(); 

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


      // getting JSON Object 
      // Note that create product url accepts POST method 
      JSONObject json = jsonParser.makeHttpRequest(url_register, 
        "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) { 
        AlertDialog alertDialog; 
        alertDialog = new AlertDialog.Builder(MainActivity1.this).create(); 
        alertDialog.setTitle("password verification.."); 
        alertDialog.setMessage("success.!!"); 
        alertDialog.show(); 

        finish(); 
       } else { 
        // failed to create product 
       } 
      } 
      catch (Exception 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(); 
     } 

    } 

} 

和我的PHP代碼是

<?php 
$response = array(); 
// check for required fields 
if (isset($_POST['userid']) && isset($_POST['serverid'])) 
{ 

    $userid = $_POST['userid']; 
    $serverid= $_POST['serverid']; 
     // include db connect class 
    require_once __DIR__ . '/db_connect.php'; 

    // connecting to db 
    $db = new DB_CONNECT(); 

    // mysql inserting a new row 
    $result = mysql_query("INSERT INTO opass(User_ID, Server_ID) VALUES('$userid', '$serverid')"); 

    // check if row inserted or not 
    if ($result) 
{ 
     // successfully inserted into database 
     $response["success"] = 1; 
     $response["message"] = " Registered successfully"; 

     // echoing JSON response 
     echo json_encode($response); 
    } 
else 
{ 
     // failed to insert row 
     $response["success"] = 0; 
     $response["message"] = "Oops! An error occurred."; 

     // echoing JSON response 
     echo json_encode($response); 
    } 
} 
else 
{ 
    // required field is missing 
    $response["success"] = 0; 
    $response["message"] = "Required field(s) is missing"; 

    // echoing JSON response 
    echo json_encode($response); 
} 

?> 

我想database.help我存儲數據後顯示在模擬器的消息框。 謝謝:)

+0

哪一個是你的問題,顯示一個對話框,或從服務器獲得成功響應? – 2013-04-22 04:00:04

+0

我不知道代碼出了什麼問題。從服務器獲得成功響應後,我想要顯示一個消息框 – 2013-04-22 04:08:03

+0

還有一件事要在調試中檢查你的問題和int success = json.getInt(TAG_SUCCESS); if(success == 1){...........這裏檢查1是不是cuming或不 – Rohit 2013-04-22 13:24:05

回答

0

UI修改不能在doInBackground(String ... args)方法中完成。將邏輯移到創建並顯示對話框到onPostExecute(String file_url)方法中。

從doInBackground()返回布爾狀態並檢查onPostExecute()中的狀態並相應地顯示對話框。