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
}