使用AsyncTask实现时,httppost方法中的httpclient.execute无法与模拟器一起使用

时间:2012-12-14 15:09:30

标签: android-asynctask android-emulator

我是android的新手,正在尝试从webservices API获取一些数据的异步任务实现,并且它可以正常使用单线程(UI主要)实现。但是在转向AsyncTask以实现正确实施和更好的用户体验时,我无法触发网络消息。任何人都可以帮助我找出这里可能出现的问题,我在这里附上我的代码片段,

private class HttpConnector extends AsyncTask<String,String ,String> 
{
    private final String Name;
    private final String setID; 
    private final String setCntrlValue;
    private final String effDate;

    HttpConnector(String aName,String aSetId, String aDate)
    {
        Name=aName;
        setID=aSetId;
        effDate=aDate;

    }


  String response="";
  protected String doInBackground(String ... params) 
    {
        response="";
        String soapCall="<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" ";
        soapCall+=" xmlns:get=\"http://XXXX/GET_TREE_REQUEST.v1\">";
        soapCall+="<soapenv:Header/>";
        soapCall+="<soapenv:Body>";
        soapCall+="<get:TreeData>";
        soapCall+="<get:TreeName>"+Name.toString()+"</get:TreeName>";
        soapCall+="<get:SETID>"+setID.toString()+"</get:SETID>";
        soapCall+="<get:EFFDT>"+effDate.toString()+"</get:EFFDT>";
        soapCall+="</get:TreeData>";
        soapCall+="</soapenv:Body>";
        soapCall+="</soapenv:Envelope>";

        System.setProperty("socksProxyHost", "XXXX");
        System.setProperty("socksProxyPort", "XX");

         HttpParams httpParameters = new BasicHttpParams();
          // Set the timeout in milliseconds until a connection is established.
         int timeoutConnection = 15000;
         HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
          // Set the default socket timeout (SO_TIMEOUT)
          // in milliseconds which is the timeout for waiting for data.
         int timeoutSocket = 0;
         HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

         org.apache.http.client.HttpClient httpclient = new DefaultHttpClient(httpParameters);

         HttpPost httppost = new HttpPost(URL);
         httppost.setHeader("soapaction", "XXXX.v1");
         httppost.setHeader("Content-Type", "text/xml; charset=utf-8");

         try{

              HttpEntity entity = new StringEntity(soapCall.toString(),HTTP.UTF_8);
              httppost.setEntity(entity);
              HttpResponse httpResponse = httpclient.execute(httppost);// calling server
              InputStream content = httpResponse.getEntity().getContent();

              BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
              String s = "";
              while ((s = buffer.readLine()) != null) {
                response += s;
              }
             return response;
        }catch (UnsupportedEncodingException e) {
             return e.getStackTrace().toString();
        } catch (ClientProtocolException e) {
             return e.getStackTrace().toString();
        } catch (IOException e) {
             return e.getStackTrace().toString();
       }catch (Exception e) 
        {   
            return e.getStackTrace().toString();
        }finally{
             httpclient.getConnectionManager().shutdown();//shut down the connection
        }
    }
    // onPostExecute displays the results of the AsyncTask.
    @Override
    protected void onPostExecute(String response) {
         //super.onPostExecute(response);
         txtResponse.setText(response);
   } 


}

  public class FirstAppUI extends Activity 
  {
     public void onClick(View v)
     {
        HttpConnector netConnect= new           HttpConnector(txtTreeName.getText().toString(),txtSetId.getText().toString(),txtEffDate.getText().toString()); 
        netConnect.execute(); 
        txtResponse.setText("Waiting for the Server Response..!!"); 

      }
 }

我正在从我的活动类中对上面的AsynTask进行讨论。代码流在httpclient.execute(httppost)行停止。

由于

0 个答案:

没有答案