Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.daumantas.zaidimas;
- import android.app.AlertDialog;
- import android.content.Context;
- import android.os.AsyncTask;
- import android.os.Bundle;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.webkit.WebSettings;
- import android.webkit.WebView;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- import android.widget.ProgressBar;
- import android.widget.TextView;
- import android.widget.Toast;
- import org.json.JSONArray;
- import android.content.Intent;
- import org.json.JSONException;
- import org.json.JSONObject;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.IOError;
- 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.URLEncoder;
- import static android.content.ContentValues.TAG;
- public class BackgroundWorker extends AsyncTask<String,Void,String> {
- public String Grain;
- public String Timber;
- public String Stone;
- public String Iron;
- public String Gold;
- public String a = "111";
- TextView t1;
- TextView t2;
- TextView t3;
- TextView t4;
- TextView t5;
- Context context;
- AlertDialog alertDialog;
- BackgroundWorker (Context ctx){
- context = ctx;
- }
- @Override
- protected String doInBackground(String... params) {
- String type = params[0];
- String login_url = "http://192.168.1.65/getdataa.php";
- String register_url = "http://192.168.1.65/register.php";
- if (type.equals("login")) { //Jei tipas == login, bando prisijungt, else registruotis
- try {
- URL url = new URL(login_url);
- try {
- String user_name = params[1];
- String password = params[2];
- 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("coordX", "UTF-8")+"="+URLEncoder.encode(user_name, "UTF-8")+"&"
- +URLEncoder.encode("coordY", "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 result="";
- String line="";
- while((line = bufferedReader.readLine())!=null) {
- result += line;
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- return result;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else if (type.equals("register")) {
- try {
- URL url = new URL(register_url);
- try {
- String name = params[1];
- String username = params[2];
- String password = params[3];
- String age = params[4];
- String email = params[5];
- 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("name", "UTF-8")+"="+URLEncoder.encode(name, "UTF-8")+"&"
- + URLEncoder.encode("age", "UTF-8")+"="+URLEncoder.encode(username, "UTF-8")+"&"
- + URLEncoder.encode("username", "UTF-8")+"="+URLEncoder.encode(password, "UTF-8")+"&"
- + URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(age, "UTF-8")+"&"
- +URLEncoder.encode("email", "UTF-8")+"="+URLEncoder.encode(email, "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 result="";
- String line="";
- while((line = bufferedReader.readLine())!=null) {
- result += line;
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- return result;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else if (type.equals("register")) {
- try {
- URL url = new URL(register_url);
- try {
- String name = params[1];
- String username = params[2];
- String password = params[3];
- String age = params[4];
- String email = params[5];
- 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("name", "UTF-8")+"="+URLEncoder.encode(name, "UTF-8")+"&"
- + URLEncoder.encode("age", "UTF-8")+"="+URLEncoder.encode(username, "UTF-8")+"&"
- + URLEncoder.encode("username", "UTF-8")+"="+URLEncoder.encode(password, "UTF-8")+"&"
- + URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(age, "UTF-8")+"&"
- +URLEncoder.encode("email", "UTF-8")+"="+URLEncoder.encode(email, "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 result="";
- String line="";
- while((line = bufferedReader.readLine())!=null) {
- result += line;
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- return result;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- }
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- @Override
- protected void onPreExecute() {
- alertDialog = new AlertDialog.Builder(context).create();
- alertDialog.setTitle("Coordinate Status");
- }
- @Override
- protected void onPostExecute(String result) {
- alertDialog.setMessage(result);
- alertDialog.show();
- // [{"Grain":3,"Timber":5,"Stone":5,"Iron":5,"Gold":5}]
- MapActivity insert = new MapActivity();
- getStrings(result);
- enterMats();
- }
- @Override
- protected void onProgressUpdate(Void... values) {
- super.onProgressUpdate(values);
- }
- public void getStrings(String result){
- result = result.replace("[","");
- result = result.replace("]","");
- JSONObject jObject = null;
- try {
- jObject = new JSONObject(result);
- Grain = jObject.getString("Grain");
- Timber = jObject.getString("Timber");
- Stone = jObject.getString("Stone");
- Iron = jObject.getString("Iron");
- Gold = jObject.getString("Gold");
- Log.d(TAG, "______________GRAIN___________: " + Grain);
- Log.d(TAG, "______________TIMBER__________: " + Timber);
- Log.d(TAG, "______________STONE___________: " + Stone);
- Log.d(TAG, "______________IRON____________: " + Iron);
- Log.d(TAG, "______________GOLD____________: " + Gold);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- public void enterMats(){
- MapActivity sendname = new MapActivity();
- sendname.getMats(Gold,Timber,Stone,Iron,Gold);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement