2013-07-06 54 views
-1

我在做一個應用程序,我需要做一個「註冊」活動,它有很少的EditTexts。現在,我面臨的問題是我需要將數據從EditText發送到某個網站。這個網站也有相同的註冊頁面。如何將數據從EditText發送到網站?

請建議一些方法,我怎樣才能發送數據到特定的網站列。

我重視的網站 enter image description here enter image description here IMG1的圖片是網站和 IMG2的形象是我的應用程序的圖像。

P.S.該網站是在PHP中,我與擁有此網站的人沒有任何關係。所以我不能指望他的任何幫助。

在此先感謝!

+0

你在Java中嘗試GET/POST請求?它非常簡單。但首先,你的圖像不顯示。 – 2013-07-06 17:07:59

+0

對不起。現在圖像編輯.. ,你可以解釋get/post的詳細信息..我新來的android開發。 –

+0

好吧,那麼你知道實際上註冊的php腳本嗎?我的意思是你知道該腳本的網址嗎?如果你這樣做,我可以告訴你如何做一個POST到Android中的鏈接。 – 2013-07-06 17:13:12

回答

0

要從EditText上讀出值做到這一點:

EditText edit = (EditText)findViewById(R.id.edittext); 
String name = edit.getText().toString(); 

執行此操作在表單中的所有其他值

嘗試使用此功能將數據發佈到處理該服務器上的腳本註冊。

public void postData(String name, String email, String password, String city, 
String phone, String, address, String pin) { 
    // Create a new HttpClient and Post Header 
    HttpClient httpclient = new DefaultHttpClient(); 
    HttpPost httppost = new HttpPost("http://www.yoursite.com/registration.php"); 

    try { 
     // Add your data 
     List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
     nameValuePairs.add(new BasicNameValuePair("Name", name)); 
     nameValuePairs.add(new BasicNameValuePair("Email", email)); 
     nameValuePairs.add(new BasicNameValuePair("Password", password)); 
     nameValuePairs.add(new BasicNameValuePair("City", city)); 
     nameValuePairs.add(new BasicNameValuePair("Phone", phone)); 
     nameValuePairs.add(new BasicNameValuePair("Address", address)); 
     nameValuePairs.add(new BasicNameValuePair("Pin", pin)); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

     // Execute HTTP Post Request 
     HttpResponse response = httpclient.execute(httppost); 

    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
    } 
} 

您應該運行在被以這種形式被髮送進行實際發佈之前,因爲我假定這些值進入數據庫及其好的做法,以檢查有效數據的值的檢查。

也在你的android清單文件中,你應該啓用互聯網使用。

<manifest xlmns:android...> 
... 
<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
</manifest> 

您應該考慮的另一點是網絡在任何時候都不能在手機上使用。所以你應該在進行上述呼叫之前檢查網絡狀態。你應該設置其他權限:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> 

現在你可以使用這個功能來輪詢網絡可用性:

public boolean checkNetwork() { 
    ConnectivityManager connectivityManager = (ConnectivityManager) 
     getSystemService(Context.CONNECTIVITY_SERVICE); 
    NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo(); 
    if (networkInfo != null && networkInfo.isConnected()) { 
     return true; 
    }else{ 
     return false; 
    } 
} 
+0

我會嘗試,如果我面臨一些困難..感謝很多.. –

+0

如何將按鈕點擊應用程序中的按鈕點擊網頁? –

+0

同時實現這個我得到android.os.NetworkOnMainThreadException 請解釋我錯了哪裏? –

相關問題