数据转换工具类:
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/**
* Created by Administrator on 2018/9/14 0014.
*/
public class Util_app {
public String DATA_URLEncoder(String data_name[],String[] data_value){
String return_value = "";
//声明返回值
//判断 键值长度是否相等
if(data_name.length==data_value.length){
//循环赋值
for(int i=0;i<data_name.length;i++){
try {
//做数据格式转换,UTF-8 中文格式
return_value+=URLEncoder.encode(data_name[i], "UTF-8") + "=" +URLEncoder.encode(data_value[i], "UTF-8");
//非末尾使用 & 拼接
if(i!=data_name.length-1){return_value+="&";}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}else{
return_value = "data_erro";
}
return return_value;
}
}
请求链接管理类
public class Data {
static String home_url = "http://你的域名";
static String login_url = home_url+"/mz_pbl/app_con/login_up.php";
}
网络请求处理层(一般不用修改)
package com.example.administrator.myapplication;
import android.util.Log;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.List;
import java.util.Map;
/**
* Created by Administrator on 2018/9/14 0014.
*/
public class Common {
public static String postGetJson(String url, String content) {
try {
URL mUrl = new URL(url);
HttpURLConnection mHttpURLConnection = (HttpURLConnection) mUrl.openConnection();
//设置链接超时时间
mHttpURLConnection.setConnectTimeout(15000);
//设置读取超时时间
mHttpURLConnection.setReadTimeout(15000);
//设置请求参数
mHttpURLConnection.setRequestMethod("POST");
//添加Header
mHttpURLConnection.setRequestProperty("Connection", "Keep-Alive");
//接收输入流
mHttpURLConnection.setDoInput(true);
//传递参数时需要开启
mHttpURLConnection.setDoOutput(true);
//Post方式不能缓存,需手动设置为false
mHttpURLConnection.setUseCaches(false);
mHttpURLConnection.connect();
DataOutputStream dos = new DataOutputStream(mHttpURLConnection.getOutputStream());
String postContent = content;
dos.write(postContent.getBytes());
dos.flush();
// 执行完dos.close()后,POST请求结束
dos.close();
// 获取代码返回值
int respondCode = mHttpURLConnection.getResponseCode();
Log.d("respondCode","respondCode="+respondCode );
// 获取返回内容类型
String type = mHttpURLConnection.getContentType();
Log.d("type", "type="+type);
// 获取返回内容的字符编码
String encoding = mHttpURLConnection.getContentEncoding();
Log.d("encoding", "encoding="+encoding);
// 获取返回内容长度,单位字节
int length = mHttpURLConnection.getContentLength();
Log.d("length", "length=" + length);
// // 获取头信息的Key
// String key = mHttpURLConnection.getHeaderField(idx);
// Log.d("key", "key="+key);
// 获取完整的头信息Map
Map<String, List<String>> map = mHttpURLConnection.getHeaderFields();
if (respondCode == 200) {
// 获取响应的输入流对象
InputStream is = mHttpURLConnection.getInputStream();
// 创建字节输出流对象
ByteArrayOutputStream message = new ByteArrayOutputStream();
// 定义读取的长度
int len = 0;
// 定义缓冲区
byte buffer[] = new byte[1024];
// 按照缓冲区的大小,循环读取
while ((len = is.read(buffer)) != -1) {
// 根据读取的长度写入到os对象中
message.write(buffer, 0, len);
}
// 释放资源
is.close();
message.close();
// 返回字符串
String msg = new String(message.toByteArray());
Log.d("Common", msg);
return msg;
}
return "fail";
}catch(Exception e){
return "error";
}
}
}
逻辑处理层
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by Administrator on 2018/9/14 0014.
*/
public class NetConnect extends AsyncTask<Void,Integer,String> {
private Context context;//请求页面的context
private String url;//请求的URL
private String postValue;//请求的POST参数
private View view;//可以作为动态UI更新view(这里写成View,可以是TextView/Button/LinearLayout 等)
private int tab;//用于区分请求完毕后要做什么
NetConnect(Context context , String string , String postValue , View view , int tab) {
this.context = context;
this.url = string;
this.postValue = postValue;
this.view = view;
this.tab = tab;
}
/**
* 运行在UI线程中,在调用doInBackground()之前执行
* 该方法运行在UI线程当中,并且运行在UI线程当中 可以对UI空间进行设置
*/
//开始执行
@Override
protected void onPreExecute() {
Toast.makeText(context,"开始执行",Toast.LENGTH_SHORT).show();
}
/**
* 后台运行的方法,可以运行非UI线程,可以执行耗时的方法
* 这里的Void参数对应AsyncTask中的第一个参数
* 这里的String返回值对应AsyncTask的第三个参数
* 该方法并不运行在UI线程当中,主要用于异步操作,所有在该方法中不能对UI当中的空间进行设置和修改
* 但是可以调用publishProgress方法触发onProgressUpdate对UI进行操作
*/
//正在连接
@Override
protected String doInBackground(Void... params) {
int i=0;
while(i<10){
i++;
//publishProgress 更新进度,给onProgressUpdate()传递进度参数
publishProgress(i);//执行onProgressUpdate 展示进度
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
String result = Common.postGetJson(url,postValue);
//第三个参数为String 所以此处return一个String类型的数据
return result;
}
/**
* 在publishProgress()被调用以后执行,publishProgress()用于更新进度
* 这里的Intege参数对应AsyncTask中的第二个参数
* 在doInBackground方法当中,,每次调用publishProgress方法都会触发onProgressUpdate执行
* onProgressUpdate是在UI线程中执行,所有可以对UI空间进行操作
*/
//进度报告
@Override
protected void onProgressUpdate(Integer... values)
{
if(tab==1){//tab 为1 情况下,我传送的view 是TextView,我想要展示进度
//第二个参数为Int
TextView view2 = (TextView) view;
view2.setText(""+values[0]);
}
}
/**
* 这里的String参数对应AsyncTask中的第三个参数(也就是接收doInBackground的返回值)
* 运行在ui线程中,在doInBackground()执行完毕后执行,传入的参数为doInBackground()返回的结果
*/
//执行完毕
@Override
protected void onPostExecute(String i) {
if(tab==1){//逻辑处理1:展示请求完毕后内容(处理结果自由编辑)
//尝试显示内容
TextView textView = (TextView) view;
Toast.makeText(context,i,Toast.LENGTH_SHORT).show();
textView.setText(i+this.postValue);
}else if(tab==2){//逻辑处理2:跳转页面(处理结果自由编辑)
//尝试跳转页面
Intent intent = new Intent(context,【你要跳哪里去】.class);
context.startActivity(intent);
}
}
}
运行:
private void Update(){
try{
String url = Data.login_url;
String data_name[] = {"user","pass"};
String data_value[] = {"18312345678","000000"};
String value = utleapp.DATA_URLEncoder(data_name,data_value);
NetConnect updatetext = new NetConnect(this, url, value , 你要传过去的view,2);
updatetext.execute();
}catch(Exception e){
ajax_text.setText("error");
}
}