我是Android的新手,这是我的第一个应用程序。我在最近几天遇到了这个问题,只是放弃寻找答案。我非常感谢你的帮助。
单击按钮时会调用sendSelectedItem方法。在else if子句中,方法setTvText和setTvColor被简单地忽略,它们应该设置TextView的Text和Color但不做任何事情。接下来是我的web服务器上的php脚本上的httpPOST请求,它返回一个简单的String,这部分工作正常。
public void sendSelectedItem(View v){
if(v == findViewById(R.id.btnCancel)){
setTvText("nothing happens");
setTvColor(0);
}
else if (v == findViewById(R.id.btnSend)){
setTvText("selection transmitted");
setTvColor(1);
/*SelectedItemHttpPost si = new SelectedItemHttpPost();
try {
String retResp = si.executeHttpPost(selectedItem);
setTvText(retResp);
setTvColor(2);
}
catch(Exception e){};*/
}
}
您是否有想法,为什么只有在剩余的代码被注释掉时才执行setTvText和setTvColor,如上所述?
如果有帮助,我会粘贴SelectedItemHttpPost的代码。 非常感谢你。
public class SelectedItemHttpPost{
private HttpClient client = null;
private HttpPost request = null;
private HttpResponse response = null;
private List<NameValuePair> postParameters = null;
private UrlEncodedFormEntity formEntity = null;
private String responsePHP = "";
private BufferedReader in = null;
private StringBuffer sb = null;
public SelectedItemHttpPost(){
}
public String executeHttpPost(String selectedItem) throws Exception{
try{
client = new DefaultHttpClient();
request = new HttpPost("http://www.somedomain.com/test/test.php");
postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("arg1", selectedItem));
formEntity = new UrlEncodedFormEntity(postParameters);
request.setEntity(formEntity);
response = client.execute(request);
in = new BufferedReader((new InputStreamReader(response.getEntity().getContent())));
sb = new StringBuffer("");
sb.append(in.readLine());
in.close();
responsePHP = sb.toString();
return responsePHP;
}
finally {}
}
}
答案 0 :(得分:0)
OnCreate方法需要创建一个按钮对象,如:
btnCancel = (Button) findViewById (R.id.btnCancel);
然后在点击它时访问按钮对象:
if(v == btnCancel)
{
setTvText("nothing happens");
setTvColor(0);
}