我想發送名稱= zaeem到我的PHP服務器端,但是當我從Android發送它我沒有得到任何東西在PHP端。我的PHP代碼是免費託管。下面是我的Android和PHP代碼。爲什麼數據沒有收到在PHP中,當我從Android發佈它?
這個代碼是GET請求
protected String doInBackground(Void... params)
{
String responseData = "";
InputStream inputStream = null;
HttpURLConnection urlConnection = null;
Integer result = 0;
try {
/* forming th java.net.URL object */
URL url = new URL("http://za*******pk/testregister.php/");
urlConnection = (HttpURLConnection) url.openConnection();
/* optional request header */
// urlConnection.setRequestProperty("Content-Type", "application/json");
/* optional request header */
// urlConnection.setRequestProperty("Accept", "application/json");
/* for Get request */
urlConnection.setRequestMethod("GET");
DataOutputStream wr = new DataOutputStream(
urlConnection.getOutputStream());
wr.writeBytes("name=zaeem");
wr.flush();
wr.close();
int statusCode = urlConnection.getResponseCode();
Log.d("code", statusCode + "");
/* 200 represents HTTP OK */
if (statusCode == 200) {
inputStream = new BufferedInputStream(urlConnection.getInputStream());
responseData = convertInputStreamToString(inputStream);
result = 1; // Successful
} else {
result = 0; //"Failed to fetch data!";
}
} catch (Exception e) {
Log.d("exception", e.getLocalizedMessage());
}
return responseData;
}
該代碼可用於POST請求
私有類MyTaskPost擴展的AsyncTask {
@Override
protected String doInBackground(Void... params) {
String responseData = "";
InputStream inputStream = null;
HttpURLConnection urlConnection = null;
Integer result = 0;
try {
/* forming th java.net.URL object */
URL url = new URL("http://za****e.pk/testregister.php");
urlConnection = (HttpURLConnection) url.openConnection();
/* optional request header */
//urlConnection.setRequestProperty("Content-Type", "application/json");
/* optional request header */
// urlConnection.setRequestProperty("Accept", "application/json");
/* for Get request */
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
DataOutputStream wr = new DataOutputStream(
urlConnection.getOutputStream());
wr.writeBytes("name=zaeem");
wr.flush();
wr.close();
int statusCode = urlConnection.getResponseCode();
Log.d("code", statusCode+"");
/* 200 represents HTTP OK */
if (statusCode == 200) {
Toast.makeText(getApplicationContext(),"200",Toast.LENGTH_SHORT).show();
inputStream = new BufferedInputStream(urlConnection.getInputStream());
responseData = convertInputStreamToString(inputStream);
result = 1; // Successful
}else{
result = 0; //"Failed to fetch data!";
}
} catch (Exception e) {
Log.d("exception", e.getLocalizedMessage());
}
return responseData;
}
而且我的PHP端代碼是(這是免費託管託管)
$name1=$_GET['name'];
$name1=$_POST['name'];
$name1=$_REQUEST['name'];
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
// selecting database
mysql_select_db(DB_DATABASE);
$result = mysql_query("INSERT INTO gcm_users(name, email, gcm_regid,created_at) VALUES('$name1', 'email', 'longtext', NOW())");
$result = mysql_query("SELECT * FROM gcm_users WHERE id = 1") or die(mysql_error());
if (mysql_num_rows($result) > 0)
{
echo 'more than 1 user';
}
當我使用POST方法我得到200代碼但仍然沒有添加到數據庫的數據。
是你的代碼在PHP工作正常? –
@SyedTayyabAbbas $ name1 = $ _ GET ['name']; $ name1 = $ _ POST ['name']; $ name1 = $ _ REQUEST ['name']; $ con = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD); //選擇數據庫\t mysql_select_db(DB_DATABASE); (''name1','email','longtext',NOW())「);這個函數返回一個數組, \t echo'shown'; –
是由此代碼修改的數據庫。 –