简体   繁体   中英

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

I have this site ( http://www.usadebtclock.com/us-debt-clock-widget.php ) and I want to get number from it (there's no JSON) to my android studio application using retrofit 2 and show this number in the text view. What converter factory should I use instead GsonConverterFactory? Or how can I get number from this page to my text view?

I tried to use GsonConverterFactory.create() for this, but it didn't make a response and showed a log from onFailure method

This is MainActivity.class (with useless 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");
                    }
                });


            }
        });
    }
}

This is Retrofit2 response API:

public interface DolgAPI {

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

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

}

I used WebView to dispaly value from that site

It contains JavaScript so I used next lines to allow executing web scripts in application:

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

First of all looks like the website if not functional as it not loading. But if assume it is working and you don't have there any GET/ endpoint, you will receive back whole HTML document, and you need to parse it via html tags to fetch the required text.

You should use ResponseBody

getDolg() should return Call<ResponseBody>

Then you have to parse html. Loan returns in this format

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

You can parse html using regex.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM