how to send 2 values to the php get & get http // localhost / ... php? kk = par1 & zz = par2 ";

1

Hi, I'm having a problem with my web server, and I'm having problems with the web service. ... but I need to assign this to the url of the code ... I thought I had it but somehow my list goes back empty and does not return anything via gson ....
Hi, I have a problem with the php side. look at the code ...

public class Empresa extends AppCompatActivity {

    Button btnfatura;
    private String jsonResult;
    private String caminho = "http://localhost/blablabla.php?kk=par1&zz=par2";
    private TextView txtid;
    private ListView unidade;
    private ArrayList<Tab_Unidade> lista;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_empresa);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        btnfatura = (Button) findViewById(R.id.btnfatura);
        txtid = (TextView)findViewById(R.id.txtid);
        Bundle extras = getIntent().getExtras();
        String filial = extras.getString("Filial");
        txtid.setText(filial);
        accessWebService();

        String caminho2 = caminho;
        caminho2 = caminho2.replace("par1",Preferencia.getCodEmpresa(this));
        caminho2 = caminho2.replace("par2", txtid.getText());
        URL url = null;
        try {
            url = new URL(caminho2);
            Log.i("url Login", url.toString());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }



    }

    private class JsonReadTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(params[0]);
            try {
                HttpResponse response = httpclient.execute(httppost);
                jsonResult = inputStreamToString(
                        response.getEntity().getContent()).toString();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return jsonResult.toString();
        }

        private StringBuilder inputStreamToString(InputStream is) {
            String rLine = "";
            StringBuilder answer = new StringBuilder();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));

            try {
                while ((rLine = rd.readLine()) != null) {
                    answer.append(rLine);
                }
            } catch (IOException e) {
                //e.printStackTrace();
                Toast.makeText(getApplicationContext(),
                        "Error..." + e.toString(), Toast.LENGTH_LONG).show();
            }
            return answer;
        }// end inputstream

        @Override
        protected void onPostExecute(String result) {
            Log.i("TESTE", "" + result);

            //transformando em objeto
            Gson gson = new Gson();
            Type listType = new TypeToken<ArrayList<Tab_Unidade>>(){}.getType();
            lista = gson.fromJson(result, listType);
            Log.i("QTDE de itens", "" + lista.size());
            unidade = (ListView) findViewById(R.id.unidade);

            final ArrayAdapter<Tab_Unidade> adapter = new ArrayAdapter<Tab_Unidade>
                    (Empresa.this, android.R.layout.simple_list_item_1, lista);
            unidade.setAdapter(adapter);

            btnfatura.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent in = new Intent(Empresa.this, Filiais.class);
                    startActivity(in);
                }
            });
        }

    }
    public void accessWebService() {
        JsonReadTask task = new JsonReadTask();
        // passes values for the urls string array
        task.execute(new String[]{caminho});
    }//end acesso web
}
    
asked by anonymous 08.04.2016 / 20:15

3 answers

2

Just change the path you are using at this location

public void accessWebService() {
        JsonReadTask task = new JsonReadTask();
        // passes values for the urls string array
        task.execute(new String[]{caminho});  altere esse "caminho"
    }//end acesso 

In place of this "path" you should put your "path2" as it receives all that you do in this line of code

 String caminho2 = caminho;
        caminho2 = caminho2.replace("par1",Preferencia.getCodEmpresa(this));
        caminho2 = caminho2.replace("par2", txtid.getText());

so in this function

public void accessWebService() {
        JsonReadTask task = new JsonReadTask();
        // passes values for the urls string array
        task.execute(new String[]{caminho2});  com o "caminho alterado
    }//end acesso web

I hope I have helped.

    
24.05.2016 / 19:58
2

You only have to change this line of code

task.execute(new String[]{caminho}); 

for

task.execute(new String[]{caminho2});

This code I copied from you so change the path to path2 that exactly the path that you saved and that will take the information from get

public void accessWebService() {
        JsonReadTask task = new JsonReadTask();
        // passes values for the urls string array
        task.execute(new String[]{caminho});
    }//end acesso web
    
20.05.2016 / 21:31
1

The problem is with the formation of the URL in the path2 string, you can use the following example to construct urls:

Let's say you want to access the url:

link

Uri.Builder builder = new Uri.Builder();
builder.scheme("http")
    .authority("www.meusite.com")
    .appendPath("pagina")
    .appendPath("index")
    .appendQueryParameter("tipo", 1)
    .appendQueryParameter("ordem", "campo")
    .fragment("nome-sessao");
String myUrl = builder.build().toString();
    
08.04.2016 / 21:38