Reputation: 21
I'm a beginner on Android development. I created a project that uses AsyncTask
with doInBackground
and onExecute
.
I successfully displayed the data on AsyncTask to UI, but I wanna store that data into SharePreferences
when I click a Button
.
Here is my code:
public class MainActivity extends Activity
{
TextView ipDetail, pip;
String lll;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ipDetail = (TextView) findViewById(R.id.ipDetail);
pip = (TextView) findViewById(R.id.ip);
TextView title = (TextView) findViewById(R.id.title);
title.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick ( View p1 ) {
// TODO: Implement this method
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.create();
alert.setTitle("KAMPRET");
alert.show();
return false;
}
});
new getIP().execute();
}
...
Inside MainActivity
I create a class named GetIP
that extends AsyncTask.
Here the code:
private class getIP extends AsyncTask<Void,Void,Void> {
// URL Data
String SCHEME_DATA = "http";
String AUTH_DATA = "ip-api.com";
String PATH_DATA = "line";
String QUERY_DATA = "fields";
String[] QUERY_ARRAY = {
"query",
"city",
"regionName",
"country",
"isp",
"org"
};
String ip;
String c;
String rn;
String cn;
String isp;
String org;
String url = "http://ip-api.com/line/?fields=query";
@Override
protected Void doInBackground ( Void[] p1 ) {
// TODO: Implement this method
Uri.Builder URL_IP = new Uri.Builder();
URL_IP.scheme(SCHEME_DATA)
.authority(AUTH_DATA)
.appendPath(PATH_DATA)
.appendPath("")
.appendQueryParameter(QUERY_DATA, QUERY_ARRAY[0]);
Uri.Builder URL_CITY = new Uri.Builder();
URL_CITY.scheme(SCHEME_DATA)
.authority(AUTH_DATA)
.appendPath(PATH_DATA)
.appendPath("")
.appendQueryParameter(QUERY_DATA, QUERY_ARRAY[1]);
Uri.Builder URL_REGION = new Uri.Builder();
URL_REGION.scheme(SCHEME_DATA)
.authority(AUTH_DATA)
.appendPath(PATH_DATA)
.appendPath("")
.appendQueryParameter(QUERY_DATA, QUERY_ARRAY[2]);
Uri.Builder URL_COUNTRY = new Uri.Builder();
URL_COUNTRY.scheme(SCHEME_DATA)
.authority(AUTH_DATA)
.appendPath(PATH_DATA)
.appendPath("")
.appendQueryParameter(QUERY_DATA, QUERY_ARRAY[3]);
Uri.Builder URL_ISP = new Uri.Builder();
URL_ISP.scheme(SCHEME_DATA)
.authority(AUTH_DATA)
.appendPath(PATH_DATA)
.appendPath("")
.appendQueryParameter(QUERY_DATA, QUERY_ARRAY[4]);
Uri.Builder URL_ORG = new Uri.Builder();
URL_ORG.scheme(SCHEME_DATA)
.authority(AUTH_DATA)
.appendPath(PATH_DATA)
.appendPath("")
.appendQueryParameter(QUERY_DATA, QUERY_ARRAY[5]);
String uriIP = URL_IP.build().toString();
String uriCity = URL_CITY.build().toString();
String uriRegion = URL_REGION.build().toString();
String uriCountry = URL_COUNTRY.build().toString();
String uriIsp = URL_ISP.build().toString();
String uriOrg = URL_ORG.build().toString();
// Try IP Address
try {
Document showIP = Jsoup.connect(uriIP).get();
Document showCity = Jsoup.connect(uriCity).get();
Document showRegion = Jsoup.connect(uriRegion).get();
Document showCountry = Jsoup.connect(uriCountry).get();
Document showIsp = Jsoup.connect(uriIsp).get();
Document showOrg = Jsoup.connect(uriOrg).get();
ip = showIP.text();
c = showCity.text();
rn = showRegion.text();
cn = showCountry.text();
isp = showIsp.text();
org = showOrg.text();
} catch(IOException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute ( Void result ) {
// TODO: Implement this method
ipDetail.setText("IP: " + ip + "\nCity: " + c + "\nRegion: " + rn + "\nCountry: " + cn + "\nISP: " + isp + "\nOrg: " + org);
pip.setText(ip);
super.onPostExecute(result);
}
}
And the onClick Button:
public void saveData(View v) {
SharedPreferences sp = getSharedPreferences("ipList", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("usedIP", pip.toString());
editor.apply();
Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
}
And the Question: How can I get data of class GetIP and store it into SharePreferences?
Upvotes: 1
Views: 29
Reputation: 21
I can solve it, i just forget one thing.
In button onClick()
public void saveData(View v){
SharedPreferences sp = getSharedPreferences("ipList", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
// I forget to adding getText().toString().
editor.putString("usedIP", pip.getText().toString());
editor.apply();
Toast.makeText(this, "Saved", Toast.LENGTH_SHORT).show();
}
Upvotes: 1