0

我尝试将列表发送到服务器并使用此列表在数据库中搜索并创建响应并回复客户端但是当我运行程序时它停止。有时它会起作用,直到我停止它。经常项目停止在这一行

     HttpResponse response = httpclient.execute(httppost);

在调试窗口中显示这个...

Thread [<15> AsyncTask #3] (Suspended (exception RuntimeException)) 
        ThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker) line: 1094  
        ThreadPoolExecutor$Worker.run() line: 569   

代码:

public void onclick_search(View v)
    {

            dialog = ProgressDialog.show(this, "Sending...", "please wait...",true);
          new Thread(new Runnable() {
              @Override   
              public void run() {
                     postData();                          
                 }
               }).start();


    }
    public void postData() {
        String str = null;
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://saynaco.ir/Handler.ashx");

        try {
            List<NameValuePair> item_select = new ArrayList<NameValuePair>(6);
            item_select.add(new BasicNameValuePair("land","sale" ));
            item_select.add(new BasicNameValuePair("city", city ));         
            item_select.add(new BasicNameValuePair("price",s11 ));
            item_select.add(new BasicNameValuePair("bar",s22));
            item_select.add(new BasicNameValuePair("area",s33));
          item_select.add(new BasicNameValuePair("land_kind", rb));

            httppost.setEntity(new UrlEncodedFormEntity(item_select));

            HttpResponse response = httpclient.execute(httppost);
             str = inputStreamToString(response.getEntity().getContent()).toString();

        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
            String []respon = str.split("&");
        if(respon[0] == "ok")
        {


            dialog.dismiss();
            Intent in = new Intent(getApplication(), List_show.class).putExtra("url", respon[1]);
            startActivity(in);
            Toast.makeText(getBaseContext(),"",Toast.LENGTH_SHORT).show();

            //Intent in= new Intent(getBaseContext(), .class);
            //startActivity(in);
        }
        else {
            dialog.dismiss();
        }
    } 
     private StringBuilder inputStreamToString(InputStream is) {
         String line = "";
         StringBuilder total = new StringBuilder();
         // Wrap a BufferedReader around the InputStream
         BufferedReader rd = new BufferedReader(new InputStreamReader(is));
         // Read response until the end
         try {
          while ((line = rd.readLine()) != null) { 
            total.append(line); 
          }
         } catch (IOException e) {
          e.printStackTrace();
         }
         // Return full string
         return total;
        }
4

1 回答 1

0

您不能从后台线程触摸 UI 线程中的任何内容。所以像:

  dialog.dismiss();

  Toast.makeText(getBaseContext(),"",Toast.LENGTH_SHORT).show();

不能被postData()非 UI 线程调用的对象调用。

我建议使用AsyncTask,并将此行之后的所有内容放在 postExecute() 方法中:

String []respon = str.split("&");

另请阅读有关此主题的更多信息:http: //developer.android.com/guide/components/processes-and-threads.html

于 2013-09-19T23:46:51.187 回答