我和我的朋友需要帮助我们为一个学校项目创建一个小应用程序。首先,这个应用程序的目的是在我们居住的地方寻找药房。这两个网站是我试图从其中提取信息的网站(这叫“拉”吗?)
http://www.istanbulsaglik.gov.tr/w/nobet/liste.asp?lc=0&gun=09.05.2015
在这一页。
和/或
http://www.nobetcieczanebul.com/
这个“看起来”更容易获得所需的信息。从…。
它们都包含了我需要的信息,药房的名字,地址,也许还有电话号码。通过使用JSOUP,我希望解析(拉?)但我需要帮助。我的首要任务是使用'webview‘。
首先,我们将从HTML中读取cs,然后将cs放入代码中,并调用药店的所有名称、日期和地址。
下面是我的示例代码,这个代码调用id 32 date of date 05/05/2015,其中包含名称等。
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);
}
}发布于 2015-05-09 17:14:25
使用JSoup,您应该能够指定要解析哪些CSS选择器/ HTML组件。
Document doc = Jsoup.connect(url).get(); Elements links = doc.select("a[href]"); // This should return a collection of links.
可以进一步指定,因此您可能希望返回所有段落标记的子集,然后可以指定:
Document doc = Jsoup.connect(url).get(); Elements linksInPTags= doc.select("p > a[href]");
通过汤菜谱阅读。很清楚也很有帮助。
发布于 2015-05-09 17:18:04
下面是使用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-您必须生成实现您的需要的URL (例如:https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=-33.8670522,151.1957362&radius=500&types=food&name=cruise&key=AddYourOwnKeyHere )(上面的链接中有创建URL的完整描述)
2-在后台线程中使用此方法是可选的,但更可靠
3-您必须解析响应(响应可以是json或xml格式,您可以从URL中选择)数据来执行您的需求(您可以从上面的链接获得响应字段的描述)
https://stackoverflow.com/questions/30142207
复制相似问题