-
Notifications
You must be signed in to change notification settings - Fork 0
/
AsyncTaskLoader.java
96 lines (70 loc) · 2.45 KB
/
AsyncTaskLoader.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package android.egypt.Mina.myapp;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;
import org.json.JSONException;
import java.io.BufferedReader;
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 AsyncTaskLoader extends AsyncTask<Void,Void,String[]> {
Context myContext;
ProgressDialog dialog;
String dawt;
Parsor parsor;
String JSONCode;
public AsyncTaskLoader(Context context,String Url,Parsor parsor){
myContext=context;
dawt=Url;
this.parsor=parsor;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
Toast.makeText(myContext, "The Application is started", Toast.LENGTH_SHORT).show();
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
dialog=new ProgressDialog(myContext);
dialog.setMessage("Loading.....");
dialog.show();
dialog.setCanceledOnTouchOutside(false);
}
@Override
protected String[] doInBackground(Void... params) {
publishProgress();
try {
URL url=new URL(dawt);
HttpURLConnection urlConnection= (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream= urlConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));
StringBuilder JsonCodeBuilder=new StringBuilder();
String JsonLine;
while((JsonLine =bufferedReader.readLine())!=null)
{
JsonCodeBuilder.append(JsonLine+'\n');
}
JSONCode=JsonCodeBuilder.toString();
Log.i("Json", JSONCode);
return parsor.parsing(JSONCode);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
public String getJSONCode() {
return JSONCode;
}
}