JSONを使用してPHPサーバーにデータを送信できるAndroidアプリを作成しようとしています。私はこのガイドに従いました: http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/そしてここに私のAndroidコードがあります:
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.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class HelloWorldActivity extends Activity {
JSONParser jsonParser = new JSONParser();
EditText userName;
// url to create new product
private static String url = "http://192.168.1.35/workspace/sosapp/db_connect.php";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Edit Text
userName = (EditText) findViewById(R.id.userName);
// Send button
Button sendButton = (Button) findViewById(R.id.sendButton);
// button click event
sendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
String name = userName.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
// getting JSON Object
JSONObject json = jsonParser.makeHttpRequest(url, "POST", params);
}
});
}
}
私のPHPコードについては:
function insert($var1) {
// mysql inserting a new row
$result = mysql_query("INSERT INTO report (name) VALUES ('$var1')");
// check if row inserted or not
if ($result) {
// successfully inserted into database;
$msg = "Message received.";
} else {
$msg = "Not received.";
}
return ($msg);
}
if (isset($_POST['name'])) {
$name = $_POST['name']);
insert($name);
} else {
echo "No input.";
}
入力がありません。Android アプリを (コンピューターに接続された電話で) 実行してから、PHP サイトを更新しましたが、入力が得られません。Android アプリはデータを送信しません。誰かが助けてくれますか?どうもありがとうございます!