String uriAPI = "http://192.168.1.100:8080/test/test.jsp"; //這是我測試的本地,大家可以隨意改 /*建立HTTPost對象*/ HttpPost httpRequest = new HttpPost(uriAPI); /* * NameValuePair實現請求參數的封裝 */ List <NameValuePair> params = new ArrayList <NameValuePair>(); params.add(new BasicNameValuePair("u", "沈大海")); params.add(new BasicNameValuePair("p", "123")); try { /* 添加請求參數到請求對象*/ httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); /*發送請求並等待響應*/ HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest); /*若狀態碼爲200 ok*/ if(httpResponse.getStatusLine().getStatusCode() == 200) { /*讀返回數據*/ String strResult = EntityUtils.toString(httpResponse.getEntity()); mTextView1.setText(strResult); } else { mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString()); } } catch (ClientProtocolException e) { mTextView1.setText(e.getMessage().toString()); e.printStackTrace(); } catch (IOException e) { mTextView1.setText(e.getMessage().toString()); e.printStackTrace(); } catch (Exception e) { mTextView1.setText(e.getMessage().toString()); e.printStackTrace(); } ////大家能根據這個代碼實現個android用戶登陸嗎?
Android開發中如何執行POST請求
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.