【发布时间】:2013-11-23 03:01:35
【问题描述】:
我很困惑哪一个更适合使用 Restful Protocol.I 调用 php webservice 确实使用了 API(HttpClient 和 HttpURLConnection) 来调用 webservice。
使用 HttpClient 调用 Web 服务时会发生什么
- 在 Froyo 上完美运行(在本地主机和服务器上)。
- 在 JellyBean 上工作,但一段时间后就无法工作
- HttpClient 在 localhost 上运行良好,但在服务器上调用 werbservice 时出现问题。
使用 HttpURLConnection
调用网络服务时会发生什么- 在 Froyo 上无法正常工作(在 localhost 上)
- 第二点与HttpClient的第二点相同
- 我无法将 php webservice 页面重定向到另一个 php 页面。
当我调用 webservice abc.php(在本地主机和服务器上)并从这里我重定向到另一个页面,如 xyz.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;
}
HttpURL连接代码
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;
}
如果上面的代码有任何问题,请任何人建议我,并告诉我完美的调用方式
网络服务。我尝试了很多,但没有达到我的目标。如果您不明白上述问题,请
问我。
Apache HTTP 客户端在 Eclair 和 Froyo 上的错误更少。这是这些版本的最佳选择。
google 建议针对 Gingerbread 及更高版本的应用程序使用 HttpURLConnection 在 Froyo 之前,HttpURLConnection 有一些令人沮丧的错误。那么弗罗约呢?我的应用 在 froyo 和更高版本上运行。
哪个客户端最好?
任何帮助都会很有用。谢谢。
【问题讨论】:
-
你什么时候调用这些函数?!如果它不在
asyncTask中,那么这就是你的问题。因为在早期版本中,您可以在没有线程的情况下使用 httpClient,但在 4.0 中,您需要使用asynctask -
我在整个应用程序中都使用了 AsyncTask。只是我正在从活动的 doInBackground 方法调用 makeHttpReqToSrvr 方法。我知道 AsyncTask。我使用两个客户端类进行检查
标签: php android json web-services