2014-02-13 91 views
0

我是android開發新手。我需要解析這個代碼到php數據庫。我可以從logcat獲得響應。我只需要解析這個網站數據庫masterlist。有人可以幫助我與這些請。非常感謝。在Android中解析JSON響應

public class MainActivity extends Activity implements OnClickListener { 

private EditText inputCompanyID; 
private EditText inputBranchID; 
private EditText inputFName; 
private EditText inputLName; 
private EditText inputGenderID; 
private EditText inputBirthdate; 
private EditText inputEmail; 
private EditText inputMobile; 
private EditText inputItemID; 
private EditText inputVoucherID; 

private Button btnCreateProduct;  
private ProgressDialog pDialog; 


JSONParser jsonParser = new JSONParser(); 


// JSON Node names 
private static final String TAG_SUCCESS = "success"; 
private static final String TAG_MESSAGE = "message"; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

// new MyAsyncTask().execute(); 


inputCompanyID = (EditText) findViewById(R.id.inputCompanyID); 
inputBranchID = (EditText) findViewById(R.id.inputBranchID); 
inputLName = (EditText) findViewById(R.id.inputLName); 
inputFName = (EditText) findViewById(R.id.inputFName); 
inputGenderID = (EditText) findViewById(R.id.inputGenderID); 
inputBirthdate = (EditText) findViewById(R.id.inputBirthdate); 
inputEmail = (EditText) findViewById(R.id.inputEmail); 
inputMobile = (EditText) findViewById(R.id.inputMobile); 
inputItemID = (EditText) findViewById(R.id.inputItemID); 
inputVoucherID = (EditText) findViewById(R.id.inputVoucherID); 


btnCreateProduct = (Button) findViewById(R.id.btnAddCustomer); 
btnCreateProduct.setOnClickListener(this); 
} 


// button click event 


    @Override 
    public void onClick(View v) { 
     // creating new product in background thread 
     new PostComment().execute(); 
    } 



    public void sendCustomer() { 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("oAuth", "test123")); 
     params.add(new BasicNameValuePair("action", "add-customer")); 
     params.add(new BasicNameValuePair("company_id", inputCompanyID.getText().toString())); 
     params.add(new BasicNameValuePair("branch_id", inputBranchID.getText().toString())); 
     params.add(new BasicNameValuePair("lastname", inputLName.getText().toString())); 
     params.add(new BasicNameValuePair("firstname", inputFName.getText().toString())); 
     params.add(new BasicNameValuePair("gender_id", inputGenderID.getText().toString())); 
     params.add(new BasicNameValuePair("birth_date", inputBirthdate.getText().toString())); 
     params.add(new BasicNameValuePair("email", inputEmail.getText().toString())); 
     params.add(new BasicNameValuePair("mobile", inputMobile.getText().toString())); 
     params.add(new BasicNameValuePair("item_id", inputItemID.getText().toString())); 
     params.add(new BasicNameValuePair("voucher_id", inputVoucherID.getText().toString())); 

     String response = ISparkLib.getHttpResponse("http://192.168.0.110/teezly/API/1.0/customer.php", params); 
     Log.e("PARAMS",""+response); 

     return; 
     } 


class PostComment extends AsyncTask<String, String, String> { 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    pDialog = new ProgressDialog(MainActivity.this); 
    pDialog.setMessage("Creating Product.."); 
    pDialog.setIndeterminate(false); 
    pDialog.setCancelable(true); 
    pDialog.show(); 
} 


        @Override 
        protected String doInBackground(String... params) { 
         new Thread(new Runnable() { 
          public void run() { 
           // TODO Auto-generated method stub 
           try 
           { 
            sendCustomer(); 
            Thread.sleep(0); 
           }catch(Exception e){ 
           } 

          } 
         }).start(); 
         return null; 
        } 


       }} 
+1

解析什麼?你能解釋一下你的問題嗎? – 2014-02-13 10:52:15

+0

檢查:http://stackoverflow.com/questions/21480634/unable-to-loop-through-dynamic-json-string-recursively-in-android/21480997#21480997 – user2450263

+0

你想解析迴應?它是一個JSON字符串?如果你想在你的網站數據庫中添加一些值,你必須在服務器上開發一個可以接收值的APi – Dyna

回答

0

一些事情你可以在你的代碼調整:

SendCustomer不返回任何東西,但你的AsyncTask返回doInbackground()一個字符串。

也不需要轉換爲字符串當你getText(),這是多餘的。

因此改變sendCustomer這樣的:

public String sendCustomer() { 
    List<NameValuePair> params = new ArrayList<NameValuePair>(); 
    params.add(new BasicNameValuePair("oAuth", "test123")); 
    params.add(new BasicNameValuePair("action", "add-customer")); 
    params.add(new BasicNameValuePair("company_id", inputCompanyID.getText())); 
    params.add(new BasicNameValuePair("branch_id", inputBranchID.getText())); 
    params.add(new BasicNameValuePair("lastname", inputLName.getText())); 
    params.add(new BasicNameValuePair("firstname", inputFName.getText())); 
    params.add(new BasicNameValuePair("gender_id", inputGenderID.getText())); 
    params.add(new BasicNameValuePair("birth_date", inputBirthdate.getText())); 
    params.add(new BasicNameValuePair("email", inputEmail.getText())); 
    params.add(new BasicNameValuePair("mobile", inputMobile.getText())); 
    params.add(new BasicNameValuePair("item_id", inputItemID.getText())); 
    params.add(new BasicNameValuePair("voucher_id", inputVoucherID.getText())); 

    String url = "http://192.168.0.110/teezly/API/1.0/customer.php"; 
    String response = ISparkLib.getHttpResponse(url, params); 
    Log.e("PARAMS",""+response); 

return response; 
} 

AsyncTask已經是一個線程沒有必要使用它裏面的線,那是整點。

變化`doInbackground這樣的:

@Override 
protected String doInBackground(String... params) { 
    return sendCustomer(); 
} 

就可以讀取結果裏面的onPostExecute

protected void onPostExecute(String result) { 
    // do something with the result 
    Log.e("PARAMS","result =" + result); 
} 

投遞PHP服務器時,它是非常容易閱讀的POST變量

customer.php做到這一點

<?php 
if(isset($_POST)){ 
    //to check the data 
    var_dump($_POST); 
    //get the value assign them to variables 
    $oAuth = $_POST['oAuth']; 
    $action = $_POST['action']; 
    $company_id = $_POST['company_id']; 
    $branch_id = $_POST['branch_id']; 
    $lastname = $_POST['lastname']; 
    $firstname = $_POST['firstname']; 
    $gender_id = $_POST['gender_id']; 
    $birth_date = $_POST['birth_date']; 
    $email = $_POST['email']; 
    $mobile = $_POST['mobile']; 
    $item_id = $_POST['item_id']; 
    $voucher_id = $_POST['voucher_id']; 
} 
?> 

還有其他的事情可以改善你的代碼。我希望這有助於你開始!