将参数传递给 URL AsyncTask android

Pass parameter to a URL AsyncTask android

我正在开发一个应用程序,现在我必须将参数传递给 RESTful 服务的 URL。
我正在使用 AsyncTask,我需要传递一个列表视图中的文本作为 URL 的参数,例如:
URL 是 http://ip:7001/product?product_name=PARAM
我需要从所选中获取文本我的列表视图中的项目,并使用 AsyncTask 在 PARAM 中作为参数传递。
我已经从列表视图中的项目中获得了文本,现在我只需要将它作为参数传递。

这是我的 AsycTask class:

package com.tumta.henrique.teste;

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import com.tumta.henrique.teste.ProdutoFragment;

/**
 * Created by Henrique on 18/05/2015.
 */
public class FiltraProduto extends AsyncTask<String, Void, List<String>> {

    private ConsultaConcluidaFiltroProdutoListener listener;

    public static String URL_STRING = "http://192.168.0.20:7001/com.henrique.rest/api/v1/status/pro_filtro?pro_nome=";

    public FiltraProduto(ConsultaConcluidaFiltroProdutoListener listener) {
        this.listener = listener;
    }





    private List<String> InterpretaResultado(String resultado) throws JSONException {
        JSONObject object = new JSONObject(resultado);
        JSONArray jsonArray = object.getJSONArray("produto");
        //JSONObject jsonProduto = jsonArray.getJSONObject(0);
       // String id = jsonProduto.getString("pro_id");
        //proId = id;
        List<Object> listaNomes = new ArrayList<>();
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonProdutoInfo = jsonArray.getJSONObject(i);
            String proNome= jsonProdutoInfo.getString("pro_nome");
            double proPreco = jsonProdutoInfo.getDouble("pro_preco");
            double proSdAtual = jsonProdutoInfo.getDouble("pro_sdAtual");
            listaNomes.add(i, proNome);
            listaNomes.add(i, proPreco);
            listaNomes.add(i, proSdAtual);
        }
        List<String> strings = new ArrayList<String>();
        for (Object o : listaNomes) {
            strings.add(o != null ? o.toString() : null);
        }
        return strings;
    }

    private String ConsultaServidor() throws IOException {
        InputStream is = null;

        try {
            URL url = new URL(URL_STRING);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setConnectTimeout(10000);
            conn.setReadTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            conn.getResponseCode();

            is = conn.getInputStream();

            Reader reader = null;
            reader = new InputStreamReader(is);
            char[] buffer = new char[2048];
            reader.read(buffer);
            return new String(buffer);
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

    @Override
    protected List<String> doInBackground(String... params) {
        try {
            String resultado = ConsultaServidor();
            return InterpretaResultado(resultado);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(List<String> result) {
        listener.onConsultaConcluida(result);
        super.onPostExecute(result);
    }


    public interface ConsultaConcluidaFiltroProdutoListener {
        void onConsultaConcluida(List<String> result);
    }
}

在 URL_STRING 中,我需要在 pro_nome=?

处传递参数

在这里我得到了项目文本。这是在我的具有列表视图的片段中:

 public String retornaParam(String param){
        return param;
    }

    @Override
    public void onConsultaConcluida(List<String> result) {
        final ListView listaProdutos = (ListView) getView().findViewById(R.id.listaprodutos);
        ArrayAdapter arrayAdapter = new ArrayAdapter<>(getView().getContext(),android.R.layout.simple_list_item_1, result);
        listaProdutos.setAdapter(arrayAdapter);
        listaProdutos.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
                                    long id) {
                String nomeProduto = listaProdutos.getItemAtPosition(position).toString();
                retornaParam(nomeProduto);
                Intent intent = new Intent(getActivity(), DetalhesProdutoActivity.class);
                //intent.putExtra("pro_nome", listaProdutos.getItemAtPosition(position).toString());
                startActivity(intent);
            }
        });
    }

我从 retornaParam 方法获取文本并将其存储在参数中。
有人知道怎么做吗?
如果您需要更多信息,请告诉我。

只需在发送执行您的 httpClient 之前添加以下代码:

URL_STRING + = textInsideYourTextView;

它应该可以工作,只是避免在 UI 线程之外操作 ui 元素。

您使用以下方式将参数传递给 AsyncTask:

YourAsyncTask.execute(yourview.getText(), "and", "more", "params");

然后您可以在

中访问它们
@Override
protected String doInBackground(String... params) {
URL_STRING += params[0];
...