我是Android应用程序开发的新手。我做了一个编码从android.it存储wamp服务器数据库中的数据很好。但我没有从PHP到android.i不知道我做了什么错误的成功消息。 这是我的编码 .java文件
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);
}
?>
我希望在数据库中存储数据后在模拟器中显示一个消息框。帮助我。 感谢:)
答案 0 :(得分:0)
无法在doInBackground(String ... args)方法中完成UI修改。将逻辑移动到onPostExecute(String file_url)方法创建并显示对话框。
从doInBackground()返回布尔状态并检查onPostExecute()中的状态并相应地显示对话框。