當前位置:首頁 > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > Anddroid App和Java Web服務(wù)器間數(shù)據(jù)交互 之Android App搭建
創(chuàng)建Android App程序,提交數(shù)據(jù)給Java Web服務(wù)器,并得到Java Web服務(wù)器的返回結(jié)果。
1) 安裝eclipse集成Android開發(fā)環(huán)境(包括sdk和adt,本文不再詳述)。
2) 創(chuàng)建Android App項目。點擊File->New->Android Application Project,在頁面中Application Name對應(yīng)的輸入框中輸入MyApp項目名稱,點擊Next,后點擊Finish。如下圖所示:
3) 布局文件activity_main.xml中創(chuàng)建EditText輸入框,可以輸入用戶名和密碼,創(chuàng)建Button按鈕,用于注冊,點擊該按鈕將輸入的用戶名和密碼提交給Java Web應(yīng)用服務(wù)器。activity_main.xml源碼如下:
xmlns:tools="//schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="36dp"
android:layout_marginTop="58dp"
android:text="用戶名:" />
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="40dp"
android:text="密碼:" />
android:id="@+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView2"
android:layout_marginLeft="31dp"
android:layout_toRightOf="@+id/textView2"
android:ems="10" >
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView1"
android:layout_alignLeft="@+id/username"
android:ems="10"
android:inputType="textPassword" />
android:id="@+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:onClick="register"
android:text="注冊" />
4) 在MainActivity.java文件中,響應(yīng)按鈕的點擊事件,在響應(yīng)事件中啟動線程將輸入的用戶名和密碼以POST方式提交給JavaWeb服務(wù)器。代碼如下所示:
public class MainActivity extends Activity {
public EditText nameEdit,passwordEdit;
public String nameInput,pwdInput;
public final String SERVER_URL = "//10.0.2.2:8080/MyAppServer/LoginAction";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nameEdit = (EditText)findViewById(R.id.username);
passwordEdit = (EditText)findViewById(R.id.password);
}
// 點擊注冊按鈕觸發(fā)該方法
public void register(View v){
nameInput = nameEdit.getText().toString();
pwdInput = passwordEdit.getText().toString();
RegisterTask rt = new RegisterTask();
rt.execute(SERVER_URL);
}
class RegisterTask extends AsyncTask
@Override
protected String doInBackground(String... params) {
String result = "";
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(params[0]);
//將傳給服務(wù)器
ArrayList
paramList.add(new BasicNameValuePair("name", nameInput));//第一個參數(shù)必須和服務(wù)器獲取參數(shù)的key相同
paramList.add(new BasicNameValuePair("pwd", pwdInput));
try {
post.setEntity(new UrlEncodedFormEntity(paramList));
HttpResponse response = client.execute(post);
if(response.getStatusLine().getStatusCode()==200){
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
byte[] data = new byte[1024];
int len = 0 ;
while((len=is.read(data))!=-1){
result += new String(data,0,len);
}
}
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
}
}
}
5) 在AndroidManifest.xml文件中添加訪問網(wǎng)絡(luò)的權(quán)限,加入下面代碼:
6) 啟動模擬器AVD,運行該項目。在輸入框中輸入用戶名john,密碼123456,點擊注冊,提示服務(wù)器返回結(jié)果,如下圖所示:
7) 驗證Android App輸入的信息是否存儲到數(shù)據(jù)庫中。
打開MySQL5.5 Command Line Client,輸入密碼(安裝MySQL時設(shè)置的密碼),進入到mysql中后,輸入use apptest;表示使用該數(shù)據(jù)庫。輸入select * from user;表示從user表中查詢所有數(shù)據(jù),可以看到輸入如下圖所示:
此時可以看到在Android App端輸入的john和123456已經(jīng)存儲到數(shù)據(jù)庫MySQL中。我們的Android程序成功的通過Java Web應(yīng)用程序?qū)?shù)據(jù)處理后存儲到數(shù)據(jù)庫,并將錄入成功的結(jié)果返回給Android程序進行顯示。