andorid 远程存储中JDK和Volley的GET和POST方法

时间:2021-01-01 05:41:53

在操作Volley的时候先添加volley.jar(包)

<uses-permission android:name="android.permission.INTERNET"/>(权限)

wep.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hanqi.application3.WebActivity"
android:orientation="vertical"> <EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et_5"
android:hint="url地址"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="JDK的get方式"
android:onClick="bt55_onclick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="JDK的pose方式"
android:onClick="bt66_onclick"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Android的get方式"
android:onClick="bt77_onclick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Android的pose方式"
android:onClick="bt88_onclick"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Volley的get方式"
android:onClick="bt99_onclick"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Volley的pose方式"
android:onClick="bt100_onclick"/>
</LinearLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="100dp"
android:id="@+id/et_6"/>
</LinearLayout>

WepActivity.java

package com.hanqi.application3;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast; import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley; import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.Map; public class WebActivity extends AppCompatActivity { EditText et5;
EditText et6;
RequestQueue requestQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web); et5 = (EditText)findViewById(R.id.et_5);
et5.setText("http://192.168.0.101:8080/TestWeb/index.jsp");
et6 = (EditText)findViewById(R.id.et_6);
//获取Volley的请求队列
requestQueue = Volley.newRequestQueue(this); }
//1.在子线程访问网络
//2.提供进度对话框
//3.设置权限
public void bt66_onclick(View v)
{
//1.显示ProgressDialog进度对话框
final ProgressDialog dialog = ProgressDialog.show(this,null,"请求中...");
//2.访问网络,启动分线程
new Thread() {
//3. 在分线程, 发送请求, 得到响应数据
@Override
public void run() { try {
//1-URL
URL url = new URL(et5.getText().toString());
//2-URL获取链接
HttpURLConnection huc= (HttpURLConnection)url.openConnection();
//请求方式
huc.setRequestMethod("POST");
//设置超时
huc.setConnectTimeout(5000);
huc.setReadTimeout(5000);
//链接并发送请求
huc.connect();
//用输出流设置请求体
OutputStream os = huc.getOutputStream();
String str = "name=Rose";
os.write(str.getBytes("UTF-8")); //接收:
//判断返回状态码 200
int code = huc.getResponseCode(); if(code == 200)
{
//接收数据 //输入流:
InputStream is = huc.getInputStream();
//读取流
//1-byte数据
byte[] b= new byte[1024];
//2-读到数组的长度
int i = 0;
//3-数据
final StringBuffer sd1 = new StringBuffer();
while ((i=is.read(b))>0)
{
sd1.append(new String(b,0,i));
}
is.close();
huc.disconnect();
//释放资源
//通过主线程信息和关闭对话框
runOnUiThread(new Runnable() {
@Override
public void run() {
et6.setText(sd1); dialog.dismiss();
}
}); }
else {
Toast.makeText(WebActivity.this, "连接错误,返回的状态码="+code, Toast.LENGTH_SHORT).show();
}
os.close(); } catch (Exception e) {
e.printStackTrace(); dialog.dismiss(); }
}
}.start();
}
public void bt55_onclick(View v)
{
//1.显示ProgressDialog进度对话框
final ProgressDialog dialog = ProgressDialog.show(this,null,"请求中...");
//2.访问网络,启动分线程
new Thread() {
//3. 在分线程, 发送请求, 得到响应数据
@Override
public void run() { try {
//1-URL
URL url = new URL(et5.getText().toString() + "?name=Tom1&age=11");
//2-URL获取链接
HttpURLConnection huc= (HttpURLConnection)url.openConnection();
//请求方式
huc.setRequestMethod("GET");
//设置超时
huc.setConnectTimeout(3000);
huc.setReadTimeout(3000);
//链接并发送请求
huc.connect();
//接收:
//判断返回状态码 200
int code = huc.getResponseCode(); if(code == 200)
{
//接收数据 //输入流:
InputStream is = huc.getInputStream();
//读取流
//1-byte数据
byte[] b= new byte[1024];
//2-读到数组的长度
int i = 0;
//3-数据
final StringBuffer sd1 = new StringBuffer();
while ((i=is.read(b))>0)
{
sd1.append(new String(b,0,i));
}
is.close();
huc.disconnect();
//释放资源
//通过主线程信息和关闭对话框
runOnUiThread(new Runnable() {
@Override
public void run() {
et6.setText(sd1); dialog.dismiss();
}
}); }
else {
Toast.makeText(WebActivity.this, "连接错误,返回的状态码="+code, Toast.LENGTH_SHORT).show();
}
//接收数据 } catch (Exception e) {
e.printStackTrace(); dialog.dismiss(); }
}
}.start();
}
public void bt77_onclick(View v)
{
final ProgressDialog dialog = ProgressDialog.show(this,null,"请求中..."); }
public void bt99_onclick(View v)
{
final ProgressDialog dialog = ProgressDialog.show(this,null,"请求中...");
//构造Volley的请求对象
StringRequest sr = new StringRequest(et5.getText().toString() + "?name=jack", new Response.Listener<String>() {
@Override
public void onResponse(String s) {
et6.setText(s);
dialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(WebActivity.this, "访问出现异常", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
//把请求对象放入队列
requestQueue.add(sr);
}
public void bt100_onclick(View v)
{
final ProgressDialog dialog = ProgressDialog.show(this,null,"请求中...");
//构造Volley的请求对象
StringRequest sr = new StringRequest(Request.Method.POST,et5.getText().toString(), new Response.Listener<String>() {
@Override
public void onResponse(String s) {
et6.setText(s);
dialog.dismiss();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(WebActivity.this, "访问出现异常", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
}){
//重写设置参数的方法
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> m = new HashMap<String,String>();
m.put("name","Volley");
return m;
}
};
//把请求对象放入队列
requestQueue.add(sr);
}
}