是时候来简单网络请求了
1.通过HttpUrlConnection分别进行get请求post请求
//请求前先加上这个,防止忘记
<uses-permission android:name="android.permission.INTERNET"/>
package com.heima.demohttp.myhttp;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
/**
* Created by zhouzhou on 2016/8/2.
*
*/
public class MyUrlConnection extends Thread {
String url;
Handler handler;
Context context;
String data;
public MyUrlConnection(String url, Context context, Handler handler) {
this.url = url;
this.context = context;
this.handler = handler;
}
public MyUrlConnection(String url, Context context, Handler handler,String data) {
this.url = url;
this.context = context;
this.handler = handler;
this.data =data;
}
@Override
public void run() {
post();
}
public void post(){
try {
URL myUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) myUrl.openConnection();
connection.setRequestMethod("POST");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setDoOutput(true);
OutputStream os = connection.getOutputStream();
os.write(data.getBytes());
os.close();
connection.connect();
int code = connection.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) { //请求成功
InputStream is = connection.getInputStream();
byte[] bs = new byte[1024];
int len = is.read(bs);
String str = new String(bs, 0, len);
Message message = Message.obtain();
message.obj = str;
message.what = 100;
handler.sendMessage(message);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public void get(){
try {
URL myUrl = new URL(url);
HttpURLConnection connection = (HttpURLConnection) myUrl.openConnection();
connection.setRequestMethod("GET");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.connect();
int code = connection.getResponseCode();
if (code == HttpURLConnection.HTTP_OK) { //请求成功
InputStream is = connection.getInputStream();
byte[] bs = new byte[1024];
int len = is.read(bs);
String str = new String(bs, 0, len);
Message message = Message.obtain();
message.obj = str;
message.what = 100;
handler.sendMessage(message);
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
MainActivity
package com.heima.demohttp;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.provider.Settings;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.heima.demohttp.myhttp.MyOkHttp;
import com.heima.demohttp.myhttp.MyUrlConnection;
public class MainActivity extends AppCompatActivity {
private EditText mUsername;
private EditText mPassword;
private static final String TAG = "MainActivity";
private Handler handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what==100){
String str = (String) msg.obj;
Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//http://10.0.155.30:8080/day14/AServlet?my_file=&username=zhouzhou&password=123
mUsername = (EditText) findViewById(R.id.username);
mPassword = (EditText) findViewById(R.id.password);
Log.e(TAG, "onCreate" + "=======" + "MainActivity" + "=======" + Thread.currentThread().getName() + "=======");
}
public void click(View view){
String username = mUsername.getText().toString().trim();
String password = mPassword.getText().toString().trim();
String url ="http://10.0.155.30:8080/day14/AServlet";
String data = "my_file=&username="+username+"&password="+password;
//get请求
// new MyUrlConnection(url,this,handler).start();
// new MyUrlConnection(url,this,handler,data).start();
// new MyOkHttp(this,handler,url).start();
new MyOkHttp(this,handler,url,username,password).start();
// runOnUiThread(new Runnable() {
// @Override
// public void run() {
// Log.e(TAG, "runOnUiThread" + "=======" + "MainActivity" + "=======" + Thread.currentThread().getName() + "=======");
// SystemClock.sleep(5000);
// Toast.makeText(MainActivity.this, "可以更新ui", Toast.LENGTH_SHORT).show();
// }
// });
}
}
okhttp
package com.heima.demohttp.myhttp;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
/**
* Created by 周周 on 2016/8/2.
*
*/
public class MyOkHttp extends Thread{
private Handler handler;
private String url;
private Context context;
String username;
String password;
public MyOkHttp(Context context,Handler handler, String url) {
this.handler = handler;
this.url = url;
this.context =context;
}
public MyOkHttp(Context context,Handler handler, String url,String username,String password) {
this.handler = handler;
this.url = url;
this.context =context;
this.username = username;
this.password =password;
}
@Override
public void run() {
forPost();
}
public void forPost(){
OkHttpClient client = new OkHttpClient();
FormBody.Builder formBody = new FormBody.Builder();
formBody.add("username",username);
formBody.add("password",password);
RequestBody requestBody = formBody.build();
Request request = new Request.Builder().post(requestBody).url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String str = response.body().string();
Message message = Message.obtain();
message.obj =str;
message.what=100;
handler.sendMessage(message);
}
});
}
public void forGet(){
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder().get().url(url).build();
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
String str = response.body().string();
Message message = Message.obtain();
message.obj =str;
message.what=100;
handler.sendMessage(message);
}
});
}
}
get和post提交就到这把,上传文件,自己不会搭建,就有点坑,下次把,自己学到的,多线程断点续传,试试,复习还是蛮爽的,哈哈