@Override
  protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
    try {
      String js = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
      Log.i("APP", js);
      return (Response.success(new JSONArray(js), HttpHeaderParser.parseCacheHeaders(response)));
    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    } catch (JSONException e) {
      e.printStackTrace();
      /*if(e.getMessage().contains("{\"id\":\"not_found\"}")){

      	HashMap<String,String> jsErr = new HashMap<String,String>();
      	jsErr.put("id","not_found");
      	HashMap<String,String> nulo = new HashMap<String,String>();
      	//nulo.put("0","0");

      	ArrayList<HashMap> ja = new ArrayList<HashMap>();
      	ja.add(jsErr);
      	//ja.add(nulo);

      	return(Response.success(new JSONArray(ja), HttpHeaderParser.parseCacheHeaders(response)));

      }*/
    }
    return null;
  }
 @Override
 protected Response<String> parseNetworkResponse(NetworkResponse response) {
   try {
     String ret = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
     return Response.success(ret, HttpHeaderParser.parseCacheHeaders(response));
   } catch (Exception E) {
     return Response.error(new ParseError(E));
   }
 }
Example #3
0
 protected Response<String> parseNetworkResponse(NetworkResponse response) {
   String parsed;
   try {
     parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
   } catch (UnsupportedEncodingException e) {
     parsed = new String(response.data);
   }
   return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
 }
 /**
  * 这里开始解析数据
  *
  * @param response Response from the network
  * @return
  */
 @Override
 protected Response<String> parseNetworkResponse(NetworkResponse response) {
   try {
     String mString = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
     return Response.success(mString, HttpHeaderParser.parseCacheHeaders(response));
   } catch (UnsupportedEncodingException e) {
     return Response.error(new ParseError(e));
   }
 }
 protected Response<JSONArray> parseNetworkResponse(NetworkResponse response) {
   try {
     String je = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
     return Response.success(new JSONArray(je), HttpHeaderParser.parseCacheHeaders(response));
   } catch (UnsupportedEncodingException var3) {
     return Response.error(new ParseError(var3));
   } catch (JSONException var4) {
     return Response.error(new ParseError(var4));
   }
 }
 @Override
 protected Response<String> parseNetworkResponse(NetworkResponse response) {
   // TODO Auto-generated method stub
   String parsed;
   try {
     parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
   } catch (UnsupportedEncodingException e) {
     parsed = new String(response.data);
   }
   return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
 }
 @Override
 protected Response<JsonObject> parseNetworkResponse(NetworkResponse response) {
   try {
     String jsonString =
         new String(response.data, HttpHeaderParser.parseCharset(response.headers));
     JsonObject jsonObject = new JsonParser().parse(jsonString).getAsJsonObject();
     return Response.success(jsonObject, HttpHeaderParser.parseCacheHeaders(response));
   } catch (UnsupportedEncodingException e) {
     return Response.error(new ParseError(e));
   }
 }
Example #8
0
 @Override
 protected Response<T> parseNetworkResponse(NetworkResponse response) {
   try {
     String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
     return Response.success(
         gson.fromJson(json, clazz), HttpHeaderParser.parseCacheHeaders(response));
   } catch (UnsupportedEncodingException e) {
     return Response.error(new ParseError(e));
   } catch (JsonSyntaxException e) {
     return Response.error(new ParseError(e));
   }
 }
 @Override
 protected Response<T> parseNetworkResponse(NetworkResponse response) {
   try {
     String jsonString =
         new String(response.data, HttpHeaderParser.parseCharset(response.headers));
     return Response.success(
         Mapper.objectOrThrow(jsonString, responseType),
         HttpHeaderParser.parseCacheHeaders(response));
   } catch (Exception e) {
     return Response.error(new ParseError(e));
   }
 }
 @Override
 protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
   try {
     String jsonString =
         new String(response.data, HttpHeaderParser.parseCharset(response.headers));
     return Response.success(
         new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response));
   } catch (UnsupportedEncodingException e) {
     return Response.error(new ParseError(e));
   } catch (JSONException je) {
     return Response.error(new ParseError(je));
   }
 }
Example #11
0
 @Override
 protected Response<T> parseNetworkResponse(NetworkResponse response) {
   try {
     Logger.d("YbbHttpRequest", requestUrl() + "?" + WebUtils.buildQuery(GetParameters()));
     String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
     Logger.d("YbbHttpRequest", StringUtils.unicodeToChinese(json));
     return Response.success(
         mGson.fromJson(json, getResponseClass()), HttpHeaderParser.parseCacheHeaders(response));
   } catch (UnsupportedEncodingException e) {
     return Response.error(new ParseError(e));
   } catch (JsonSyntaxException e) {
     return Response.error(new ParseError(e));
   }
 }
 @Override
 protected Response<List<Project>> parseNetworkResponse(NetworkResponse response) {
   try {
     String jsonArrayString =
         new String(response.data, HttpHeaderParser.parseCharset(response.headers, "utf-8"));
     JSONArray jsonArray = new JSONArray(jsonArrayString);
     List<Project> projects = Project.fromJSONArray(jsonArray);
     return Response.success(projects, HttpHeaderParser.parseCacheHeaders(response));
   } catch (UnsupportedEncodingException e) {
     return Response.error(new ParseError(e));
   } catch (JSONException e) {
     return Response.error(new ParseError(e));
   }
 }
Example #13
0
  /**
   * Extracts a {@link Cache.Entry} from a {@link com.android.volley.NetworkResponse}. Cache-control
   * headers are ignored. SoftTtl == 3 mins, ttl == 24 hours.
   *
   * @param response The network response to parse headers from
   * @return a cache entry for the given response, or null if the response is not cacheable.
   */
  public static Cache.Entry parseIgnoreCacheHeaders(NetworkResponse response) {
    long now = System.currentTimeMillis();

    Map<String, String> headers = response.headers;
    long serverDate = 0;
    String serverEtag = null;
    String headerValue;

    headerValue = headers.get("Date");
    if (headerValue != null) {
      serverDate = com.android.volley.toolbox.HttpHeaderParser.parseDateAsEpoch(headerValue);
    }

    serverEtag = headers.get("ETag");

    final long cacheHitButRefreshed =
        3 * 60 * 1000; // in 3 minutes cache will be hit, but also refreshed on background
    final long cacheExpired =
        24 * 60 * 60 * 1000; // in 24 hours this cache entry expires completely
    final long softExpire = now + cacheHitButRefreshed;
    final long ttl = now + cacheExpired;

    Cache.Entry entry = new Cache.Entry();
    entry.data = response.data;
    entry.etag = serverEtag;
    entry.softTtl = softExpire;
    entry.ttl = ttl;
    entry.serverDate = serverDate;
    entry.responseHeaders = headers;

    return entry;
  }
Example #14
0
 @Override
 protected Response parseNetworkResponse(NetworkResponse response) {
   if (response.statusCode != 200) {
     return Response.error(new VolleyError("failer"));
   } else {
     try {
       String content = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
       L.e(content);
       T t = JSON.parseObject(content, clazz);
       return Response.success(t, HttpHeaderParser.parseCacheHeaders(response));
     } catch (Exception e) {
       e.printStackTrace();
       return Response.error(new VolleyError("failer"));
     }
   }
 }
Example #15
0
 @Override
 protected Response<XmlPullParser> parseNetworkResponse(NetworkResponse networkResponse) {
   String xmlString = null;
   try {
     xmlString =
         new String(networkResponse.data, HttpHeaderParser.parseCharset(networkResponse.headers));
     XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
     XmlPullParser xmlPullParser = factory.newPullParser();
     xmlPullParser.setInput(new StringReader(xmlString));
     return Response.success(xmlPullParser, HttpHeaderParser.parseCacheHeaders(networkResponse));
   } catch (UnsupportedEncodingException e) {
     return Response.error(new ParseError());
   } catch (XmlPullParserException e) {
     return Response.error(new ParseError());
   }
 }
 @Override
 protected Response<List<Game>> parseNetworkResponse(NetworkResponse response) {
   try {
     List<Game> games = new ArrayList<>();
     JSONArray json =
         new JSONArray(new String(response.data, HttpHeaderParser.parseCharset(response.headers)));
     for (int i = 0; i < json.length(); i++) {
       games.add(Game.fromJSON(json.getJSONObject(i)));
     }
     return Response.success(games, HttpHeaderParser.parseCacheHeaders(response));
   } catch (JSONException e) {
     return Response.error(new ParseError(e));
   } catch (UnsupportedEncodingException e) {
     return Response.error(new ParseError(e));
   }
 }
 protected Response<JSONObject> parseNetworkResponse(NetworkResponse paramNetworkResponse) {
   try {
     Response localResponse =
         Response.success(
             new JSONObject(
                 new String(
                     paramNetworkResponse.data,
                     HttpHeaderParser.parseCharset(paramNetworkResponse.headers))),
             HttpHeaderParser.parseCacheHeaders(paramNetworkResponse));
     return localResponse;
   } catch (UnsupportedEncodingException localUnsupportedEncodingException) {
     return Response.error(new ParseError(localUnsupportedEncodingException));
   } catch (JSONException localJSONException) {
     return Response.error(new ParseError(localJSONException));
   }
 }
  @Override
  protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    Log.d(TAG, "$$$ parseNetworkResponse");
    try {
      Log.d(TAG, " status Code " + response.statusCode);
      initDevicePairResponseData(response.statusCode, response.headers, new String(response.data));
      String jsonString =
          new String(response.data, HttpHeaderParser.parseCharset(response.headers));

      return Response.success(
          new JSONObject(jsonString), HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
      return Response.error(new ParseError(e));
    } catch (JSONException je) {
      return Response.error(new ParseError(je));
    }
  }
 public static Cache.Entry parseCacheHeaders(NetworkResponse response) {
   Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);
   if (entry == null) {
     entry = new Cache.Entry();
     entry.data = response.data;
   }
   return entry;
 }
 @Override
 protected Response<Workitem> parseNetworkResponse(NetworkResponse networkResponse) {
   try {
     String json =
         new String(
             networkResponse.data,
             HttpHeaderParser.parseCharset(networkResponse.headers, "utf-8"));
     Workitem workitem = Workitem.fromJSON(new JSONArray(json).getJSONObject(0));
     return Response.success(workitem, HttpHeaderParser.parseCacheHeaders(networkResponse));
   } catch (UnsupportedEncodingException e) {
     return Response.error(new ParseError(e));
   } catch (ParseException e) {
     return Response.error(new ParseError(e));
   } catch (JSONException e) {
     return Response.error(new ParseError(e));
   }
 }
Example #21
0
 @Override
 protected Response<T> parseNetworkResponse(NetworkResponse response) {
   T t = null;
   try {
     String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
     Log.w("aaaaaaaaaaaaaaaa", "========JSON=======" + json);
     t = JSON.parseObject(json, clz);
     // 非空判断并且符合JSON格式
     /*
      * if (!TextUtils.isEmpty(json) && json.matches("^\\{.*\\}$")) { t =
      * JSON.parseObject(json, clz); }
      */
   } catch (UnsupportedEncodingException e) {
     e.printStackTrace();
   }
   // 得到解析的缓存头部信息
   return Response.success(t, HttpHeaderParser.parseCacheHeaders(response));
 }
Example #22
0
  @Override
  protected Response<NetworkResponse> parseNetworkResponse(NetworkResponse response) {
    int statusCode = response.statusCode;

    Cache.Entry entry = HttpHeaderParser.parseCacheHeaders(response);

    Log.i(TAG, "status code: " + statusCode);

    return Response.success(response, entry);
  }
Example #23
0
  @Override
  protected Response<BaseResponse> parseNetworkResponse(NetworkResponse response) {

    try {
      String jsonString =
          new String(response.data, HttpHeaderParser.parseCharset(response.headers));

      /*  if (BuildConfig.DEBUG)
                      Log.d(TAG,"respone:"+jsonString);
      */

      BaseResponse baseResponse = parseJson(jsonString);
      return Response.success(baseResponse, HttpHeaderParser.parseCacheHeaders(response));

    } catch (UnsupportedEncodingException e) {
      e.printStackTrace();
    }

    return null;
  }
Example #24
0
  @Override
  protected Response<ResponseDTO> parseNetworkResponse(NetworkResponse response) {
    ResponseDTO dto = new ResponseDTO();
    try {
      Gson gson = new Gson();
      String resp = new String(response.data);
      Log.i(LOG, "response string length returned: " + getSize(resp.length()));
      try {
        dto = gson.fromJson(resp, ResponseDTO.class);
        if (dto != null) {
          return Response.success(dto, HttpHeaderParser.parseCacheHeaders(response));
        }
      } catch (Exception e) {
        // ignore, it's a zipped response
      }

      InputStream is = new ByteArrayInputStream(response.data);
      ZipInputStream zis = new ZipInputStream(is);
      @SuppressWarnings("unused")
      ZipEntry entry;
      ByteArrayBuffer bab = new ByteArrayBuffer(2048);

      while ((entry = zis.getNextEntry()) != null) {
        int size = 0;
        byte[] buffer = new byte[2048];
        while ((size = zis.read(buffer, 0, buffer.length)) != -1) {
          bab.append(buffer, 0, size);
        }
        resp = new String(bab.toByteArray());
        dto = gson.fromJson(resp, ResponseDTO.class);
      }
    } catch (Exception e) {
      VolleyError ve = new VolleyError("Exception parsing server data", e);
      errorListener.onErrorResponse(ve);
      Log.e(LOG, "Unable to complete request: " + dto.getMessage(), e);
      return Response.error(new VolleyError(dto.getMessage()));
    }
    end = System.currentTimeMillis();
    Log.e(LOG, "#### comms elapsed time in seconds: " + getElapsed(start, end));
    return Response.success(dto, HttpHeaderParser.parseCacheHeaders(response));
  }
Example #25
0
  @SuppressWarnings("unchecked")
  @Override
  protected Response<T> parseNetworkResponse(NetworkResponse response) {
    String json;
    try {
      json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
      if (Configuration.isShowNetworkJson()) Log.e("result", json);
    } catch (UnsupportedEncodingException e) {
      return Response.error(new ParseError(e));
    }
    Type objectType;
    if (mClass != null) {
      objectType = type(parentClass, mClass);
    } else {
      objectType = parentClass;
    }

    return (Response<T>)
        Response.success(
            mGson.fromJson(json, objectType), HttpHeaderParser.parseCacheHeaders(response));
  }
  @Override
  protected Response<String> parseNetworkResponse(NetworkResponse response) {
    // TODO Auto-generated method stub

    try {
      String jsonString =
          new String(response.data, HttpHeaderParser.parseCharset(response.headers));

      Pattern pattern = Pattern.compile("Set-Cookie.*?;");
      Matcher m = pattern.matcher(response.headers.toString());
      if (m.find()) {
        cookie = m.group();
        // L.e("LOG","cookie from server "+ cookie);
      }

      return Response.success(jsonString, HttpHeaderParser.parseCacheHeaders(response));

    } catch (UnsupportedEncodingException e) {
      return Response.error(new ParseError(e));
    }
  }
Example #27
0
 @Override
 protected Response<T> parseNetworkResponse(NetworkResponse response) {
   try {
     String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
     JSONObject obj = new JSONObject(json);
     if (obj.getString("status").equalsIgnoreCase("OK")) {
       Log.v(MainActivity.TAG, "Request is SUCCESS !!");
       return Response.success(
           gson.fromJson(obj.getJSONObject(clazz.getSimpleName().toLowerCase()).toString(), clazz),
           HttpHeaderParser.parseCacheHeaders(response));
     } else {
       return Response.error(new VolleyError("Failed or Unexpected response message !!"));
     }
   } catch (UnsupportedEncodingException e) {
     return Response.error(new ParseError(e));
   } catch (JsonSyntaxException e) {
     return Response.error(new ParseError(e));
   } catch (JSONException e) {
     return Response.error(new ParseError(e));
   }
 }
  @Override
  protected Response<TwitterSearchResponse> parseNetworkResponse(NetworkResponse response) {
    // 1 second delay
    doArtificialDelay();

    try {
      // get string response
      String json = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
      // Parse string response with Gson
      TwitterSearchResponse twitterSearchResponse =
          gson.fromJson(json, TwitterSearchResponse.class);

      // process tweets
      if (twitterSearchResponse != null && tweetsResultProcessor != null) {
        tweetsResultProcessor.processTweets(twitterSearchResponse.getStatuses());
      }

      return Response.success(twitterSearchResponse, HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
      return Response.error(new ParseError(e));
    }
  }
Example #29
0
 @Override
 // 解析返回的数据
 protected Response<String> parseNetworkResponse(NetworkResponse response) {
   byte[] data = response.data;
   Log.d("test", "url = " + getUrl() + ",response = " + new String(data));
   try {
     String model = new String(data);
     return Response.success(model, HttpHeaderParser.parseCacheHeaders(response));
   } catch (Exception e) {
     // 解析json出错
     e.printStackTrace();
     return Response.error(new VolleyError("服务器错误"));
   }
 }
  @Override
  protected Response<T> parseNetworkResponse(NetworkResponse response) {

    try {
      String jsonString =
          new String(response.data, HttpHeaderParser.parseCharset(response.headers));

      String result = new String(response.data, "UTF8");
      // result = URLDecoder.decode(result, "UTF8");
      // result=result.replace("\"", "");
      LogUtil.d(TAG, result); // 编码转换
      // Log.d(URLDecoder.decode("123abc\u8ba2\u5355\u5df2\u53d6\u6d88\u6216\u6b63\u5728\u53d6\u6d88\u4e2d",
      // "UTF-8"));

      return Response.success(
          JSON.parseObject(jsonString, mClass), HttpHeaderParser.parseCacheHeaders(response));
      // return Response.success(JSON.parseObject(jsonString,
      // mClass),HttpHeaderParser.parseIgnoreCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
      LogUtil.d(TAG, "error-7-8");
      return Response.error(new ParseError(e));
    }
  }