1
我想創建一個應用程序,顯示在我的學校每個餐廳的菜單。我將如何去解析這個網站https://hdh.ucsd.edu/DiningMenus/default.aspx?i=18或https://hdh.ucsd.edu/DiningMenus/default.aspx?i=18並將早餐午餐和晚餐的菜單變成顯示在片段上的列表視圖? 這是我的代碼有: ErcFragment.java如何解析一個網站,並顯示在列表視圖片段android
import android.view.View;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ListView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import android.os.AsyncTask;
import android.app.ProgressDialog;
import android.widget.TextView;
import java.util.ArrayList;
public class ErcFragment extends Fragment {
// URL Address
String url = "http://hdh.ucsd.edu/mobile/dining/locationdetails.aspx?l=18&no_server_init";
ProgressDialog mProgressDialog;
String MENU;
ListView listview;
TextView textview;
@Nullable
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_erc, container, false);
View view = inflater.inflate(R.layout.fragment_erc, container, false);
textview = (TextView)view.findViewById(R.id.textView10);
new JSOUP().execute();
return rootView;
}
public class JSOUP extends AsyncTask<Void, Void, Void>{
ProgressDialog dialog;
@Override
protected void onPreExecute(){
super.onPreExecute();
dialog = new ProgressDialog(getActivity());
dialog.setMessage("loading...");
dialog.show();
}
@Override
protected Void doInBackground(Void... params){
try{
Document document = Jsoup.connect(url).get();
Elements elements = document.select("div[class='message info last']");
for(int i = 0; i<elements.size(); i++){
MENU += "\n" + elements.get(i).text();
}
}
catch (Exception e){
}
return null;
}
@Override
protected void onPostExecute(Void result){
dialog.dismiss();
textview.setText(MENU);
super.onPostExecute(result);
}
}
}
和fragment_erc.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Cafe Ventanas"
android:id="@+id/textView9"
android:layout_gravity="center_horizontal"
android:elegantTextHeight="false"
android:textSize="40dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView10"
android:layout_gravity="center" />
</FrameLayout>
你必須使用JSON或XML解析從服務器獲取數據,並可以使用你的數據在你的應用 – sud
遵循這個答案的解決方案[http://stackoverflow.com/questions/16545378/how-can-我取入數據從-A-web服務器功能於一個-機器人的應用](http://stackoverflow.com/questions/16545378/how-can-i-fetch-data-from-a-web -server-in-an-android-application) – sud
@sud他在這裏沒有處理api,他解析了HTML。 –