mooc课程精选,成品人片观看入口众乐乐,久久久久人妻一区精品性色av,苍兰诀大结局是什么,白丝美女被狂躁免费视频网站

當(dāng)前位置:首頁(yè) > 嵌入式培訓(xùn) > 嵌入式學(xué)習(xí) > 講師博文 > Anddroid App和Java Web服務(wù)器間數(shù)據(jù)交互 之Android App搭建

Anddroid App和Java Web服務(wù)器間數(shù)據(jù)交互 之Android App搭建 時(shí)間:2018-09-28      來(lái)源:未知

創(chuàng)建Android App程序,提交數(shù)據(jù)給Java Web服務(wù)器,并得到Java Web服務(wù)器的返回結(jié)果。

1) 安裝eclipse集成Android開(kāi)發(fā)環(huán)境(包括sdk和adt,本文不再詳述)。

2) 創(chuàng)建Android App項(xiàng)目。點(diǎn)擊File->New->Android Application Project,在頁(yè)面中Application Name對(duì)應(yīng)的輸入框中輸入MyApp項(xiàng)目名稱,點(diǎn)擊Next,后點(diǎn)擊Finish。如下圖所示:

3) 布局文件activity_main.xml中創(chuàng)建EditText輸入框,可以輸入用戶名和密碼,創(chuàng)建Button按鈕,用于注冊(cè),點(diǎn)擊該按鈕將輸入的用戶名和密碼提交給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="注冊(cè)" />

 

4) 在MainActivity.java文件中,響應(yīng)按鈕的點(diǎn)擊事件,在響應(yīng)事件中啟動(dò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);

}

// 點(diǎn)擊注冊(cè)按鈕觸發(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 = new ArrayList();

paramList.add(new BasicNameValuePair("name", nameInput));//第一個(gè)參數(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èn)網(wǎng)絡(luò)的權(quán)限,加入下面代碼:

6) 啟動(dòng)模擬器AVD,運(yùn)行該項(xiàng)目。在輸入框中輸入用戶名john,密碼123456,點(diǎn)擊注冊(cè),提示服務(wù)器返回結(jié)果,如下圖所示:

7) 驗(yàn)證Android App輸入的信息是否存儲(chǔ)到數(shù)據(jù)庫(kù)中。

打開(kāi)MySQL5.5 Command Line Client,輸入密碼(安裝MySQL時(shí)設(shè)置的密碼),進(jìn)入到mysql中后,輸入use apptest;表示使用該數(shù)據(jù)庫(kù)。輸入select * from user;表示從user表中查詢所有數(shù)據(jù),可以看到輸入如下圖所示:

此時(shí)可以看到在Android App端輸入的john和123456已經(jīng)存儲(chǔ)到數(shù)據(jù)庫(kù)MySQL中。我們的Android程序成功的通過(guò)Java Web應(yīng)用程序?qū)?shù)據(jù)處理后存儲(chǔ)到數(shù)據(jù)庫(kù),并將錄入成功的結(jié)果返回給Android程序進(jìn)行顯示。

上一篇:Anddroid App和Java Web服務(wù)器間數(shù)據(jù)交互 之JavaWeb服務(wù)器搭建

下一篇:typedef分析理解

熱點(diǎn)文章推薦
華清學(xué)員就業(yè)榜單
高薪學(xué)員經(jīng)驗(yàn)分享
熱點(diǎn)新聞推薦
前臺(tái)專線:010-82525158 企業(yè)培訓(xùn)洽談專線:010-82525379 院校合作洽談專線:010-82525379 Copyright © 2004-2022 北京華清遠(yuǎn)見(jiàn)科技集團(tuán)有限公司 版權(quán)所有 ,京ICP備16055225號(hào)-5,京公海網(wǎng)安備11010802025203號(hào)

回到頂部