forked from hussien89aa/AndroidTutorialForBeginners
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyAsyncTaskgetNews.java
More file actions
Latest commit
86 lines (63 loc) · 2.64 KB
/
Copy pathMyAsyncTaskgetNews.java
File metadata and controls
86 lines (63 loc) · 2.64 KB
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
publicvoidbuloginckic(Viewview) {
//get user name and password
EditTextUserName=(EditText)findViewById(R.id.EDTUserName);
EditTextPassword=(EditText)findViewById(R.id.EDTpassword);
// send user name and password over the http
Stringurl="http://sellingportal.alruabye.net/UsersWS.asmx/Login?UserName="+ UserName.getText().toString() +"&Password="+ Password.getText().toString();
// start background task
newMyAsyncTaskgetNews().execute(url, "news");
}
// get news from server
publicclassMyAsyncTaskgetNewsextendsAsyncTask<String, String, String> {
@Override
protectedvoidonPreExecute() {
//before works
}
@Override
protectedStringdoInBackground(String... params) {
// TODO Auto-generated method stub
try {
StringNewsData;
//define the url we have to connect with
URLurl = newURL(params[0]);
//make connect with url and send request
HttpURLConnectionurlConnection = (HttpURLConnection) url.openConnection();
//waiting for 7000ms for response
urlConnection.setConnectTimeout(7000);//set timeout to 5 seconds
try {
//getting the response data
InputStreamin = newBufferedInputStream(urlConnection.getInputStream());
//convert the stream to string
NewsData = ConvertInputToStringNoChange(in);
//send to display data
publishProgress(NewsData);
} finally {
//end connection
urlConnection.disconnect();
}
}catch (Exceptionex){}
returnnull;
}
protectedvoidonProgressUpdate(String... progress) {
try {
//display response data
Toast.makeText(getApplicationContext(),progress[0],Toast.LENGTH_LONG).show();
} catch (Exceptionex) {
}
}
protectedvoidonPostExecute(Stringresult2){
}
}
// this method convert any stream to string
publicstaticStringConvertInputToStringNoChange(InputStreaminputStream) {
BufferedReaderbureader=newBufferedReader( newInputStreamReader(inputStream));
Stringline ;
Stringlinereultcal="";
try{
while((line=bureader.readLine())!=null) {
linereultcal+=line;
}
inputStream.close();
}catch (Exceptionex){}
returnlinereultcal;
}