Android 连接 就是 Java 连接,一般用Socket,网上关于Java网络编程的例子很多,给你几个连接,可以看看。http://blog.csdn.net/hbcui1984/archive/2006/12/12/1439880.aspx关于Java IO流的
http://blog.csdn.net/xiaoenxiaohui8/archive/2009/05/04/4145866.aspx
http://blog.csdn.net/liu251/archive/2009/02/03/3860578.aspx
http://blog.csdn.net/xiaoenxiaohui8/archive/2009/05/04/4145866.aspx
http://blog.csdn.net/liu251/archive/2009/02/03/3860578.aspx
HttpClient httpClient = new DefaultHttpClient();
// HttpContext localContent = new BasicHttpContext();
HttpGet httpGet = new HttpGet(GOOGLE_API_URL + city);
try {
// HttpResponse response = httpClient.execute(httpGet, localContent);
HttpResponse response = httpClient.execute(httpGet);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
Log.e(TAG, "Failed. status code is not ok.");
httpGet.abort();
} else {
// Get weather information successful
// Reset charset for Chinese
HttpEntity httpEntity = response.getEntity();
String string = EntityUtils.toString(httpEntity, "utf-8").trim();
InputStream is = new ByteArrayInputStream(string.getBytes());
return parseWeather(is);
}
} catch (Exception e) {
Log.e(TAG, "Failed to get weather information.", e);
e.printStackTrace();
} finally {
// disconnect network
httpClient.getConnectionManager().shutdown();
}
// CMWAP连接方式
if (connection.Type == 1)
{
URL url = new URL(urlString);
httpUrlConnection = (HttpURLConnection) url.openConnection();
httpUrlConnection.setRequestProperty("X-Online-Host", task.url
);
httpUrlConnection.setRequestProperty("Accept", "*/*");
}
// CMNET连接方式
else
{ String newUrl = geturl();
URL url = new URL(newUrl); httpUrlConnection = (HttpURLConnection) url.openConnection(); }
httpUrlConnection.setRequestProperty("Accept", "*/*");
httpUrlConnection.setRequestProperty("Pragma", "No-cache");
httpUrlConnection.setRequestProperty("Cache-Control", "no-cache");
httpUrlConnection.setRequestProperty("connection", "keep-alive");
httpUrlConnection.setRequestProperty("accept-charset", "utf-8"); httpUrlConnection.setRequestProperty("User-Agent", "Mozilla/5.0"); httpUrlConnection.setRequestProperty("x-up-calling-line-id",
"13800000");
// POST方式
if (conntion.data != null)
{
httpUrlConnection.setDoInput(true);
httpUrlConnection.setDoOutput(true);
httpUrlConnection.setRequestMethod("POST");
httpUrlConnection.setUseCaches(false);
httpUrlConnection.setInstanceFollowRedirects(false);
httpUrlConnection.setRequestProperty("Content-Type",
"application/xml");
PrintWriter out = new PrintWriter(httpUrlConnection
.getOutputStream());
out.print(connection.data);
out.flush();
out.close(); }
// GET方式
else
{
httpUrlConnection.setRequestMethod("GET");
} // 获取HTTP请求返回码
int code = httpUrlConnection.getResponseCode();
// time.cancel(); if (code != HttpURLConnection.HTTP_OK)
{
return null;
}