2012-01-12 63 views
0

這裏是我從Eclipse到包含數據庫連接的PHP文件建立連接的代碼,但它將一個空字符串存儲到數據庫。任何人都可以指出我一路上可能犯的錯誤嗎?建立從Eclipse到PHP數據庫文件的連接

add.setOnClickListener(new View.OnClickListener() { 


      public void onClick(View arg0) { 
      Intent add = new Intent(addUser.this, FavUser.class); 
      startActivity(add); 

      InputStream is = null; 


      EditText txtSearch = (EditText) findViewById(R.id.txtSearch); 
      String strSearch = txtSearch.getText().toString(); 

      List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
      nameValuePairs.add(new BasicNameValuePair(strSearch, strSearch)); 

      try { 

       // Add your data 

       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://10.0.2.2/connecttodb.php"); 

       httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

       // Execute HTTP Post Request 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 

      } catch (IOException e) { 
       Log.e("Log_tag", "error in http connection" +e.toString()); 
      } 
      // convert reposonse to string 
+0

你得到了什麼錯誤? – Hend 2012-01-12 03:18:45

+0

您至少需要使用計算機的IP地址而不是'localhost'。 – jprofitt 2012-01-12 03:35:52

+0

檢查http://fahmirahman.wordpress.com/2011/04/21/connection-between-php-server-and-android-client-using-http-and-json/ – 2012-01-12 06:21:01

回答

0

該代碼本身看起來不錯,但如果您提供了任何錯誤消息,它會更好。無論如何,我會建議檢查你的IP地址。它看起來像您的路由器的IP,尋址到您的服務器。轉到谷歌並輸入「我的IP地址是什麼」,並將其替換爲您正在使用的那個;如果它不同。更改代碼後也不要忘記將端口從路由器轉發到您的服務器。