我是Android开发的新手。我需要将此代码解析为php数据库。我可以从logcat获得响应。我只需要将其解析为网站数据库主列表。有人可以帮我这些请。非常感谢你。
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;
}
}}
答案 0 :(得分: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'];
}
?>
您还可以采取其他措施来改进代码。我希望这对你有所帮助!