HttpURLConnection的GET和POST请求方式
2017-07-20 10:23
633 查看
//使用GET简单获取数据
public class MainActivity extends AppCompatActivity {
ImageView image;
File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
new Thread(){
@Override
public void run() {
try {
//创建url对象
URL url = new URL("http://172.17.29.120/localuser/chenhao/img1.png");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//设置请求 方式 连接超时 读取超时
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(10000);
urlConnection.setReadTimeout(10000);
//得到返回码
int code = urlConnection.getResponseCode();
if ( code== 200) {
//获取流
InputStream is = urlConnection.getInputStream();
//文件方式
file= new File(Environment.getExternalStorageDirectory().getPath(), "copy.jpg");
FileOutputStream fos = new FileOutputStream(file);
int len = 0;
byte[] b = new byte[1024];
while ((len = is.read(b)) != -1) {
fos.write(b, 0, len);
}
············//这里用的 decodeFile方法 可以直接用decodeStrean直接传一个 inputStream 就可以了
fos.close();
is.close();
final Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
//主线程 更改UI
runOnUiThread(new Runnable() {
@Override
public void run() {
image.setImageBitmap(bitmap);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}
//使用Post简单提交数据
package com.example.administrator.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class MainActivity extends AppCompatActivity {
private String path="http://123.206.70.44:8080/JavaWebTest/Upload_html?";
private EditText etName;
private EditText etPwd;
private TextView tv;
private String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById(R.id.etName);
etPwd = (EditText) findViewById(R.id.etPwd);
tv = (TextView) findViewById(R.id.tv);
}
public void butLog(View v){
final String name = getTextString(etName);
final String pwd = getTextString(etPwd);
if(name==null){
Toast.makeText(MainActivity.this,"账号不能为空",Toast.LENGTH_SHORT).show();
return;
}
if(pwd==null){
Toast.makeText(MainActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
return;
}
new Thread(){
@Override
public void run() {
try {
URL url=new URL(path);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//要向服务器写对的数据
String data="user="+name+"&password="+pwd;
//设置请求配置 Arg1:配置类型 Arg2:参数新值 String类型
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length",data.length()+"");
//设置是否要给服务器写数据
connection.setDoInput(true);
connection.setDoOutput(true);
//写数据
connection.getOutputStream().write(data.getBytes());
//从服务器读
int code = connection.getResponseCode();
if(code==200){
InputStream is = connection.getInputStream();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int len=0;
byte[] b=new byte[1024];
while((len=is.read(b))!=-1){
baos.write(b,0,len);
s = baos.toString();
}
baos.close();
is.close();
System.out.println(s);
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(s);
}
});
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
public String getTextString(EditText editText){
String s = editText.getText().toString();
if(s!=null && !s.equals("")){
return s;
}
return null;
}
}
public class MainActivity extends AppCompatActivity {
ImageView image;
File file;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
new Thread(){
@Override
public void run() {
try {
//创建url对象
URL url = new URL("http://172.17.29.120/localuser/chenhao/img1.png");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//设置请求 方式 连接超时 读取超时
urlConnection.setRequestMethod("GET");
urlConnection.setConnectTimeout(10000);
urlConnection.setReadTimeout(10000);
//得到返回码
int code = urlConnection.getResponseCode();
if ( code== 200) {
//获取流
InputStream is = urlConnection.getInputStream();
//文件方式
file= new File(Environment.getExternalStorageDirectory().getPath(), "copy.jpg");
FileOutputStream fos = new FileOutputStream(file);
int len = 0;
byte[] b = new byte[1024];
while ((len = is.read(b)) != -1) {
fos.write(b, 0, len);
}
············//这里用的 decodeFile方法 可以直接用decodeStrean直接传一个 inputStream 就可以了
fos.close();
is.close();
final Bitmap bitmap = BitmapFactory.decodeFile(file.getPath());
//主线程 更改UI
runOnUiThread(new Runnable() {
@Override
public void run() {
image.setImageBitmap(bitmap);
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}
//使用Post简单提交数据
package com.example.administrator.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class MainActivity extends AppCompatActivity {
private String path="http://123.206.70.44:8080/JavaWebTest/Upload_html?";
private EditText etName;
private EditText etPwd;
private TextView tv;
private String s;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName = (EditText) findViewById(R.id.etName);
etPwd = (EditText) findViewById(R.id.etPwd);
tv = (TextView) findViewById(R.id.tv);
}
public void butLog(View v){
final String name = getTextString(etName);
final String pwd = getTextString(etPwd);
if(name==null){
Toast.makeText(MainActivity.this,"账号不能为空",Toast.LENGTH_SHORT).show();
return;
}
if(pwd==null){
Toast.makeText(MainActivity.this,"密码不能为空",Toast.LENGTH_SHORT).show();
return;
}
new Thread(){
@Override
public void run() {
try {
URL url=new URL(path);
HttpURLConnection connection= (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
//要向服务器写对的数据
String data="user="+name+"&password="+pwd;
//设置请求配置 Arg1:配置类型 Arg2:参数新值 String类型
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("Content-Length",data.length()+"");
//设置是否要给服务器写数据
connection.setDoInput(true);
connection.setDoOutput(true);
//写数据
connection.getOutputStream().write(data.getBytes());
//从服务器读
int code = connection.getResponseCode();
if(code==200){
InputStream is = connection.getInputStream();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int len=0;
byte[] b=new byte[1024];
while((len=is.read(b))!=-1){
baos.write(b,0,len);
s = baos.toString();
}
baos.close();
is.close();
System.out.println(s);
runOnUiThread(new Runnable() {
@Override
public void run() {
tv.setText(s);
}
});
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
public String getTextString(EditText editText){
String s = editText.getText().toString();
if(s!=null && !s.equals("")){
return s;
}
return null;
}
}
相关文章推荐
- 使用HttpURLConnection和使用HttpClient方式请求网络采用get方式和post方式请求数据
- HttpURLConnection get post 方式请求 (笔记)
- HttpUrlConnection两种请求数据的方式get和post提交数据
- HttpUrlConnection以GET方式和POST方式请求服务器
- httpURLConnection-网络请求的两种方式-get请求和post请求
- Android中的post和get请求(HttpURLConnection)方式
- HttpUrlConnection和HttpClient两种方式获取GET和POST请求
- HttpUrlConnection以GET方式和POST方式请求服务器
- java发送post和get请求(1)---HttpURLConnection方式
- 使用HttpURLConnection采用get方式或post方式请求数据
- 使用HttpURLConnection向服务器发送post和get请求
- HttpURL连接远程serverGet和Post方式请求并返回数据
- HttpURLConnection发送post和get请求
- HttpUrlConnection用get和post请求发送参数
- HttpUrlConnection用get和post请求发送参数
- HttpUrlConnection用get和post请求发送参数
- HttpUrlConnection的get请求方式
- HttpURLConnection与HttpClient的Get与Post请求数据流程
- Android中使用HttpURLConnection和HttpClient实现GET和POST请求访问网络
- Android网络请求,Get方式请求,HttpURLConnection请求工具类