Version 0.1 ist fertig
[yacyandroid:yacyandroid.git] / src / net / yacy / yacyAndroid / YacyAndroidActivity.java
1 package net.yacy.yacyAndroid;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStreamReader;
6 import java.util.ArrayList;
7
8 import net.yacy.rss.Feed;
9 import net.yacy.rss.FeedMessage;
10 import net.yacy.rss.RSSFeedParser;
11
12 import org.apache.http.HttpResponse;
13 import org.apache.http.client.ClientProtocolException;
14 import org.apache.http.client.methods.HttpGet;
15 import org.apache.http.client.methods.HttpPost;
16 import org.apache.http.impl.client.DefaultHttpClient;
17
18 import android.app.Activity;
19 import android.net.ConnectivityManager;
20 import android.content.Context;
21 import android.os.Bundle;
22 import android.text.util.Linkify;
23 import android.view.View;
24 import android.widget.EditText;
25 import android.widget.TextView;
26 import android.widget.Toast;
27
28 public class YacyAndroidActivity extends Activity 
29 {
30         // Beispiel: http://search.yacy.net/yacysearch.html
31         // ?query=tagesspiegel&Enter=Search&verify=false
32         // &nav=hosts%2Ctopics%2Cfiletype&startRecord=0
33         // &resource=global&urlmaskfilter=.*&prefermaskfilter=
34         // &indexof=off&meanCount=5&maximumRecords=10
35         StringBuilder url = new StringBuilder("http://search.yacy.net/yacysearch.rss?query=");
36         String urlRest = new String("&Enter=Search&verify=false&nav=hosts%2Ctopics%2Cfiletype&startRecord=0&resource=global&urlmaskfilter=.*&prefermaskfilter=&indexof=off&meanCount=5&maximumRecords=10");
37         
38     /** Called when the activity is first created. */
39     @Override
40     public void onCreate(Bundle savedInstanceState) 
41     {
42         super.onCreate(savedInstanceState);
43         setContentView(R.layout.main);
44     }
45     
46     /** Send the search-query to yacy */
47     public void onClickBtnSend(View view)
48     {
49 Context context = getApplicationContext();
50         final DefaultHttpClient client = new DefaultHttpClient();
51         final HttpGet get ;
52         final HttpResponse response;
53         final EditText eingabe;
54         final StringBuilder tmp = new StringBuilder();
55         final RSSFeedParser parser = new RSSFeedParser();
56         final Feed feed; 
57         final ArrayList liste;
58         FeedMessage fm = new FeedMessage();
59         TextView ausgabe;
60         
61         try
62                 {
63                 // Ein- und Ausgabefeld vorbereiten
64                 eingabe = (EditText)findViewById(R.id.eingabe);
65                 ausgabe = (TextView) findViewById(R.id.ausgabe);
66                 url.append(eingabe.getText());
67                 url.append(urlRest);
68                 get = new HttpGet(url.toString());
69                 response = client.execute(get);
70                 // Rückgabe auslesen
71                 BufferedReader rd = new BufferedReader(new InputStreamReader(
72                                         response.getEntity().getContent()));
73                         String line = "";
74                                                 
75                         while ((line = rd.readLine()) != null) 
76                         {
77                                 tmp.append(line);                               
78                         
79                         }
80                         // Rückgabe parsen
81                         liste = parser.readFeed(tmp);
82                         for (int i = 0; i < liste.size(); i++)
83                         {
84                                 fm = (FeedMessage) liste.get(i);
85                                 ausgabe.append(System.getProperty("line.separator"));
86                                 ausgabe.append(fm.getLink());
87                                 ausgabe.append(System.getProperty("line.separator"));
88                                 ausgabe.append(fm.getTitle());
89 Linkify.addLinks(ausgabe, Linkify.WEB_URLS);                            
90                         }
91 //Toast toast = Toast.makeText(context, tmp.toString(), Toast.LENGTH_LONG);                     
92 //toast.show();                         
93                 }
94                 catch (ClientProtocolException e)
95                 {
96                         // TODO Auto-generated catch block
97                         e.printStackTrace();
98                 }
99                 catch (IOException e)
100                 {
101                         // TODO Auto-generated catch block
102                         e.printStackTrace();
103                 }
104         catch (Exception e)
105                 {
106                         // TODO Auto-generated catch block
107                         e.printStackTrace();
108                 }
109 Toast toast = Toast.makeText(context, "Programmende", Toast.LENGTH_LONG);                       
110 toast.show();           
111         
112         
113         
114     }
115 }