历览千载书,时时见遗烈。这篇文章主要讲述android-数据存储之远程服务器存储相关的知识,希望能为你提供帮助。
待续....
public class MainActivity extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.textView1);
}
【android-数据存储之远程服务器存储】
public void testGet(View v) throws Exception{
final ProgressDialog dialog=ProgressDialog.show(this, null, "正在请求中...");
new Thread(){
public void run() {
String path="http://192.168.56.1:8080/Web_Server/index.jsp?name=tom&
age=23";
URL url;
try {
url = new URL(path);
HttpURLConnection huc= (HttpURLConnection)url.openConnection();
huc.setRequestMethod("GET");
huc.setConnectTimeout(5000);
huc.setReadTimeout(6000);
huc.connect();
//发送请求
int responseCode=huc.getResponseCode();
if (responseCode==200) {
InputStream is=huc.getInputStream();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=-1;
while((len=is.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
final String reault=baos.toString();
baos.close();
is.close();
//在主线程更新ui
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(reault);
dialog.dismiss();
}
});
//断开连接
huc.disconnect();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
dialog.dismiss();
}
}
}.start();
}
public void testPost1(View v){
final ProgressDialog dialog=ProgressDialog.show(this, null, "正在请求中...");
new Thread(){
public void run() {
String path="http://192.168.56.1:8080/Web_Server/index.jsp";
URL url;
try {
url = new URL(path);
HttpURLConnection huc= (HttpURLConnection)url.openConnection();
huc.setRequestMethod("POST");
huc.setConnectTimeout(5000);
huc.setReadTimeout(6000);
huc.connect();
OutputStream os=huc.getOutputStream();
String data="https://www.songbingjia.com/android/name=tom5&
age=11";
os.write(data.getBytes("utf-8"));
//发送请求
int responseCode=huc.getResponseCode();
if (responseCode==200) {
InputStream is=huc.getInputStream();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=-1;
while((len=is.read(buffer))!=-1){
baos.write(buffer, 0, len);
}
final String reault=baos.toString();
baos.close();
is.close();
//在主线程更新ui
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(reault);
dialog.dismiss();
}
});
//断开连接
huc.disconnect();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
dialog.dismiss();
}
}
}.start();
}
public void testGet2(View v) throws Exception{
final ProgressDialog dialog=ProgressDialog.show(this, null, "正在请求中...");
new Thread(){
public void run() {
String path="http://192.168.56.1:8080/Web_Server/index.jsp?name=tom&
age=23";
try {
//创建Client对象
HttpClient httpClient=new DefaultHttpClient();
//设置超时
HttpParams params=httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);
//创建请求对象
HttpGet request=new HttpGet(path);
//执行请求对象
HttpResponse response=httpClient.execute(request);
int statusCode=response.getStatusLine().getStatusCode();
if (statusCode==200) {
//得到响应文本
HttpEntity entity=response.getEntity();
final String result=EntityUtils.toString(entity);
//在主线程更新ui
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(result);
dialog.dismiss();
}
});
}
//断开连接
httpClient.getConnectionManager().shutdown();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
dialog.dismiss();
}
}
}.start();
}
public void testPost2(View v){
final ProgressDialog dialog=ProgressDialog.show(this, null, "正在请求中...");
new Thread(){
public void run() {
String path="http://192.168.56.1:8080/Web_Server/index.jsp";
try {
//创建Client对象
HttpClient httpClient=new DefaultHttpClient();
//设置超时
HttpParams params=httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, 5000);
HttpConnectionParams.setSoTimeout(params, 5000);
//创建请求对象
HttpPost request=new HttpPost(path);
//设置请求体
List<
BasicNameValuePair>
parameters=new ArrayList<
>
();
parameters.add(new BasicNameValuePair("name", "tom4"));
parameters.add(new BasicNameValuePair("age", "14"));
HttpEntity entity=new UrlEncodedFormEntity(parameters);
request.setEntity(entity);
//执行请求对象
HttpResponse response=httpClient.execute(request);
int statusCode=response.getStatusLine().getStatusCode();
if (statusCode==200) {
//得到响应文本
HttpEntity entity2=response.getEntity();
final String result=EntityUtils.toString(entity2);
//在主线程更新ui
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(result);
dialog.dismiss();
}
});
}
//断开连接
httpClient.getConnectionManager().shutdown();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
dialog.dismiss();
}
}
}.start();
}
}
推荐阅读
- 极品手机推荐安卓3G运存16G内存,相机1300+500,三星高画质显示J7109|J7108
- android开发系列之aidl
- android批量文件上传(android批量图片上传)
- Android 5.0 Camera系统源码分析(Camera预览3A流程)
- Android应用开发性能优化完全分析
- android Drawable Resource学习
- Android 使用Facebook的 Stetho工具
- 是时候来了解android7了:shortcuts(快捷方式)
- Android问题-XE5提示"[DCC Fatal Error] Project1.dpr: F1027 Unit not found: 'System.pas' o