Как передать параметр в RSS парсер
06.10.2013, 17:37. Показов 1442. Ответов 5
Здравствуйте уважаемые!
В своем Android приложении использую RSS парсер по описанию на сайте anddev.ru.
RSS ленты просматриваю на сайте http://www.top68.ru, где они представлены в виде ссылок
Политика http://www.top68.ru/rss-news-policy
Экономика http://www.top68.ru/rss-news-economy
Общество http://www.top68.ru/rss-news-society
Проишествия http://www.top68.ru/rss-news-incidents
Спорт http://www.top68.ru/rss-news-sports
Культура http://www.top68.ru/rss-news-culture
Здоровье http://www.top68.ru/rss-news-health
Закон http://www.top68.ru/rss-news-thelaw
Персона http://www.top68.ru/rss-news-person
Официально http://www.top68.ru/rss-news-officially
Краеведение http://www.top68.ru/rss-news-studyoflocallore
соответственно имеется layout new.xml
| XML | 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:background="#fffabf8f" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:background="@drawable/backv2"
android:paddingBottom="10.0dip"
android:paddingLeft="10.0dip"
android:paddingRight="10.0dip"
android:paddingTop="10.0dip" >
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
<Button
android:id="@+id/bt1"
android:layout_width="fill_parent"
android:layout_height="43.0dip"
android:background="@drawable/button_state"
android:text="@string/news_1"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<Button
android:id="@+id/bt2"
android:layout_width="fill_parent"
android:layout_height="43.0dip"
android:background="@drawable/button_state"
android:text="@string/news_2"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<Button
android:id="@+id/bt3"
android:layout_width="fill_parent"
android:layout_height="43.0dip"
android:background="@drawable/button_state"
android:text="@string/news_3"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<Button
android:id="@+id/bt4"
android:layout_width="fill_parent"
android:layout_height="43.0dip"
android:background="@drawable/button_state"
android:text="@string/news_4"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<Button
android:id="@+id/bt5"
android:layout_width="fill_parent"
android:layout_height="43.0dip"
android:background="@drawable/button_state"
android:text="@string/news_5"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<Button
android:id="@+id/bt6"
android:layout_width="fill_parent"
android:layout_height="43.0dip"
android:background="@drawable/button_state"
android:text="@string/news_6"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<Button
android:id="@+id/bt7"
android:layout_width="fill_parent"
android:layout_height="43.0dip"
android:background="@drawable/button_state"
android:text="@string/news_7"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<Button
android:id="@+id/bt8"
android:layout_width="fill_parent"
android:layout_height="43.0dip"
android:background="@drawable/button_state"
android:text="@string/news_8"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<Button
android:id="@+id/bt9"
android:layout_width="fill_parent"
android:layout_height="43.0dip"
android:background="@drawable/button_state"
android:text="@string/news_9"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<Button
android:id="@+id/bt10"
android:layout_width="fill_parent"
android:layout_height="43.0dip"
android:background="@drawable/button_state"
android:text="@string/news_10"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
<Button
android:id="@+id/bt11"
android:layout_width="fill_parent"
android:layout_height="43.0dip"
android:background="@drawable/button_state"
android:text="@string/news_11"
android:textColor="@color/black"
android:textSize="15sp"
android:textStyle="bold" />
</LinearLayout>
</ScrollView>
</LinearLayout> |
|
Основное Activity - NEW.java
| 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
| package ru.sample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
public class NEWS extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE); //скрываем заголовок
//скрываем статусбар:
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setContentView(R.layout.news);
Button localButton1 = (Button)findViewById(R.id.bt1);
Button localButton2 = (Button)findViewById(R.id.bt2);
Button localButton3 = (Button)findViewById(R.id.bt3);
Button localButton4 = (Button)findViewById(R.id.bt4);
Button localButton5 = (Button)findViewById(R.id.bt5);
Button localButton6 = (Button)findViewById(R.id.bt6);
Button localButton7 = (Button)findViewById(R.id.bt7);
Button localButton8 = (Button)findViewById(R.id.bt8);
Button localButton9 = (Button)findViewById(R.id.bt9);
Button localButton10 = (Button)findViewById(R.id.bt10);
Button localButton11 = (Button)findViewById(R.id.bt11);
localButton1.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
try
{
Intent localIntent = new Intent(paramAnonymousView.getContext(), RSSActivity.class);
NEWS.this.startActivity(localIntent);
return;
}
catch (Exception localException)
{
while (true)
Toast.makeText(paramAnonymousView.getContext(), localException.getMessage(), 1).show();
}
}
});
/*
localButton2.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
try
{
Intent localIntent = new Intent(paramAnonymousView.getContext(), ViewNews_Econom.class);
NEWS.this.startActivity(localIntent);
return;
}
catch (Exception localException)
{
while (true)
Toast.makeText(paramAnonymousView.getContext(), localException.getMessage(), 1).show();
}
}
});
localButton3.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
try
{
Intent localIntent = new Intent(paramAnonymousView.getContext(), ViewNews_Sociaty.class);
NEWS.this.startActivity(localIntent);
return;
}
catch (Exception localException)
{
while (true)
Toast.makeText(paramAnonymousView.getContext(), localException.getMessage(), 1).show();
}
}
});
localButton4.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
try
{
Intent localIntent = new Intent(paramAnonymousView.getContext(), ViewNews_Accident.class);
NEWS.this.startActivity(localIntent);
return;
}
catch (Exception localException)
{
while (true)
Toast.makeText(paramAnonymousView.getContext(), localException.getMessage(), 1).show();
}
}
});
localButton5.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
try
{
Intent localIntent = new Intent(paramAnonymousView.getContext(), ViewNews_Sport.class);
NEWS.this.startActivity(localIntent);
return;
}
catch (Exception localException)
{
while (true)
Toast.makeText(paramAnonymousView.getContext(), localException.getMessage(), 1).show();
}
}
});
localButton6.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
try
{
Intent localIntent = new Intent(paramAnonymousView.getContext(), ViewNews_Culture.class);
NEWS.this.startActivity(localIntent);
return;
}
catch (Exception localException)
{
while (true)
Toast.makeText(paramAnonymousView.getContext(), localException.getMessage(), 1).show();
}
}
});
localButton7.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
try
{
Intent localIntent = new Intent(paramAnonymousView.getContext(), ViewNews_Health.class);
NEWS.this.startActivity(localIntent);
return;
}
catch (Exception localException)
{
while (true)
Toast.makeText(paramAnonymousView.getContext(), localException.getMessage(), 1).show();
}
}
});
localButton8.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
try
{
Intent localIntent = new Intent(paramAnonymousView.getContext(), ViewNews_Law.class);
NEWS.this.startActivity(localIntent);
return;
}
catch (Exception localException)
{
while (true)
Toast.makeText(paramAnonymousView.getContext(), localException.getMessage(), 1).show();
}
}
});
localButton9.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
try
{
Intent localIntent = new Intent(paramAnonymousView.getContext(), ViewNews_Person.class);
NEWS.this.startActivity(localIntent);
return;
}
catch (Exception localException)
{
while (true)
Toast.makeText(paramAnonymousView.getContext(), localException.getMessage(), 1).show();
}
}
});
localButton10.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
try
{
Intent localIntent = new Intent(paramAnonymousView.getContext(), ViewNews_Officially.class);
NEWS.this.startActivity(localIntent);
return;
}
catch (Exception localException)
{
while (true)
Toast.makeText(paramAnonymousView.getContext(), localException.getMessage(), 1).show();
}
}
});
localButton11.setOnClickListener(new View.OnClickListener()
{
public void onClick(View paramAnonymousView)
{
try
{
Intent localIntent = new Intent(paramAnonymousView.getContext(), ViewNews_Region.class);
NEWS.this.startActivity(localIntent);
return;
}
catch (Exception localException)
{
while (true)
Toast.makeText(paramAnonymousView.getContext(), localException.getMessage(), 1).show();
}
}
});
*/
}
} |
|
Работает только первая кнопка, остальные закомментированы.
Остальные Activity
RSSActivity.java
| 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
| package ru.sample;
import java.util.ArrayList;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
public class RSSActivity extends ListActivity {
private ArrayList<PostItem> messages;
private PostAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
messages = savedInstanceState.getParcelableArrayList("messages");
adapter = new PostAdapter(RSSActivity.this, R.layout.post_entry,
messages);
setListAdapter(adapter);
} else
new GetParserResult().execute();
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent postActivity = new Intent(this, PostActivity.class);
Bundle store = new Bundle();
store.putString("title", messages.get(position).title);
store.putString("description", messages.get(position).description);
store.putString("date", messages.get(position).date);
store.putString("link", messages.get(position).link);
postActivity.putExtras(store);
startActivity(postActivity);
}
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putParcelableArrayList("messages", messages);
}
private class GetParserResult extends
AsyncTask<Context, Integer, ArrayList<PostItem>> {
private ProgressDialog loadingDialog;
private NewParser parser;
protected void onPreExecute() {
parser = new NewParser();
loadingDialog = ProgressDialog.show(RSSActivity.this, "",
"Загружается лента новостей\nПожалуйста, подождите...", true);
}
@Override
protected ArrayList<PostItem> doInBackground(Context... arg0) {
return parser.parse();
}
protected void onPostExecute(ArrayList<PostItem> result) {
if (result == null) {
AlertDialog.Builder builder = new AlertDialog.Builder(
RSSActivity.this);
builder.setMessage("Ошибка загрузки ленты новостей с www.top68.ru")
.setCancelable(false)
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int id) {
dialog.cancel();
loadingDialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
messages = result;
loadingDialog.dismiss();
adapter = new PostAdapter(RSSActivity.this,
R.layout.post_entry, messages);
setListAdapter(adapter);
}
}
}
} |
|
NewParser.java
| 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
| package ru.sample;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.util.Xml;
public class NewParser {
protected InputStream getInputStream() {
URL feedUrl = null;
try {
feedUrl = new URL("http://www.top68.ru/rss-news-policy");
} catch (MalformedURLException e1) {
feedUrl = null;
}
try {
return feedUrl.openConnection().getInputStream();
} catch (IOException e) {
return null;
}
}
public ArrayList<PostItem> parse() {
final PostItem currentPost = new PostItem();
final ArrayList<PostItem> messages = new ArrayList<PostItem>();
RootElement root = new RootElement("rss");
Element channel = root.getChild("channel");
Element item = channel.getChild("item");
item.setEndElementListener(new EndElementListener() {
public void end() {
messages.add(currentPost.copy());
}
});
item.getChild("title").setEndTextElementListener(
new EndTextElementListener() {
public void end(String body) {
currentPost.title = body;
}
});
item.getChild("link").setEndTextElementListener(
new EndTextElementListener() {
public void end(String body) {
currentPost.link = body;
}
});
item.getChild("description").setEndTextElementListener(
new EndTextElementListener() {
public void end(String body) {
currentPost.description = body;
}
});
item.getChild("pubDate").setEndTextElementListener(
new EndTextElementListener() {
public void end(String body) {
currentPost.setDate(body);
}
});
try {
Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8,
root.getContentHandler());
} catch (Exception e) {
return null;
}
return messages;
}
} |
|
PostActivity.java
| 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
| package ru.sample;
import android.app.Activity;
import android.os.Bundle;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
public class PostActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.post);
TextView title = (TextView) findViewById(R.id.title);
TextView description = (TextView) findViewById(R.id.description);
TextView date = (TextView) findViewById(R.id.date);
Bundle store = getIntent().getExtras();
title.setText(store.getString("title").replace("[...]", ""));
String tDescription = store.getString("description").replace("[...]", "") +
"<br><a href='"+ store.getString("link") +"'>" + getString(R.string.read_more_text) + "</a>";
description.setMovementMethod(LinkMovementMethod.getInstance());
description.setText(Html.fromHtml(tDescription));
date.setText(store.getString("date"));
}
} |
|
PostAdapter.java
| 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
| package ru.sample;
import java.util.ArrayList;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class PostAdapter extends ArrayAdapter<PostItem> {
public ArrayList<PostItem> messages;
public LayoutInflater inflater;
public PostAdapter(Activity context, int resource,
ArrayList<PostItem> objects) {
super(context, resource, objects);
messages = objects;
inflater = LayoutInflater.from(context);
}
static class ViewHolder {
public TextView titleView;
public TextView pubDateView;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.post_entry, null, true);
holder = new ViewHolder();
holder.titleView = (TextView) convertView
.findViewById(R.id.post_title);
holder.pubDateView = (TextView) convertView
.findViewById(R.id.post_pubDate);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.titleView.setText(messages.get(position).title);
holder.pubDateView.setText((CharSequence) messages.get(position).date);
return convertView;
}
} |
|
PostItem.java
| 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
| package ru.sample;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Locale;
import android.os.Parcel;
import android.os.Parcelable;
public class PostItem implements Parcelable {
static SimpleDateFormat FORMATTER = new SimpleDateFormat(
"E, dd MMM yyyy HH:mm:ss Z", Locale.US);
static SimpleDateFormat OUT_FORMATTER = new SimpleDateFormat(
"dd MMM yyyy HH:mm");
public String title;
public String link;
public String description;
public String date;
public PostItem() {
title = "";
link = "";
description = "";
date = "";
}
public void setDate(String date) {
try {
this.date = (String) OUT_FORMATTER.format(FORMATTER.parse(date
.trim()));
} catch (ParseException e) {
this.date = null;
}
}
public PostItem copy() {
PostItem copy = new PostItem();
copy.title = title;
copy.link = link;
copy.description = description;
copy.date = date;
return copy;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeString(title);
out.writeString(link);
out.writeString(description);
out.writeString(date);
}
public static final Parcelable.Creator<PostItem> CREATOR = new Parcelable.Creator<PostItem>() {
public PostItem createFromParcel(Parcel source) {
return new PostItem(source);
}
public PostItem[] newArray(int size) {
return new PostItem[size];
}
};
private PostItem(Parcel source) {
title = source.readString();
date = source.readString();
link = source.readString();
description = source.readString();
}
} |
|
Первая кнопка - в NewParser
feedUrl = new URL("http://www.top68.ru/rss-news-policy");
ВОПРОС!!!
Как сделать так, чтобы при нажатии на вторую кнопку - в NewParser
feedUrl = new URL("http://www.top68.ru/rss-news-economy ");
при нажатии на третью кнопку - в NewParser
feedUrl = new URL("http://www.top68.ru/rss-news-society ");
и т.д.
Заранее благодарен
ALSHA
Добавлено через 12 часов 5 минут
Да....
Ну хоть кто-нибудь, ответьте.
Принимаются любые соображения.
ALSHA
0
|