2015-05-09 72 views
0

我和我的朋友需要帮助,我们正在尝试为学校项目创建一个小应用程序。首先,这个应用程序的目的是找到我们住的地方的药房。这两个是我试图从其中提取信息的网站(在这家名为拉?)我们如何从这些网页解析信息?

http://www.istanbulsaglik.gov.tr/w/nobet/liste.asp?lc=0&gun=09.05.2015

  • 枪=天/日期

  • LC =镇

  • ID
  • cs每天都会给一个城镇,每天都在变化,这只是一个数字。

在这个页面上。

AND/OR

http://www.nobetcieczanebul.com/

这一个 '看起来' 更容易获得所需的信息。从。

它们都包含我需要的信息;药房的名称,地址和电话号码。通过使用JSOUP,我希望解析(拉?)这些信息,但我需要帮助。我的首要任务是使用'webview'。

首先,我们将从HTML读取cs,然后我们将cs放入代码并调用药店的所有名称,日期和地址。

这里是我的示例代码,这一个来电ID 32日镇的2015年5月5日包含名称等

import android.os.AsyncTask; 
import android.support.v7.app.ActionBarActivity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.webkit.WebView; 
import android.widget.Button; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import org.jsoup.Jsoup; 
import org.jsoup.nodes.Document; 

import java.io.IOException; 

public class MainActivity extends ActionBarActivity { 

Button btnGetir; 
ProgressBar pb; 
TextView tv; 
Button btnNobet; 
TextView tvNobet; 
WebView wv; 

class mytask extends AsyncTask<Void,Void,Void>{ 
String Title; 
@Override 
protected void onPostExecute(Void aVoid) { 
    super.onPostExecute(aVoid); 
    pb.setVisibility(View.INVISIBLE); 
tv.setText(Title); 

} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 

    pb.setVisibility(View.VISIBLE); 
} 

@Override 
protected Void doInBackground(Void... params) { 

    try { 
     Document myDoc= Jsoup.connect("http://www.ticaret.edu.tr").get(); 
     Title=myDoc.title(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    return null; 
    } 
    } 

    class myNobettask extends AsyncTask<Void,Void,Void>{ 
    String Title; 
    @Override 
    protected void onPostExecute(Void aVoid) { 
     super.onPostExecute(aVoid); 
     pb.setVisibility(View.INVISIBLE); 
     wv.loadData(Title, "text/html", "UTF-8"); 

    } 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     pb.setVisibility(View.VISIBLE); 
    } 

    @Override 
    protected Void doInBackground(Void... params) { 

     try { 
      Document myDoc= Jsoup.connect("http://apps.istanbulsaglik.gov.tr/eczane/GetNobetciEczaneler.aspx?lc=32&gun=05.05.2015&cs=6e79f453").get(); 


      Title=myDoc.html(); 


     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
btnGetir=(Button) findViewById(R.id.btnTitleGetir); 
tv=(TextView)findViewById(R.id.tvSonuc); 
    pb=(ProgressBar) findViewById(R.id.progressBar); 

    btnNobet =(Button)findViewById(R.id.btnNobetci); 
    tvNobet=(TextView)findViewById(R.id.tvNobetci); 
wv=(WebView) findViewById(R.id.webView); 

    btnGetir.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mytask newtask= new mytask(); 
      newtask.execute(); 
     } 
    }); 


    btnNobet.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      myNobettask task= new myNobettask(); 
      task.execute(); 
     } 
    }); 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
    } 
} 
+1

是必须使用本网站之一? ,因为您可以使用Google Places API,效率更高,并提供json o xml响应(易于解析) –

+0

我“相信”,因为我们正在努力寻找药房值班。不知道这是否是正确的说法,值班药房 - >全天开放的药店,紧急药房。这两个网站就是展示它的网站。 Google Places API会显示它们吗? –

+0

以下是Google API提供的所有类型的列表:https://developers.google.com/places/supported_types –

回答

0

随着JSoup你应该可以指定要哪个CSS选择器/ HTML组件解析。

Document doc = Jsoup.connect(url).get(); Elements links = doc.select("a[href]"); // This should return a collection of links.

有可能进一步指定所以也许你想返回所有段落标记的一个子集,然后你可以指定:通读Jsoup cookbook

Document doc = Jsoup.connect(url).get(); Elements linksInPTags= doc.select("p > a[href]");

。这很清楚,真的很有帮助。

0

这是一个使用Google Places API的代码片段可能非常有助于:

public static void makeRequest(String url) 
    { 

     HttpClient httpclient = new DefaultHttpClient(); 
     HttpGet httpget = new HttpGet(url); 
     HttpResponse response; 
     try { 
      response = httpclient.execute(httpget); 
      HttpEntity entity = response.getEntity(); 
      if (entity != null) { 
       InputStream instream = entity.getContent(); 
       String result= convertStreamToString(instream); 
       instream.close(); 
      } 
     } catch (Exception e) {} 
    } 

    private String convertStreamToString(InputStream is) { 
     BufferedReader.readLine() 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 

步骤来完成:

1 - 你必须生成执行你的需要(作为一个例子:https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=AddYourOwnKeyHere)URL (有关创建URL的完整说明存在于上面的链接中)

2 - 它是可选的,但在后台线程中使用此方法更可靠

3 - 您必须解析响应(响应可以是json或xml格式,您可以从URL中选择它)来执行您的需求(您可以从上面的链接获取响应字段描述)