Advertisement
Guest User

Untitled

a guest
Jan 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.12 KB | None | 0 0
  1. package com.ksni.roots.ngsales.domain;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.net.Uri;
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.util.Base64;
  8. import android.util.Log;
  9. import android.view.LayoutInflater;
  10. import android.view.View;
  11. import android.view.ViewGroup;
  12. import android.app.Fragment;
  13. import android.widget.ListAdapter;
  14. import android.widget.ListView;
  15. import android.widget.SimpleAdapter;
  16. import android.widget.Toast;
  17.  
  18. import com.ksni.roots.ngsales.R;
  19. import com.ksni.roots.ngsales.util.validator.konfigurasi;
  20.  
  21. import org.json.JSONException;
  22. import org.json.JSONObject;
  23.  
  24. import java.io.IOException;
  25. import java.io.InputStream;
  26. import java.net.HttpURLConnection;
  27. import java.net.URL;
  28. import java.util.ArrayList;
  29. import java.util.HashMap;
  30.  
  31.  
  32. /**
  33.  * A simple {@link Fragment} subclass.
  34.  * Activities that contain this fragment must implement the
  35.  * {@link InventoryPlant.OnFragmentInteractionListener} interface
  36.  * to handle interaction events.
  37.  * Use the {@link InventoryPlant#newInstance} factory method to
  38.  * create an instance of this fragment.
  39.  */
  40. public class InventoryPlant extends Fragment {
  41.     // TODO: Rename parameter arguments, choose names that match
  42.     // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
  43.     private static final String ARG_PARAM1 = "param1";
  44.     private static final String ARG_PARAM2 = "param2";
  45.  
  46.     // TODO: Rename and change types of parameters
  47.     private String mParam1;
  48.     private String mParam2;
  49.     private ListView listView;
  50.     private String JSON_STRING;
  51.     private OnFragmentInteractionListener mListener;
  52.     private String teeest;
  53.     private String strBildc,strArktx;
  54.  
  55.     /**
  56.      * Use this factory method to create a new instance of
  57.      * this fragment using the provided parameters.
  58.      *
  59.      * @param param1 Parameter 1.
  60.      * @param param2 Parameter 2.
  61.      * @return A new instance of fragment InventoryPlant.
  62.      */
  63.     // TODO: Rename and change types and number of parameters
  64.     public static InventoryPlant newInstance(String param1, String param2) {
  65.         InventoryPlant fragment = new InventoryPlant();
  66.         Bundle args = new Bundle();
  67.         args.putString(ARG_PARAM1, param1);
  68.         args.putString(ARG_PARAM2, param2);
  69.         fragment.setArguments(args);
  70.         return fragment;
  71.     }
  72.  
  73.     @Override
  74.     public void onCreate(Bundle savedInstanceState) {
  75.         super.onCreate(savedInstanceState);
  76. //        getBil();]
  77.         new AsyncFetchDetailBilling().execute();
  78.         if (getArguments() != null) {
  79.             mParam1 = getArguments().getString(ARG_PARAM1);
  80.             mParam2 = getArguments().getString(ARG_PARAM2);
  81.  
  82.         }
  83.     }
  84.  
  85.     private class AsyncFetchDetailBilling extends AsyncTask<String, String, Void> {
  86.         ProgressDialog pdLoading = new ProgressDialog(getActivity());
  87.         HttpURLConnection conn;
  88.         String urel = null;
  89.         String userName = "sfasys";
  90.         String passWord = "sfa321";
  91.  
  92.         @Override
  93.         protected void onPreExecute(){
  94.             super.onPreExecute();
  95.             pdLoading.setMessage("Loading...");
  96.             pdLoading.setCancelable(false);
  97.             pdLoading.show();
  98.         }
  99.  
  100.         @Override
  101.         protected Void doInBackground(String... params){
  102.             try {
  103.                 urel = "https://prd.nabatigroup.com/sap/opu/odata/sap/ZPRJ_SD0002_SRV/RKHv04ASet";
  104.                 URL myURL = new URL(urel);
  105.                 HttpURLConnection myURLConnection = (HttpURLConnection)myURL.openConnection();
  106.  
  107.                 String userCredentials = "username:password";
  108.                 String basicAuth = "Basic " + new String(Base64.getEncoder().encode(userCredentials.getBytes()));
  109.  
  110.                 myURLConnection.setRequestProperty ("Authorization", basicAuth);
  111.                 myURLConnection.setRequestMethod("POST");
  112.                 myURLConnection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  113.                 myURLConnection.setRequestProperty("Content-Length", "" + postData.getBytes().length);
  114.                 myURLConnection.setRequestProperty("Content-Language", "en-US");
  115.                 myURLConnection.setUseCaches(false);
  116.                 myURLConnection.setDoInput(true);
  117.                 myURLConnection.setDoOutput(true);
  118.  
  119.                 if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
  120.                     StringBuffer responseMsg = new StringBuffer();
  121.                     InputStream dis = conn.getInputStream();
  122.                     int chr;
  123.                     while ((chr = dis.read()) != -1) {
  124.                         responseMsg.append((char) chr);
  125.                     }
  126. //                    return new GetRecentContextCall.Response(urlConn.getResponseCode(),
  127. //                            urlConn.getResponseMessage(),
  128. //                            responseMsg.toString());
  129.                     int response_code = conn.getResponseCode();
  130.                     Log.d("URL", String.valueOf(conn));
  131.                     Log.d("ResponseCodeBill", String.valueOf(response_code));
  132. //
  133.  
  134.                 }
  135. //                int response_code = urlConn.getResponseCode();
  136.                 Log.d("ResponseMessageBill", conn.getResponseMessage());
  137.  
  138.             } catch (IOException e) {
  139.                 e.printStackTrace();
  140.             } finally {
  141.                 if (conn != null) {
  142.                     conn.disconnect();
  143.                 }
  144.             }
  145. //            try{
  146. //                url = new URL("https://sfasys:sfa321@prd.nabatigroup.com/sap/opu/odata/sap/ZPRJ_SD0002_SRV/RKHv04ASet");
  147. //                Log.d("NEWURL", "NEWURL: "+url);
  148. //            }
  149. //            catch (MalformedURLException e){
  150. //                e.printStackTrace();
  151. //                return e.toString();
  152. //            }
  153. //
  154. //            try {
  155. //                conn = (HttpURLConnection) url.openConnection();
  156. //                conn.setReadTimeout(10000);
  157. //                conn.setConnectTimeout(15000);
  158. //                conn.setUseCaches(false);
  159. //                conn.setDoOutput(true);
  160. ////                conn.setDoInput(true);
  161. ////                conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
  162. //                conn.setRequestProperty("Accept","application/json");
  163. //                conn.setRequestProperty("charset","utf-8");
  164. //                conn.setRequestMethod("GET");
  165. //
  166. //            }catch (IOException e1){
  167. //                e1.printStackTrace();
  168. //                return e1.toString();
  169. //            }
  170. //
  171. //            try {
  172. //                int response_code = conn.getResponseCode();
  173. //                Log.d("ResponseCodeBill", String.valueOf(conn.getResponseCode()));
  174. //
  175. //                if (response_code == HttpURLConnection.HTTP_OK){
  176. //                    InputStream input = conn.getInputStream();
  177. //                    BufferedReader reader = new BufferedReader(new InputStreamReader(input));
  178. //                    StringBuilder result = new StringBuilder();
  179. //                    String line;
  180. //                    while ((line = reader.readLine()) != null){
  181. //                        result.append(line);
  182. //                    }
  183. //                    return (result.toString());
  184. //                } else{
  185. //                    return (String.valueOf(response_code));
  186. //                }
  187. //            }catch (IOException e2){
  188. //                e2.printStackTrace();
  189. //                return e2.toString();
  190. //            }finally {
  191. //                conn.disconnect();
  192. //            }
  193.             return null;
  194.         }
  195.  
  196.         @Override
  197.         protected void onPostExecute(Void result){
  198.             ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
  199.             try {
  200.                 JSONObject json = new JSONObject(String.valueOf(result)).getJSONObject("results");
  201.                 Log.d("NEWJSON", "onPostExecute: "+json);
  202.                 strBildc = json.getString("Bildc");
  203.                 strArktx = json.getString("Arktx");
  204.  
  205.                 ListAdapter adapter = new SimpleAdapter(
  206.                         getActivity(),list,R.layout.fragment_inventory_plant,
  207.                         new String[]{strBildc,strArktx},
  208.                         new int[]{R.id.nobil,R.id.pid});
  209.                 listView.setAdapter(adapter);
  210.                 Log.d("showbil", "showBil: "+konfigurasi.TAG_NOBIL);
  211.             }catch (JSONException e){
  212.                 Toast.makeText(getActivity(),e.toString()+result,Toast.LENGTH_LONG).show();
  213.             }
  214.  
  215.  
  216.         }
  217.  
  218.     }
  219.  
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229.  
  230.  
  231.  
  232.  
  233. //    private void getBil(){
  234. //        class GetBill extends AsyncTask<Void,Void,String >{
  235. //            @Override
  236. //            protected void onPreExecute(){
  237. //                super.onPreExecute();
  238. //            }
  239. //            @Override
  240. //            protected void onPostExecute(String s){
  241. //                super.onPostExecute(s);
  242. //                JSON_STRING = s;
  243. //                showBil();
  244. //            }
  245. //            @Override
  246. //            protected String doInBackground(Void... params){
  247. //                String urel = "https://sfasys:sfa321@prd.nabatigroup.com/sap/opu/odata/sap/ZPRJ_SD0002_SRV/RKHv04ASet?$filter=Pgidt%20eq%20datetime%272019-01-16T00:00:00%27%20and%20Slsorg%20eq%20%27PID1%27";
  248. //                RequestHandler rh = new RequestHandler();
  249. //                String s = rh.sendGetRequest(urel);
  250. //                Log.d("sendRequest", "doInBackground: "+s);
  251. //                return s;
  252. //            }
  253. //        }
  254. //
  255. //        GetBill ge = new GetBill();
  256. //        ge.execute();
  257. //    }
  258. //
  259. //    private void showBil(){
  260. //
  261. //        JSONObject jsonObject = null;
  262. //        ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String, String>>();
  263. //        try{
  264. //            jsonObject = new JSONObject("d");
  265. //            JSONArray result = jsonObject.getJSONArray("results");
  266. //            Log.d("NewResults", "NewResults: "+result);
  267. //
  268. //            for (int i = 0; i<result.length(); i++){
  269. //                JSONObject jo = result.getJSONObject(i);
  270. //                String nobil = jo.getString(konfigurasi.TAG_NOBIL);
  271. //                String pid = jo.getString(konfigurasi.TAG_PID);
  272. //
  273. //                HashMap<String,String> billing = new HashMap<>();
  274. //                billing.put(konfigurasi.TAG_NOBIL,nobil);
  275. //                billing.put(konfigurasi.TAG_PID,pid);
  276. //                list.add(billing);
  277. //            }
  278. //        }
  279. //        catch (JSONException e){
  280. //            e.printStackTrace();
  281. //        }
  282. //
  283. //        ListAdapter adapter = new SimpleAdapter(
  284. //                getActivity(),list,R.layout.fragment_inventory_plant,
  285. //                new String[]{konfigurasi.TAG_NOBIL,konfigurasi.TAG_PID},
  286. //                new int[]{R.id.nobil,R.id.pid});
  287. //        listView.setAdapter(adapter);
  288. //        Log.d("showbil", "showBil: "+konfigurasi.TAG_NOBIL);
  289. //    }
  290.  
  291.     @Override
  292.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  293.                              Bundle savedInstanceState) {
  294.         // Inflate the layout for this fragment
  295.         View rv = inflater.inflate(R.layout.fragment_inventory_plant, container, false);
  296.         listView = rv.findViewById(R.id.listInventory);
  297.         return rv;
  298.     }
  299.  
  300.     // TODO: Rename method, update argument and hook method into UI event
  301.     public void onButtonPressed(Uri uri) {
  302.         if (mListener != null) {
  303.             mListener.onFragmentInteraction(uri);
  304.         }
  305.     }
  306.  
  307. //    @Override
  308. //    public void onAttach(Context context) {
  309. //        super.onAttach(context);
  310. //        if (context instanceof OnFragmentInteractionListener) {
  311. //            mListener = (OnFragmentInteractionListener) context;
  312. //        } else {
  313. //            throw new RuntimeException(context.toString()
  314. //                    + " must implement OnFragmentInteractionListener");
  315. //        }
  316. //    }
  317.  
  318.     @Override
  319.     public void onDetach() {
  320.         super.onDetach();
  321.         mListener = null;
  322.     }
  323.  
  324.  
  325.     public interface OnFragmentInteractionListener {
  326.         // TODO: Update argument type and name
  327.         void onFragmentInteraction(Uri uri);
  328.     }
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement