2014年10月20日 星期一

淘汰的Network 用法

HTTP GET
public static void Setdata(String urlStr) { Log.i("Ten", "Device Edit Done"); HttpUriRequest request = new HttpGet(urlStr); // Or HttpPost(), depends // on your needs String credentials = account_password; String base64EncodedCredentials = Base64.encodeToString(credentials.getBytes(), Base64.NO_WRAP); request.addHeader("Authorization", "Basic " + base64EncodedCredentials); httpclient = new DefaultHttpClient(); try { HttpResponse response = httpclient.execute(request); HttpEntity httpEntity = response.getEntity(); String state = EntityUtils.toString(httpEntity); Log.i("Ten", "Ten: " + state); } catch (ClientProtocolException e) { Log.i("Ten", "ClientProtocolException:" + e); e.printStackTrace(); } catch (IOException e) { Log.i("Ten", "IOException:" + e); e.printStackTrace(); } }



HTTP POST

public static String HttpPing(String urlStr) {
String responseBody = "";
try {
String credentials = account_password;
String encoding = Base64.encodeToString(credentials.getBytes(),
Base64.NO_WRAP);
HttpPost httppost = new HttpPost(urlStr);
httppost.setHeader("Authorization", "Basic " + encoding);
List<NameValuePair> postParams;
postParams = new ArrayList<NameValuePair>();
postParams.add(new BasicNameValuePair("GetJson", ""));

HttpClient httpclient = new DefaultHttpClient();
HttpResponse response;

UrlEncodedFormEntity urf = new UrlEncodedFormEntity(postParams,
HTTP.UTF_8);
httppost.setEntity(urf);
response = httpclient.execute(httppost);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
responseBody = EntityUtils.toString(response.getEntity());
}
} catch (ClientProtocolException e) {

} catch (IOException e) {

}
//Log.i("Network reponse:",responseBody);
return responseBody;
}

沒有留言:

張貼留言

有任何疑問歡迎寄信給我,
但垃圾訊息我會刪光喔!