Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.AsyncTask;
- import android.util.Log;
- import android.view.ViewGroup;
- import org.json.JSONArray;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOException;
- import java.io.InputStream;
- import java.io.InputStreamReader;
- import java.io.OutputStream;
- import java.io.OutputStreamWriter;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.net.URLDecoder;
- import java.net.URLEncoder;
- /**
- * Created by ARTEM on 11.04.2017.
- */
- public class NetworkHelper extends AsyncTask<String,Void,String> {
- Context context;
- String result = "";
- JSONArray jsonResult;
- static Bitmap imageResult;
- //AlertDialog alertDialog;
- NetworkHelper(Context ctx) {
- context = ctx;
- }
- @Override
- protected String doInBackground(String... params) {
- String type = params[0];
- String ip = "192.168.1.106";
- String checkList_url = "http://" + ip + "/android/checkList.php";
- String addOrder_url = "http://" + ip + "/android/addOrder.php";
- String getUserOrders_url = "http://" + ip + "/android/getUserOrders.php";
- String getAccount_url = "http://" + ip + "/android/getAccount.php";
- String setAccount_url = "http://" + ip + "/android/setAccount.php";
- String image_url = "http://" + ip + "/android/pictures/";
- if (type.equals("checkList")) {
- try {
- URL url = new URL(checkList_url);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("GET");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
- String line;
- result = "";
- while ((line = bufferedReader.readLine()) != null) {
- result += line;
- }
- try {
- jsonResult = new JSONArray();
- jsonResult = new JSONArray(result);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- delay(100);
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- return "";
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (type.equals("getImage")) {
- try {
- URL url = new URL(image_url+params[1]);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- InputStream inputStream = httpURLConnection.getInputStream();
- imageResult = BitmapFactory.decodeStream(inputStream);
- delay(200);
- inputStream.close();
- httpURLConnection.disconnect();
- return "";
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (type.equals("addOrder")) {
- try {
- String dataAboutUser = params[1];
- String products = params[2];
- URL url = new URL(addOrder_url);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream outputStream = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
- String post_data = URLEncoder.encode("dataAboutUser", "UTF-8") + "=" + URLEncoder.encode(dataAboutUser, "UTF-8") + "&" +
- URLEncoder.encode("products", "UTF-8") + "=" + URLEncoder.encode(products, "UTF-8");
- Log.i("POST DATA", post_data);
- Log.i("DECODE", URLDecoder.decode(post_data, "UTF-8"));
- bufferedWriter.write(post_data);
- bufferedWriter.flush();
- bufferedWriter.close();
- outputStream.close();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
- String line;
- while ((line = bufferedReader.readLine()) != null) {
- result += line;
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- delay(100);
- return result;
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (type.equals("getUserOrders")) {
- try {
- String androidId = params[1];
- URL url = new URL(getUserOrders_url);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream outputStream = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
- String post_data = URLEncoder.encode("androidId", "UTF-8") + "=" + URLEncoder.encode(androidId, "UTF-8");
- bufferedWriter.write(post_data);
- bufferedWriter.flush();
- bufferedWriter.close();
- outputStream.close();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
- String line;
- result = "";
- while ((line = bufferedReader.readLine()) != null) {
- result += line;
- }
- try {
- jsonResult = new JSONArray();
- jsonResult = new JSONArray(result);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- delay(100);
- return "";
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (type.equals("getAccount")) {
- try {
- String login = params[1];
- String password = params[2];
- URL url = new URL(getAccount_url);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream outputStream = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
- String post_data = URLEncoder.encode("login", "UTF-8") + "=" + URLEncoder.encode(login, "UTF-8") + "&" +
- URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
- bufferedWriter.write(post_data);
- bufferedWriter.flush();
- bufferedWriter.close();
- outputStream.close();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
- String line;
- result = "";
- while ((line = bufferedReader.readLine()) != null) {
- result += line;
- }
- try {
- MainActivity.account = new JSONObject(result);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- delay(100);
- return "";
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- if (type.equals("setAccount")) {
- try {
- String login = params[1];
- String password = params[2];
- String dataAboutAccount = params[3];
- URL url = new URL(setAccount_url);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream outputStream = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
- String post_data = URLEncoder.encode("login", "UTF-8") + "=" + URLEncoder.encode(login, "UTF-8") + "&" +
- URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8") + "&" +
- URLEncoder.encode("dataAboutAccount", "UTF-8") + "=" + URLEncoder.encode(dataAboutAccount, "UTF-8");
- bufferedWriter.write(post_data);
- bufferedWriter.flush();
- bufferedWriter.close();
- outputStream.close();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
- String line;
- result = "";
- while ((line = bufferedReader.readLine()) != null) {
- result += line;
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- delay(100);
- return "";
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- @Override
- protected void onPreExecute() {
- /*alertDialog = new AlertDialog.Builder(context).create();
- alertDialog.setTitle("Wait");
- alertDialog.show();*/
- Log.d("s","s");
- }
- @Override
- protected void onPostExecute(String s) {
- //alertDialog.dismiss();
- //alertDialog.show();
- }
- @Override
- protected void onProgressUpdate(Void... values) {
- super.onProgressUpdate(values);
- }
- public void delay(Integer value) {
- try {
- Thread.sleep(value);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement