-
Notifications
You must be signed in to change notification settings - Fork 6.1k
/
MyAsyncTaskgetNews.java
86 lines (63 loc) · 2.64 KB
/
MyAsyncTaskgetNews.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
public void buloginckic(View view) {
//get user name and password
EditText UserName=(EditText)findViewById(R.id.EDTUserName);
EditText Password=(EditText)findViewById(R.id.EDTpassword);
// send user name and password over the http
String url="http://sellingportal.alruabye.net/UsersWS.asmx/Login?UserName="+ UserName.getText().toString() +"&Password="+ Password.getText().toString();
// start background task
new MyAsyncTaskgetNews().execute(url, "news");
}
// get news from server
public class MyAsyncTaskgetNews extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
//before works
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
try {
String NewsData;
//define the url we have to connect with
URL url = new URL(params[0]);
//make connect with url and send request
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
//waiting for 7000ms for response
urlConnection.setConnectTimeout(7000);//set timeout to 5 seconds
try {
//getting the response data
InputStream in = new BufferedInputStream(urlConnection.getInputStream());
//convert the stream to string
NewsData = ConvertInputToStringNoChange(in);
//send to display data
publishProgress(NewsData);
} finally {
//end connection
urlConnection.disconnect();
}
}catch (Exception ex){}
return null;
}
protected void onProgressUpdate(String... progress) {
try {
//display response data
Toast.makeText(getApplicationContext(),progress[0],Toast.LENGTH_LONG).show();
} catch (Exception ex) {
}
}
protected void onPostExecute(String result2){
}
}
// this method convert any stream to string
public static String ConvertInputToStringNoChange(InputStream inputStream) {
BufferedReader bureader=new BufferedReader( new InputStreamReader(inputStream));
String line ;
String linereultcal="";
try{
while((line=bureader.readLine())!=null) {
linereultcal+=line;
}
inputStream.close();
}catch (Exception ex){}
return linereultcal;
}