简体   繁体   English

onClick无法正常工作

[英]onClick doesn't work properly

Here is my Class. 这是我的班级。 In OnClick method i'm trying to post data to the remote db, but nothing happend. 在OnClick方法中,我试图将数据发布到远程数据库,但没有任何事情发生。 I don't know totally why. 我完全不知道为什么。 No errors, no action. 没有错误,没有行动。 Just nothing. 没什么。 Any answers are welcome. 欢迎任何答案。 Please help. 请帮忙。

public class Wyslij extends Activity {

protected static final int TIMEOUT_MILLISEC = 5000;

ImageButton ib_wyslij;
int suma_zam;



@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.wyslij);


    ib_wyslij = (ImageButton) findViewById(R.id.ib_wyslij);

    final Zamowienie zam = new Zamowienie();



    String suma_zamowienia = podaj_sume(TowarZamowienie.towary_zamowione);
    if(suma_zamowienia != null && !suma_zamowienia.equalsIgnoreCase("")){

    suma_zam = Integer.parseInt(suma_zamowienia);
        zam.suma=suma_zam;}

    zam.suma=suma_zam;


    TowarZamowienie tz = new TowarZamowienie();

    ib_wyslij.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub


            try {
                JSONObject json = new JSONObject();
                json.put("Zam_suma", zam.getSuma());

                HttpParams httpParams = new BasicHttpParams();
                HttpConnectionParams.setConnectionTimeout(httpParams,
                        TIMEOUT_MILLISEC);
                HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
                HttpClient client = new DefaultHttpClient(httpParams);
                //
                //String url = "http://10.0.2.2:8080/sample1/webservice2.php?" + 
                //             "json={\"UserName\":1,\"FullName\":2}";
                String url = "http://www.msinzynierka.cba.pl/executeConn.php";

                HttpPost request = new HttpPost(url);
                request.setEntity(new ByteArrayEntity(json.toString().getBytes(
                        "UTF8")));
                request.setHeader("json", json.toString());
                HttpResponse response = client.execute(request);
                HttpEntity entity = response.getEntity();
                // If the response does not enclose an entity, there is no need

            } catch (Throwable t) {

            }
            //sendAccelerationData(zam);

        }
});

}

private String podaj_sume(ArrayList<Towar> l) {
    int suma = 0;
    String s = null;
    for (int i = 0; i < l.size(); i++) {
        suma += l.get(i).Tow_ilosc * l.get(i).Tow_cena;
    }

    return s = String.valueOf(suma);
}
}

Don't catch a general Exception and don't print it - that way, you won't get any hint in case of an exception. 不要捕获一般的异常并且不打印它 - 这样,如果发生异常,您将不会得到任何提示。 Simply change the throwable to exception and call yourException.printStacktrace(); 只需将throwable更改为exception并调用yourException.printStacktrace();

I bet the Exception you get is a NetworkingOnMainThreadException . 我敢打赌,你得到的Exception是NetworkingOnMainThreadException Networking operations tend to take some time, and that's why it's forbidden to perform such actions on the main (ui) thread in Android. 网络操作往往需要一些时间,这就是为什么禁止在Android的主(ui)线程上执行此类操作的原因。 This would make the UI unresponsive, which leads to a very bad user experience. 这会使UI无响应,从而导致非常糟糕的用户体验。

Have a look at AsyncTask for gracefully moving the operation to a background thread. 看看AsyncTask是否可以优雅地将操作移动到后台线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM