2016-01-30 61 views
2

我想發送名稱= 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代碼但仍然沒有添加到數據庫的數據。

+0

是你的代碼在PHP工作正常? –

+0

@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'; –

+0

是由此代碼修改的數據庫。 –

回答

1

嘿zaeem薩塔爾「試試這個代碼它的工作,如果你仍然得到一個錯誤,然後用虛擬數據檢查你的PHP文件」

StringBuilder data; 

    URL urls; 

    HttpURLConnection connection; 

    OutputStreamWriter writeQueryString = null; 

    BufferedReader readData = null; 

    InputStream is = null; 

    try { 

     urls = new URL(url); 
     connection = (HttpURLConnection) urls.openConnection(); 
     connection.setRequestMethod("POST"); 
     connection.setDoOutput(true); 
     connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); 
     connection 
       .setFixedLengthStreamingMode(queryString.getBytes().length); 

     writeQueryString = new OutputStreamWriter(
       connection.getOutputStream(), "ISO-8859-1"); 

     writeQueryString.write(queryString); 

     writeQueryString.flush(); 

     if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {  

      is = connection.getInputStream(); 

      data = new StringBuilder(); 
      if (is != null) { 
       readData = new BufferedReader(new InputStreamReader(is, 
         "ISO-8859-1")); 

       while ((lineRead = readData.readLine()) != null) { 
        data.append(lineRead); 
       } 

       output = data.toString(); 
      } 
     } else { 
      output = "Problem With Connection"; 
     } 
    } catch (Exception e) { 
     Log.d("errors","HttpClient:"+e.toString()); 
    } 

@zaeemsattar下面是您的錯誤的原因1.免費託管有一些代理問題。當你從android php發送請求,然後頁面找不到(404)類錯誤發生。根據我以往的經驗,這個免費的主機... 2.你的PHP代碼函數now()和$ name1這三個變量與3個不同的獲取帖子和請求方法有錯誤..所以你可以使用日期函數的解決方案單個單嬰兒牀和基於Android的請求,如果使用Android端的帖子,然後使用在PHP方面,以解決您的問題3.嘗試虛擬數據首先在PHP頁面上,如默認$ name1 =「zaeem」;與此運行PHP頁面好運

+0

@priyash $ 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'; –

+0

php代碼很簡單。如果有任何錯誤,請更正它 –

+0

@zaeemsattar免費託管有一些代理問題。當你從android php發送請求,然後頁面找不到(404)類錯誤發生。基於我以往的經驗,這個免費託管... –

-1
HttpClient httpclient = new DefaultHttpClient(); 
httppost = new HttpPost("http://za****e.pk/testregister.php"); 
try { 

    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6); 
    nameValuePairs.add(new BasicNameValuePair("name","abcxyz")); 
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
    HttpResponse response = httpclient.execute(httppost); 

}catch(Exception e){ } 

可以在PHP腳本獲取值

$name1=$_POST['name']; 
+0

httpClient已棄用@kami。 –

相關問題