2016-01-30 30 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。 –

相关问题