我是靠谱客的博主 疯狂水蜜桃,这篇文章主要介绍网络编程之get,post请求,文件上传及下载,现在分享给大家,希望可以做个参考。

* 1.发送GET请求
AsyncHttpClient.get(), 使用RequestParams传递数据
  * 2.发送POST请求
AsyncHttpClient.post(), 使用RequestParams传递数据
3.访问Webservice
Webservice就是别人写好的, 已经发布在网络上的公开的API, 应用程序中可以直接调用
4.上传
在RequestParams中添加一个File类型的字段, AsyncHttpClient就会执行上传操作
5.下载
请求下载路径, 使用FileAsyncHttpResponseHandler来处理下载文件
post请求代码:

复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.itheima.request; import java.io.File; import org.apache.http.Header; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.AsyncHttpResponseHandler; import com.loopj.android.http.RequestParams; public class MainActivity extends Activity { private EditText usernameET; private EditText passwordET; private EditText pathET; private AsyncHttpClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); usernameET = (EditText) findViewById(R.id.usernameET); passwordET = (EditText) findViewById(R.id.passwordET); pathET = (EditText) findViewById(R.id.pathET); client = new AsyncHttpClient(); } public void login(View v) throws Exception { String username = usernameET.getText().toString().trim(); String password = passwordET.getText().toString().trim(); String path = pathET.getText().toString().trim(); //带参数的请求; RequestParams params = new RequestParams(); params.put("username", username); // URLEncoder.encode() params.put("password", password); params.put("upload", new File(path)); // File类型会作为上传文件字段 //post请求,如果为get请求,则调用get方法; client.post("http://192.168.1.251:8080/WebServer/LoginServlet", params , new AsyncHttpResponseHandler() { public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) { Toast.makeText(getApplicationContext(), "登录成功", Toast.LENGTH_SHORT).show(); } public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) { Toast.makeText(getApplicationContext(), "登录失败 " + statusCode, Toast.LENGTH_SHORT).show(); error.printStackTrace(); } }); } }
复制代码
1
2
复制代码
1
文件下载代码:
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<pre name="code" class="java">package com.itheima.download; import java.io.File; import org.apache.http.Header; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; import android.widget.Toast; import com.loopj.android.http.AsyncHttpClient; import com.loopj.android.http.FileAsyncHttpResponseHandler; public class MainActivity extends Activity { private EditText pathET; private AsyncHttpClient client; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); pathET = (EditText) findViewById(R.id.pathET); client = new AsyncHttpClient(); } public void download(View v) { String path = pathET.getText().toString().trim(); //文件下载的保存地址; File file = new File("/mnt/sdcard/", path.substring(path.lastIndexOf("/") + 1)); client.get(path, new FileAsyncHttpResponseHandler(file) { // 处理文件传输的处理器 public void onSuccess(int statusCode, Header[] headers, File file) { Toast.makeText(getApplicationContext(), "下载成功", Toast.LENGTH_SHORT).show(); } public void onFailure(int statusCode, Header[] headers, Throwable throwable, File file) { Toast.makeText(getApplicationContext(), "下载失败 " + statusCode, Toast.LENGTH_SHORT).show(); throwable.printStackTrace(); } public void onProgress(int bytesWritten, int totalSize) { // 下载或上传的进度 System.out.println(bytesWritten + " / " + totalSize + " (" + bytesWritten * 100 / totalSize + "%)"); } }); } }


复制代码
1
复制代码
1
2





最后

以上就是疯狂水蜜桃最近收集整理的关于网络编程之get,post请求,文件上传及下载的全部内容,更多相关网络编程之get内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(59)

评论列表共有 0 条评论

立即
投稿
返回
顶部