I am trying create an android app that can send data to a PHP server using JSON. I followed this guide: http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/ and here is my Android code:
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);
}
});
}
}
As for my PHP code:
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.";
}
I keep getting No input. I run the android app (on my phone connected to the computer), then I refresh the PHP site but I keep getting no input. The android app does not send the data. Can someone pls help? THANK YOU VERY MUCH!
From what I see, it's the JSONParser class of the tutorial. It checks for
method == "POST"
which is wrong. Change this to
method.equals("POST")
and see if it works.