~Twilyappsrcmainjavacomtwilytwilyapp
5 itemsDownload ./*

5 itemsDownload ./*

..
CustomWebView.java
MainActivity.java
ReceiveData.java
customChromeClient.java
customViewClient.java


twilyappReceiveData.java
282•  2 years ago•  DownloadRawClose
2 years ago•  282

{}
package com.twily.twilyapp;

import android.content.Context;
import android.os.AsyncTask;

import android.os.Build;
import android.widget.Toast;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

import java.util.jar.Manifest;
import javax.net.ssl.HttpsURLConnection;

import android.view.MenuItem;


public class ReceiveData extends AsyncTask<URL, Integer, Long> {
	
	String response = "";
	String ajaxKey;
	String ajaxValue;
	String ajaxSetting;
	Context myContext;
	
	ReceiveData(Context context, String setting, String value, String key) {
		this.myContext = context;
		ajaxValue = value;
		ajaxSetting = setting;
		ajaxKey = key;
	}
	
	private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
		StringBuilder feedback = new StringBuilder();
		boolean first = true;
		for(Map.Entry<String, String> entry : params.entrySet()){
			if (first)
			first = false;
			else
			feedback.append("&");
			
			feedback.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
			feedback.append("=");
			feedback.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
		}
		
		return feedback.toString();
	}
	
	public void getData() throws IOException {
		
		HashMap<String, String> params = new HashMap<>();
		params.put("setting", ajaxSetting);
		params.put("value", ajaxValue);
		params.put("key", "");
		
		URL url = new URL("https://twily.info/ajaxapp.php");
		HttpURLConnection client = null;
		try {
			client = (HttpURLConnection) url.openConnection();
			client.setRequestMethod("POST");
			// You need to specify the context-type.  In this case it is a
			// form submission, so use "multipart/form-data"
			//client.setRequestProperty("multipart/form-data", "https://eddn.usgs.gov/fieldtest.html;charset=UTF-8");
			client.setDoInput(true);
			client.setDoOutput(true);
			
			OutputStream os = client.getOutputStream();
			BufferedWriter writer = new BufferedWriter(
			new OutputStreamWriter(os, "UTF-8"));
			writer.write(getPostDataString(params));
			
			writer.flush();
			writer.close();
			os.close();
			int responseCode = client.getResponseCode();
			
			if (responseCode == HttpsURLConnection.HTTP_OK) {
				String line;
				BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream()));
				while ((line = br.readLine()) != null) {
					response += line;
				}
			}
			else {
				response = "";
			}
		}
		catch (MalformedURLException e){
			e.printStackTrace();
		}
		finally {
			if(client != null) // Make sure the connection is not null.
			client.disconnect();
		}
	}
	
	@Override
	protected Long doInBackground(URL... params) {
		try {
			getData();
			} catch (IOException e) {
			e.printStackTrace();
		}
		
		// This counts how many bytes were downloaded
		final byte[] result = response.getBytes();
		Long numOfBytes = Long.valueOf(result.length);
		return numOfBytes;
	}
	
	protected void onPostExecute(Long result) {
		//System.out.println("Downloaded " + result + " bytes");
		// This is just printing it to the console for now.
		//System.out.println(response);
		// In the following two line I pass the string elsewhere and decode it.
		//InputCode input = new InputCode();
		//input.passToDisplay(myContext, response);
		//if(BuildConfig.versionName!=response) {}
		if(response!="") {
			int duration = Toast.LENGTH_SHORT;
		
			Toast toast = Toast.makeText(myContext, "Version "+response+" available! Update from App Menu. -Twily", duration);
			toast.show();
		//}
			MainActivity.newVersion=1;
		}
	}
}

Top
©twily.info 2013 - 2025
twily at twily dot info



20 124 visits
... ^ v