android訪問servlet
① android客戶端和伺服器端怎麼交互
android客戶端和伺服器端是基於IntentService的,具體如下:
後台使用簡單的servlet,支持GET或POST。這個servlet最終返回給前台一個字元串flag,值是true或false,表示登錄是否成功。
然後在安卓的ADT上創建一個安卓項目,建立兩個Activity,分別作為登錄界面和登錄成功界面。
HTTP的訪問公共類,用於處理GET和POST請求。
IntentService服務,用於在後台以隊列方式處理耗時操作。
在AndroidManifest.xml中注冊IntentService。注意uses-permission節點,為程序開啟訪問網路的許可權。
登陸界面處理,注意按鈕監聽事件中,使用Intent將要傳遞的值傳給service。接收廣播類中,同樣使用Intent將要傳遞的值傳給下一個Activity。在onCreate()中,動態注冊接收廣播類的實例receiver。在接收廣播類中,不要使用完畢後忘記注銷接收器,否則會報一個Are you missing a call to unregisterReceiver()? 的異常。
② java服務端android客戶端如何通信
一、HTTP請求(APACHE的HttpClient實現)
伺服器端,就是普通的servlet、Strutus2就可以
移動端
protected static String get(String url, List<NameValuePair> params) {
String resultMsg;
// 設置http請求配置
HttpParams parms = new BasicHttpParams();
parms.setParameter("charset", HTTP.UTF_8);
// 配置連接超時
HttpConnectionParams.setConnectionTimeout(parms, 10 * 1000);
// 設置請求超時
HttpConnectionParams.setSoTimeout(parms, 15 * 1000);
// 實例化HttpClient
HttpClient httpclient = new DefaultHttpClient(parms);
// 實例化HttpGet
HttpGet httpget = new HttpGet(url);
// 設置請求頭
httpget.addHeader("Content-Type", "application/json");
httpget.addHeader("charset", HTTP.UTF_8);
try {
if (params.size() > 0)
url = url + "?" + URLEncodedUtils.format(params, HTTP.UTF_8);
HttpResponse resp = httpclient.execute(httpget);
int statusCode = resp.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
StringBuffer result = getResponse(resp);
resultMsg = result.toString();
} else {
resultMsg = "連接異常";
}
} catch (Exception e) {
resultMsg = "連接異常";
} finally {
// 關閉get
httpget.abort();
// 關閉連接 ,釋放資源
httpclient.getConnectionManager().shutdown();
}
return resultMsg;
}
protected static String post(String uri, Object params) {
String resultMsg;
// 設置http請求配置
HttpParams hp = new BasicHttpParams();
hp.setParameter("charset", HTTP.UTF_8);
// 配置連接超時
HttpConnectionParams.setConnectionTimeout(hp, 10 * 1000);
HttpConnectionParams.setSoTimeout(hp, 15 * 1000);
// 實例化HttpClient
HttpClient httpclient = new DefaultHttpClient(hp);
// 實例化HttpPost請求
HttpPost httppost = new HttpPost(uri);
// 設置頭信息
httppost.addHeader("Content-Type", "application/json");
httppost.addHeader("charset", HTTP.UTF_8);
try {
// 將參數進行json化
ObjectMapper mapper = new ObjectMapper();
String jsonStr = mapper.writeValueAsString(params);
Log.i(TAG, "URI=" + uri + ",BEAN=" + jsonStr);
// 定義消息實體
StringEntity se = new StringEntity(jsonStr, HTTP.UTF_8);
httppost.setEntity(se);
// 通信
HttpResponse resp = httpclient.execute(httppost);
int statusCode = resp.getStatusLine().getStatusCode();
Log.i(TAG, "StatusCode=" + statusCode);
if (statusCode == HttpStatus.SC_OK) {
StringBuffer result = getResponse(resp);
resultMsg = result.toString();
} else {
resultMsg = "連接異常";
}
} catch (Exception e) {
e.printStackTrace();
resultMsg = "連接異常";
} finally {
// 關閉get
httppost.abort();
// 關閉連接 ,釋放資源
httpclient.getConnectionManager().shutdown();
}
Log.i(TAG, resultMsg);
return resultMsg;
}
二、SOCKET連接
伺服器端:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class service_java_test {
public static void main(String[] args) throws IOException {
ServerSocket server = new ServerSocket(10000); //綁定的埠號
Socket socket = server.accept(); //連接不成功以至於下一行的"連接成功"
//在調試區顯示不出來
System.out.println("連接成功");
BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
PrintWriter out = new PrintWriter(socket.getOutputStream());
while (true) {
String msg = in.readLine();
System.out.println(msg);
out.println("Server received " + msg); //向接收方發送已接受到了的語句
out.flush();
if (msg.equals("bye")) { //若接收到"bye"則break
break;
}
}
socket.close();
}
}
安卓客戶端:
package com.example.t4_android;
import java.net.Socket;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
private TextView myTextView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myTextView = (TextView) findViewById(R.id.textView1);
Thread t = new Thread(new Runnable(){
public void run(){
try {
Socket sk = new Socket("192.168.253.1", 10000);//綁定套接字,這一行一直執行不成功
//以至於下一行在安卓頁面的TextView上
//不顯示「已連接」的字樣
//"192.168.253.1"是我利用DOS命令查找
//的本機IP
myTextView.setText("已連接");
} catch (Exception e) {
e.printStackTrace();
}
}
});
t.start();
}
}
③ android 連接webservice問題.急急急急急急
目測是com.ctc.wstx.exc.WstxIOException這宏尺個異常,有null值。還有客困如戶汪絕啟端中斷