这里是我的方法连接到服务器,并在MyUtilitiesActivity中获取响应。如何在android中连接服务器时显示进度条?
private static response = "";
public static String send(final Activity act, final String url, final String keys[], final String values[])
{
try{
HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
HttpConnectionParams.setSoTimeout(httpParameters, 5000);
HttpClient httpclient = new DefaultHttpClient(httpParameters);
if(keys == null && values == null)
{
HttpGet get = new HttpGet(url);
// Execute HTTP Get Request
response = httpclient.execute(get, new BasicResponseHandler()).trim();
}
else if(keys != null && values != null && keys.length == values.length)
{
// Add your data
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
for (int i = 0; i < values.length; i++)
{
nameValuePairs.add(new BasicNameValuePair(keys[i], values[i]));
}
HttpPost post = new HttpPost(url);
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
response = httpclient.execute(post, new BasicResponseHandler()).trim();
}
}
catch (Exception e)
{
response = e + "";
e.printStackTrace();
}
return response ;
}
而且我只需点击MyTestActivity中的按钮即可调用上述方法。
public onClick(View v)
{
String response = MyUtilitiesActivity.send(MyTestActivity.this, "http://www.google.com", null, null);
//the code to be execute after getting the response from server
}
我在哪里显示进度条?在按钮的内部点击或里面的send()方法?如何显示它?请帮帮我。
+1也从我的身边... – 2012-03-22 05:51:31
把你送()方法在Async的doInBackGround()中:I'an无法做到这一点。我应该在doInBG()中调用send方法,还是应该将代码放在doInBG()中。你能否详细解释一下。谢谢。 – 2012-03-22 06:40:05
是的,你可以把你的发送方法的代码放在doInBack()中也没有问题。 – user370305 2012-03-22 06:41:32