我疯狂地想弄清楚为什么这种方法不能给我想要的结果。该方法工作正常,吐司打印我放在应用程序中的代码,当我按下此方法中描述的按钮加载cllente时,都返回空值。有什么想法吗?
popupButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
GetClienteByCodiceCliente task = new GetClienteByCodiceCliente(){
@Override
protected void onPostExecute(ClienteBean clienteBean) {
clienteSconto = clienteBean;
nomeCliente.setText(clienteBean.getNome() + " " + clienteBean.getCognome());
monteVincite.setText(clienteBean.getMonteVincite()+" €");
monteScontoCliente.setText(clienteBean.getMonteSconto()+" €");
super.onPostExecute(clienteBean);
}
};
TextView codiceCliente = (TextView)findViewById(R.id.codice_cliente);
String codice = codiceCliente.getText().toString();
task.execute(codice);
Context context = getApplicationContext();
int duration = Toast.LENGTH_SHORT;
String text = "FIND CODE: "+codice;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
}
});
GetClienteByCodiceCliente.class
public class GetClienteByCodiceCliente extends AsyncTask<Object, Void, ClienteBean> {
private static final String SERVER_URL = "http://95.110.231.118:8080/TestWSRest/GetClienteByCodiceCliente";
@Override
protected ClienteBean doInBackground(Object... params) {
ClienteBean cliente = new ClienteBean();
try {
Log.d("codiceCliente", params[0].toString());
String codiceCliente = params[0].toString();
cliente = getCliente(codiceCliente);
} catch (Exception e) {
e.printStackTrace();
}
return cliente;
}
public ClienteBean getCliente(String codiceCliente) throws Exception {
ClienteBean cliente = new ClienteBean();
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(SERVER_URL);
ArrayList<NameValuePair> postParameters;
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("codiceCliente", codiceCliente));
post.setEntity(new UrlEncodedFormEntity(postParameters));
HttpResponse response = client.execute(post);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == 200) {
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
try {
//Read the server response and attempt to parse it as JSON
Reader reader = new InputStreamReader(content);
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setDateFormat("yyyy-MM-dd");
Gson gson = gsonBuilder.create();
cliente = new ClienteBean();
cliente = gson.fromJson(reader, ClienteBean.class);
content.close();
Log.d("jsonResponse", cliente.toString());
} catch (Exception ex) {
throw ex;
}
}else{
throw new HttpException();
}
return cliente;
}
}
答案 0 :(得分:1)
我认为cliente = gson.fromJson(reader, ClienteBean.class);
将cliente
设置为null然后返回它。