【发布时间】:2014-12-24 15:10:12
【问题描述】:
到目前为止,这是我的代码,我根据我在 stackoverflow 上找到的许多帖子对其进行了更改,但是我似乎无法使其工作,它工作正常,但有时它卡在进度条上并且它一直在旋转,直到应用程序崩溃,我正在尝试设置超时,以便如果连接不成功,它将停止而不是让应用程序崩溃。
我不确定我是否以正确的方式使用 HttpConnectionParams.setSoTimeout 和 HttpConnectionParams.setConnectionTimeout,
这是 JSON 代码
public JSONObject makeHttpRequest(String url, String method,List<NameValuePair> params)
{
try
{
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setSoTimeout(httpParameters, 4000);
HttpConnectionParams.setConnectionTimeout(httpParameters, 4000);
if(method.equals("POST"))
{
HttpClient httpClient = new DefaultHttpClient(httpParameters);
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params,"utf-8"));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null)
{
is = httpEntity.getContent();
if(is != null)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
json = sb.toString();
jObj = new JSONObject(json);
}
}
}
else if(method.equals("GET"))
{
HttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null)
{
is = httpEntity.getContent();
if(is != null)
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
json = sb.toString();
jObj = new JSONObject(json);
}
}
}
}
catch (ConnectTimeoutException e)
{
//Here Connection TimeOut excepion
Log.e("connection error time OUT", "ok");
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
} catch (ClientProtocolException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
catch (Exception e)
{
Log.e("Buffer Error", "error converting result " + e.toString());
}
return jObj;
}
这是从 php 页面加载项目的 AsyncTask。
class GetSpecialOffers extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("loading items, please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
try
{
int success=0;
JSONObject json = jParser.makeHttpRequest(url_items, "GET",params);
if(json != null)
{
success = json.getInt(TAG_SUCCESS);
}
if (success == 1)
{
successValue = 1;
items = json.getJSONArray(TAG_SPECIAL_ITEM);
for (int i = 0; i < items.length(); i++)
{
JSONObject c = items.getJSONObject(i);
specialid = c.getString(TAG_SPECIAL_ID);
specialname = c.getString(TAG_SPECIAL_NAME);
specialprice = c.getString(TAG_SPECIAL_PRICE);
specialimage = c.getString(TAG_SPECIAL_IMAGE);
specialdate = c.getString(TAG_SPECIAL_DATE);
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_SPECIAL_ID, specialid);
map.put(TAG_SPECIAL_NAME, specialname);
map.put(TAG_SPECIAL_PRICE, specialprice);
map.put(TAG_SPECIAL_IMAGE, specialimage);
map.put(TAG_SPECIAL_DATE, specialdate);
SpecialList.add(map);
}
}
else
{
successValue = 0;
}
}
catch (JSONException e)
{
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url)
{
pDialog.cancel();
if (successValue==1)
{
adapter = new SpecialAdapter(getActivity(), SpecialList);
lVSpecial.setAdapter(adapter);
}
else
{
NoSpecialOffersFound();
}
}
}
【问题讨论】:
-
为什么不使用 http 库,例如 Retrofit 或 OkHTTP,而不是重新发明轮子? :)
-
是 OkHTTP 还是 Retrofit 像 Volley 库?
-
你推荐哪个?我不熟悉他们中的任何一个......
-
我用的是 Retrofit,如果你有 REST API 就很简单了。尝试其中一些或查看一些教程。
标签: php android json httprequest connection-timeout