我是 android 新手,正在尝试一个异步任务实现来从 web 服务 API 中获取一些数据,它适用于普通的单线程(UI 主)实现。但是在转移到 AsyncTask 以进行正确实施和更好的 UX 时,我无法触发网络消息。谁能帮我找出这里可能出了什么问题,我在这里附上我的代码片段,
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) 行停止。
谢谢