Androidアプリ開発初心者です。私はアンドロイドからワンプサーバーデータベースにデータを保存するためのコーディングを行いました.それはうまくいきます.しかし、私はPHPからアンドロイドに成功メッセージを受け取っていません.私は私がした間違いを知りません. ここに私のコーディング.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);
}
?>
データベースにデータを保存した後、エミュレータにメッセージボックスを表示したいです。ありがとう:)