友链:汪菜菜

友链:隔壁老王

HttpHelper

package com.example.wms.util;

import android.os.Environment;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

public class HttpHelper {

    public static String BASE_URL ="http://192.168.2.113:8090/";
    //private static final String USER_AGENT = "Mozilla/4.5";
    private static final String LoginName="Login_namefile";
    //static File loginfile=new File(Environment.getExternalStorageDirectory(),LoginName);
    //登录
    public static String url_Login="http://192.168.2.117:8090/wms/android/login/user";
    //车间收货
    public static String url_WorkShopReceipt="http://192.168.2.117:8090/wms/android/workshop/receipt";
    //库存查询
    public static String url_StockQuery="http://192.168.2.117:8090/wms/android/workshop/stock";
    //订单查询
    public static String url_BillQuery="http://192.168.2.117:8090/wms/android/workshop/order";
    //托盘状态查询
    public static String url_TrayStatusQuery="http://192.168.2.117:8090/wms/android/workshop/palletstate";
    //空盘上线
    public static String url_EmptyTrayOnline="http://192.168.2.117:8090/wms/android/workshop/palletgoonline";
    //组盘上线
    public static String url_GroupTrayOnline="http://192.168.2.117:8090/wms/android/workshop/loadpallet";

    public static String EncryptTokenNo="";
    public static String AppType ="OnLineRF";
    public static String AppDeviceNo="";
    public static String IPAddress="";

    private static HttpGet getHttpGet(String url, String json) {
        HttpGet request = null;
        try {
            String strUrl=url+"&"+json;
            request = new HttpGet(url_StockQuery);

            request.setHeader("Accept", "application/json");
            request.setHeader("Content-Type", "application/json");

        } catch (IllegalArgumentException e) {
            System.out.println("网络调用出现异常,请检查访问的URL的书写!");
            return null;
        } catch (Exception ex) {
            System.out.println("网络调用出现异常,请检查网络是否开启!");
            return null;
        }
        return request;
    }
    // 发送Get请求,获得响应查询结果
    public static String GetHttpResultByGet(String url, HttpParams json) {
        String retSrc = "";//网络返回值;

        HttpGet httpRequestHttpGet = new HttpGet(url);
        httpRequestHttpGet.setParams(json);
        // 创建客户端对象
        HttpClient httpClient = new DefaultHttpClient();
        try {
            HttpResponse httpResponse = httpClient.execute(httpRequestHttpGet);
            retSrc = EntityUtils.toString(httpResponse.getEntity(), "UTF8");
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return retSrc;
    }

    public static String Get(String url, String json) {
        // 获得HttpGet对象
        HttpGet request = HttpHelper.getHttpGet(url, json);

        String result = null;
        try {
            request = new HttpGet(url);
        } catch (IllegalArgumentException e) {
            result = "网络Get调用出现异常,请检查访问的URL的书写!";
            System.out.println(result);
            return result;
        } catch (Exception ex) {
            result = "Get调用出现异常,请检查网络是否开启!";
            System.out.println(result);
            return result;
        }

        try {
            HttpResponse response = new DefaultHttpClient().execute(request);
            if (response.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(response.getEntity());
                return result;
            }
        } catch (ClientProtocolException e) {
            result = "网络Get异常!ClientProtocolException错误!";
            return result;
        } catch (IOException e) {
            result = "网络Get异常!IOException错误!";
            return result;
        }
        return null;
    }
    private static HttpPost getHttpPost(String url, String json) {
        HttpPost request = null;
        try {
            request = new HttpPost(url);
            request.setHeader("Accept", "application/json");
            request.setHeader("Content-Type", "application/json");

        } catch (IllegalArgumentException e) {
            System.out.println("网络调用出现异常,请检查访问的URL的书写!");
            return null;
        } catch (Exception ex) {
            System.out.println("网络调用出现异常,请检查网络是否开启!");
            return null;
        }
        if (json != null) {
            try {
                StringEntity entity = new StringEntity(json, "UTF-8");
                request.setEntity(entity);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return request;
    }

    //发送post请求,获得响应查询结果
    public static String Post(String url, String json) {
        HttpPost request = HttpHelper.getHttpPost(url, json);
        String result = null;
        if (request == null) {
            result = "Post调用网络失败,网络有问题!";
            return result;
        }
        try {
            HttpResponse response = new DefaultHttpClient().execute(request);
            if (response.getStatusLine().getStatusCode() == 200) {
                HttpEntity wsy=response.getEntity();
                result = EntityUtils.toString(wsy);

            } else {
                result = "网络Post异常!请求失败!";
                //MainActivity.log("网络Post异常!请求失败!");
            }
        } catch (ClientProtocolException e) {
            result = "网络Post异常!ClientProtocolException错误!"+e.toString();
        } catch (IOException e) {
            e.printStackTrace();
            result = "网络Post异常!IOException错误!"+e.toString();
            //打印log信息,方法在MainActivity里面
//			MainActivity.Log();
//			MainActivity.log("网络Post异常!ClientProtocolException错误!");
        }
        catch(Exception e){
            result = "网络Post异常!"+e.toString();
        }
        return result;
    }

    //发送post请求,获得响应查询结果
    public static String Postnocode(String url) {
        HttpPost request = HttpHelper.getHttpPost(url,null);
        String result = null;
        if (request == null) {
            result = "Post调用网络失败,网络有问题!";
            return result;
        }
        try {
            HttpResponse response = new DefaultHttpClient().execute(request);
            if (response.getStatusLine().getStatusCode() == 200) {
                result = EntityUtils.toString(response.getEntity());

            } else {
                result = "网络Post异常!请求失败!";
            }
        } catch (ClientProtocolException e) {
            result = "网络Post异常!ClientProtocolException错误!";

        } catch (IOException e) {
            e.printStackTrace();
            result = "网络Post异常!IOException错误!";
        }
        catch(Exception e){
            result = "网络Post异常!";
        }
        return result;
    }

/*    public static  String getLoginname(){
        String name = null;
        if (Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED)) {
            try {
                FileInputStream inputStream = new FileInputStream(loginfile);
                byte[] b = new byte[inputStream.available()];
                inputStream.read(b);
                name=(new String(b));
            } catch (Exception e) {

            }
        }
        return name;
    }*/
}

 

posted @   長安憶ღ  阅读(62)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】
点击右上角即可分享
微信分享提示