編輯:關於Android編程
最近正在做Android網絡應用的開發,使用了android網絡請求方面的知識,現在向大家介紹網絡請求方面的知識,我們知道android中向服務器端發送一個請求,(這就是我們通常所說的POST請求),我們要發送一個完整的URL,然後服務器端接收到這個URL,對這個URL進行特定的解析,就是對URL進行解析,轉化為JSON數據,然後,我們只要處理這個JSON數據就可以了。

我現在就用我的項目實例來體現解析URL的過程:
1、組裝URL的過程:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PHByZSBjbGFzcz0="brush:java;">private String getOrderPayUrl(int order, int action, String accountid,
String token) {
Calendar calendar = Calendar.getInstance();
long time = calendar.getTimeInMillis() / 1000;
return orderPayUrl + "?action=" + action + "&time=" + time
+ "&accountid=" + accountid + "&token=" + token + "&paymoney="
+ order + "¤cy=CNY&" + "sign="
+ getSign(action, time, accountid);
}
2、發送URL的過程:
private void httpRequest(String url, int which, String method) {
HttpRequestTask task = new HttpRequestTask(mHandler, url, which, method);
task.startTask();
}public class HttpRequestTask implements Runnable {
private Handler handler;
private String url;
private int which;
private String method;
public HttpRequestTask(Handler handler, String url, int which, String method) {
this.url = url;
this.handler = handler;
this.which = which;
this.method = method;
}
public void startTask() {
new Thread(this).start();
}
@Override
public void run() {
Looper.prepare();
sendRequest();
}
private void sendRequest() {
String result = null;
if (method != null && method.equals(MyConstant.POST)) {
result = HttpUtil.queryStringForPost(url);
}
if (method != null && method.equals(MyConstant.GET)) {
result = HttpUtil.queryStringForGet(url);
}
// Log.e("---result---", result);
Message msg = Message.obtain();
msg.what = which;
msg.obj = result;
handler.sendMessage(msg);
}
}
// 發送Post請求,獲得響應查詢結果
public static String queryStringForPost(String url) {
HttpPost request = HttpUtil.getHttpPost(url);
String result = null;
try {
// 獲得響應對象
HttpResponse response = HttpUtil.getHttpResponse(request);
if (response.getStatusLine().getStatusCode() == 200) {
result = EntityUtils.toString(response.getEntity());
return result;
}
} catch (ClientProtocolException e) {
e.printStackTrace();
result = "網絡異常!";
return result;
} catch (IOException e) {
e.printStackTrace();
result = "網絡異常!";
return result;
}
return result;
}// // 獲得post請求對象request
public static HttpPost getHttpPost(String url) {
// 去除空格
// if (url != null) {
// Pattern p = Pattern.compile("\\s");
// Matcher m = p.matcher(url);
// url = m.replaceAll("");
// }
HttpPost request = new HttpPost(url);
return request;
}這實際上是一個比較清晰的流程,其中也可以看出多線程處理的模式。
一旦我們需要網絡請求的時候,我們一般會將網絡請求的處理部分放在子線程中,另外開一個線程,這樣就不會在原線程中處理過多的事情,這也減輕了主線程的壓力。
C#程序員學習Android開發系列之按鈕事件的4種寫法
經過前兩篇blog的鋪墊,我們今天熱身一下,做個簡單的例子。目錄結構還是引用上篇blog的截圖。具體實現代碼:public class MainActivity exte
微信怎麼群發消息 微信群發信息教程
現在大家很少用短信和飛信發信息了,自從微信出現後,微信可以說已經慢慢替代了短信,飛信,QQ等通信方式,很多朋友都選擇使用微信和朋友們溝通和交流,在節假日的時
Android 手把手帶你玩轉自定義相機
概述相機幾乎是每個APP都要用到的功能,萬一老板讓你定制相機方不方?反正我是有點方。關於相機的兩天奮斗總結免費送給你。 Intent intent = new Inte
Android使用自己封裝的Http和Thread、Handler實現異步任務
目錄結構如下:Http協議的封裝:使用http協議有request和response這兩個主要的域,下邊是Http協議封裝的結構圖(1)HttpRequestInter.