編輯:關於Android編程
[java]
package com.example.h3c.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.util.Log;
import com.example.pickride.util.ConstantsTable;
public class HttpConnection {
private final int TIMEOUT = 3000;// 設定超時時間
/**
* 請求服務器響應
*
* @param requestType
* 請求類型 requestContent [類型,參數,類型,參數]
*/
public String requestService(String requestType, String... requestContent) {
String resultData = "";
InputStreamReader in = null;
HttpURLConnection urlConn = null;
BufferedReader buffer = null;
try {
StringBuffer sBuffer = new StringBuffer();
for (int n = 0; n < requestContent.length; n++) {
if ((n + 1) % 2 == 0) {
sBuffer.append("=" + requestContent[n] + "&");
} else {
sBuffer.append(requestContent[n]);
}
}
URL url = new URL(ConstantsTable.URI + requestType + "?" + sBuffer);
if (url != null) {
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(TIMEOUT);// 設置超時時間
try {
in = new InputStreamReader(urlConn.getInputStream());
} catch (ConnectException e) {
Log.e(ConstantsTable.LOG_TAG, "服務器宕機了...");
return resultData;
}
buffer = new BufferedReader(in);
String inputLine = null;
while ((inputLine = buffer.readLine()) != null) {
resultData += inputLine + "\n";
}
urlConn.disconnect();
if (ConstantsTable.DEBUG_FLAG) {
Log.d(ConstantsTable.LOG_TAG, resultData);
}
}
} catch (MalformedURLException e) {
Log.e(ConstantsTable.LOG_TAG, "域名無法解析");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (buffer != null) {
buffer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return resultData;
}
}
package com.example.h3c.net;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.util.Log;
import com.example.pickride.util.ConstantsTable;
public class HttpConnection {
private final int TIMEOUT = 3000;// 設定超時時間
/**
* 請求服務器響應
*
* @param requestType
* 請求類型 requestContent [類型,參數,類型,參數]
*/
public String requestService(String requestType, String... requestContent) {
String resultData = "";
InputStreamReader in = null;
HttpURLConnection urlConn = null;
BufferedReader buffer = null;
try {
StringBuffer sBuffer = new StringBuffer();
for (int n = 0; n < requestContent.length; n++) {
if ((n + 1) % 2 == 0) {
sBuffer.append("=" + requestContent[n] + "&");
} else {
sBuffer.append(requestContent[n]);
}
}
URL url = new URL(ConstantsTable.URI + requestType + "?" + sBuffer);
if (url != null) {
urlConn = (HttpURLConnection) url.openConnection();
urlConn.setConnectTimeout(TIMEOUT);// 設置超時時間
try {
in = new InputStreamReader(urlConn.getInputStream());
} catch (ConnectException e) {
Log.e(ConstantsTable.LOG_TAG, "服務器宕機了...");
return resultData;
}
buffer = new BufferedReader(in);
String inputLine = null;
while ((inputLine = buffer.readLine()) != null) {
resultData += inputLine + "\n";
}
urlConn.disconnect();
if (ConstantsTable.DEBUG_FLAG) {
Log.d(ConstantsTable.LOG_TAG, resultData);
}
}
} catch (MalformedURLException e) {
Log.e(ConstantsTable.LOG_TAG, "域名無法解析");
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
if (buffer != null) {
buffer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return resultData;
}
}
首先是配置文件: 活動中的代碼如下: package com.itydl.rwinrom;import jav
在寫博客園客戶端的時候,突然想到,弄個知乎日報風格的簡單清爽多好!不需要那麼多繁雜的信息干擾視野。先貼上效果圖,左邊是知乎日報的,右邊是本方案的 本文所使用的ide是an
昨天在用360掃描應用漏洞時,掃描結果,出來一個android:exported屬性,其實之前根本不知道這個屬性,更不知道這個屬性用來干嘛
接著上篇文章Android JNI/NDK開發之基本姿勢<一>,今天我們講講怎麼調用native方法,以及native調用java層方法,上篇文章我們已經完成