Issue
I'm a newbie in Android application programming (even in Java). I have my own data from my weather stations and I decode them in background from JSON - that all wokrs fine.
in the "onPostExecute" function I call another function called "save". And in this function i need "context".
How do I define this "context"? or where should I take it from? Can you please direct me in my example?
I tried some google variants, but each time the application stops working. (Android 5.1 - Lenovo P70) - I can not use a virtual device on my computer, so Iam testing my app directly on my phone after connecting with Android studio.
I will be grateful for any advice.
Here is "fetchData.java" which contains a function "fetchData" which is called from "MainActivity.java" when i touch a button.
package arduino.weatherstation;
import android.content.Context;
import android.os.AsyncTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class fetchData extends AsyncTask<String,String,String> {
String data = "";
private String dataParsed = "";
private String singleParsed = "";
private String last_data = "";
private static final String FILE_NAME = "lastdata.txt";
@Override
protected String doInBackground(String... params) {
String result = "";
try {
URL url = new URL("https://***.cz");
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while(line != null) {
line = bufferedReader.readLine();
data = data + line;
}
JSONArray JA = new JSONArray(data);
for(int i = 0; i < JA.length(); i++) {
JSONObject JO = (JSONObject) JA.get(i);
singleParsed = "Day:" + JO.get("Day") + "\n"+
"Time:" + JO.get("Time") + "\n"+
"Temperature:" + JO.get("Temperature") + "\n"+
"Humidity:" + JO.get("Humidity") + "\n"+
"Pressure:" + JO.get("Pressure") + "\n"+
"Light:" + JO.get("Light") + "\n"+
"Rain:" + JO.get("Rain") + "\n"+
"City:" + JO.get("City") + "\n"+
"Country:" + JO.get("Country") + "\n"+
"Id:" + JO.get("Id") + "\n";
dataParsed = dataParsed + singleParsed;
if(i == (JA.length() - 1)) {
last_data = "Day:" + JO.get("Day") + "\n"+
"Time:" + JO.get("Time") + "\n"+
"Temperature:" + JO.get("Temperature") + "\n"+
"Humidity:" + JO.get("Humidity") + "\n"+
"Pressure:" + JO.get("Pressure") + "\n"+
"Light:" + JO.get("Light") + "\n"+
"Rain:" + JO.get("Rain") + "\n"+
"City:" + JO.get("City") + "\n"+
"Country:" + JO.get("Country") + "\n"+
"Id:" + JO.get("Id") + "\n";
}
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String aVoid) {
super.onPostExecute(aVoid);
MainActivity.data.setText(last_data);
save(last_data);
}
public void save(String toSave) {
String text = toSave;
FileOutputStream fos = null;
try {
fos = context.openFileOutput(FILE_NAME, Context.MODE_PRIVATE);
fos.write(text.getBytes());
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
Solution
First declare a Context variable in your fetchData class like this
Context context;
Now make a constructor for fetchData and take context as an argument like this
public fetchData(Context context)
{
this.context = context;
}
Then wherever you are creating an object of fetchData pass the context form that class like
fetchData fetchObject = new fetchData(this);
fetchObject.execute();
When you are using context for file it will be available in your fetchData class context variable. Hope it helps.
Answered By - AIK
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.