簡體   English   中英

如何從php頁面(沒有json)獲取數據?

[英]How can I get data from php page (without json)?

我有這個網站( http://www.usadebtclock.com/us-debt-clock-widget.php ),我想使用改造2從其中獲取數字(沒有JSON)到我的android studio應用程序,並顯示該數字在文本視圖中。 我應該使用哪個轉換器工廠代替GsonConverterFactory? 或者如何從該頁面獲取數字到我的文本視圖?

我試圖為此使用GsonConverterFactory.create() ,但它沒有做出響應,並顯示了onFailure方法的日志

這是MainActivity.class (沒有用的GsonConverterFactory):

public class MainActivity extends AppCompatActivity {

    TextView tv;
    Button btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv = findViewById(R.id.textview1);
        btn = findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl(DolgAPI.HOST)
                        .addConverterFactory(GsonConverterFactory
                                .create())
                        .build();

                DolgAPI apiService = retrofit.create(DolgAPI.class);

                Call<String> call = apiService.getDolg();

                call.enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        tv.setText(response.body());
                        Log.d("MyLog", "Got it!");
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {
                        Log.d("MyLog", "Error");
                    }
                });


            }
        });
    }
}

這是Retrofit2響應API:

public interface DolgAPI {

    String HOST = "http://www.usadebtclock.com";

    @GET("us-debt-clock-widget.php")
    Call<String> getDolg();

}

我使用WebView從該網站顯示價值

它包含JavaScript,因此我使用了以下幾行以允許在應用程序中執行Web腳本:

WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);

首先,它看起來像網站,但由於無法加載而無法正常運行。 但是,如果假定它正在運行並且您沒有任何GET /端點,則您將收到完整的HTML文檔,並且需要通過html標記對其進行解析以獲取所需的文本。

您應該使用ResponseBody

getDolg() should return Call<ResponseBody>

然后,您必須解析html。 貸款以這種格式退還

<span id="debt-clock">$21,954,170,905,365.84</span>

您可以使用正則表達式解析html。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM