我完全不知道使用Restful协议调用php webservice哪个更好。我尝试使用了HttpClient和HttpURLConnection来调用webservice。
使用 HttpClient 调用webservice时会发生什么
- 在Froyo上,它可以完美地工作(在本地主机和服务器上)。
- 在JellyBean上工作一段时间后就失效了。
- HttpClient在本地主机上工作得很好,但在调用服务器上的webservice时存在问题。
使用 HttpURLConnection 调用webservice时会发生什么
- 在Froyo上,在本地主机上不能正常工作。
- 第二点与HttpClient的第二点相同。
- 我无法将php webservice页面重定向到另一个php页面。
当我调用webservice abc.php(在本地主机和服务器上),并从这里重定向到另一个页面xyz.php时,实际上会以JSON格式将数据返回给Android项目。但是,如果我使用HttpClient,这种重定向就可以正常工作,但是HttpURLConnection则不行。
HttpClient代码
//calling the webservice using AsyncTask
public String makeHttpReqToSrvr(String url,String requestType,List<NameValuePair> params) {
HttpEntity httpEntity=null;
HttpResponse httpResp = null;
try {
if (requestType == "GET") {
//connection time out
HttpParams httpParameters = new BasicHttpParams();
int timeout1 = 1000*8;
int timeout2 = 1000*8;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeout1);
HttpConnectionParams.setSoTimeout(httpParameters, timeout2);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
String paramString =URLEncodedUtils.format(params, "utf-8");
HttpGet httpGet = new HttpGet(url+"?"+paramString);
httpResp = httpClient.execute(httpGet);
httpEntity = httpResp.getEntity();
}
if (requestType == "POST") {
// connection time out
HttpParams httpParameters = new BasicHttpParams();
int timeout1 = 1000*8;
int timeout2 = 1000*8;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeout1);
HttpConnectionParams.setSoTimeout(httpParameters, timeout2);
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
httpResp = httpClient.execute(httpPost);
httpEntity = httpResp.getEntity();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// -- End and Start read the data using bufferreader
try {
if(httpEntity != null)
json = EntityUtils.toString(httpEntity);
json = strBuilder.toString();
Log.v("JSON", "data"+json);
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
HttpURLConnection状态码
public String makeHttpReqToSrvr(String url,String requestType,List<NameValuePair> params) {
try {
URL urlPath= null;
String paramString = URLEncodedUtils.format(params, "utf-8");
if (requestType == "GET") {
urlPath = new URL(url+"?"+paramString);
}
if (requestType == "POST") {
urlPath = new URL(url);
}
conn = (HttpURLConnection) urlPath.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setDoInput(true);
conn.setDoOutput(true);
if (requestType == "GET") {
conn.setRequestMethod("GET");
}
if (requestType == "POST") {
conn.setRequestMethod("POST");
}
//send the data to the server using post
dos = new DataOutputStream(conn.getOutputStream());
dos.writeBytes(paramString);
dos.flush();
// Starts the query
conn.connect();
int response = conn.getResponseCode();
is = conn.getInputStream();
// Convert the InputStream into a string
json = readIt(is);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
dos.close();
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return json;
请有经验的人士指出上述代码是否有误,并告诉我调用webservice的完美方法。我尝试了很多次,但没有实现我的目标。如果您不理解上述问题,请问我。
在Eclair和Froyo上,Apache HTTP客户端的错误较少。它是这些版本的最佳选择。
谷歌建议针对Gingerbread及更高版本的应用程序使用HttpURLConnection。在Froyo之前,HttpURLConnection存在一些令人沮丧的错误。那么Froyo呢?我的应用程序运行在Froyo及更高版本上。
哪个客户端最好?
任何帮助都将非常有用。谢谢。
asyncTask
中,那么这就是你的问题。因为在早期版本中,你可以使用httpClient而不需要线程,但在4.0中,你需要使用asynctask
。 - Coderji