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

Asked

Viewed 77 times

1

Good afternoon guys I’m having a little problem... I have two values to assign the connection php via web serves... the values are saved in sharepreference and saved in "txtid"... to everything ok all right... however I need to assign this to the url of the code ... I thought I had managed but somehow my list returns empty and does not return anything via gson....
Obs: by php side this all right ... tested and approved ... only missing by android studio 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
}

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 

instead of this "path"

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

therefore 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

hope I’ve helped.

2

You have to change only 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"

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

1

The problem is in forming the URL in the way2 string, you can use the following example to build urls:

Let’s say you want to access the url:

http://www.meusite.com.br/pagina/index?tipo=1&ordem=campo#sessão-name

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();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.