从android应用搜索数据到网站,并将结果提取到android

时间:2018-08-24 10:19:03

标签: android

我有一个android应用程序,当我单击按钮时,我想从网站上搜索内容,然后我想在该应用程序上获取结果,但是我很困惑,我也不知道该怎么做。 例如,当我单击搜索按钮(pic1)时,我想在后台搜索一些字符串(pic2),并将结果带回应用程序(pic3-红色方块)。

enter image description here

enter image description here

enter image description here 我唯一要做的就是从网站(https://world.openfoodfacts.org/)带文本。

package com.example.owner.foodal11_2.activity;


import android.app.Activity;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.provider.DocumentsContract;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.owner.foodal11_2.R;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.io.IOException;


/**
 * Created by Owner on 17/7/2018.
 */

public class GetDataFromWebsite extends AppCompatActivity {

    TextView text;

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

        text=(TextView) findViewById(R.id.resultText);
        Button but= (Button)findViewById(R.id.getRes);
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new doit().execute();

            }
        });
        }

    public class doit extends AsyncTask<Void,Void,Void>{
String words;

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

            try {
                Document doc= Jsoup.connect("https://world.openfoodfacts.org/").get();

            words=doc.text();


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


            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            text.setText(words);
        }
    }

}

2 个答案:

答案 0 :(得分:0)

您可以使用Jsoup提交想要在网站中搜索的字符串,例如:

Connection.Response res = Jsoup.connect("http://www.example.com/login.php")
.data("username", "myUsername", "password", "myPassword")
.method(Method.POST)
.execute();

文档doc = res.parse(); 然后从中解析所需的数据。

答案 1 :(得分:0)

使用world.openfoodfacts.org JSON API而不是解析html内容:

public class FetchAllergensTask extends AsyncTask<String, Void, String> {

    protected String doInBackground(String... strings) {
        final String barcode = strings[0];
        @Nullable
        String allergens = null;
        try {
            final String jsonStr = Jsoup.connect(
                    "https://world.openfoodfacts.org/api/v0/product/" + barcode + ".json")
                    .ignoreContentType(true)
                    .execute()
                    .body();

            final JSONObject jsonObj = new JSONObject(jsonStr);
            if (jsonObj.has("product")) {
                JSONObject productNode = jsonObj.getJSONObject("product");
                allergens = productNode.getString("allergens");
            }
        } catch (IOException | JSONException e) {
            e.printStackTrace();
        }
        return allergens;
    }

    @Override
    protected void onPostExecute(@Nullable String result) {
        super.onPostExecute(result);
        // setText(result);
    }
}

...

new FetchAllergensTask().execute("5200334220012");
相关问题