2017-04-01 49 views
1

我已經創造了機器人工作室一類延伸JsonRequest 但是Android工作室給我一個錯誤,並要我創建一個構造函數,因爲在圖書館(凌空庫)沒有默認構造函數創建構造函數爲了那個原因。 其實我使用凌空庫來解析android中的listview中的json文件。 原因json是我創建這個類的所有波斯語文本,將其更改爲utf-8編碼。 請告訴我所需的構造函數應該如何。我嘗試了一些,但不正確。延長JsonRequest <JSONObject>類

public class Utf8JsonRequest extends JsonRequest<JSONObject> { 
    ... 
    @Override 
    protected Response<JSONObject> parseNetworkResponse (NetworkResponse response) { 
     try { 
      String utf8String = new String(response.data, "UTF-8"); 
      return Response.success(new JSONObject(utf8String), HttpHeaderParser.parseCacheHeaders(response)); 
     } catch (UnsupportedEncodingException e) { 
      // log error 
      return Response.error(new ParseError(e)); 
     } catch (JSONException e) { 
      // log error 
      return Response.error(new ParseError(e)); 
     } 
    } 
} 
+0

是不是已經編碼在utf-8中的波斯語文本? – nandsito

+1

@nandsito默認排球庫的編碼器是ISO-8859-1,它不能顯示波斯或阿拉伯文字 – Aliryanfox

回答

0

這裏的一個構造的一個例子:

// the constructor should have at least these parameters 
// to be passed onto super() 
public Utf8JsonRequest(int method, 
         String url, 
         String requestBody, 
         Response.Listener<JSONObject> listener, 
         Response.ErrorListener errorListener) { 
    super(method, url, requestBody, listener, errorListener); 

    // you can initialize other stuff if you wish 
} 
+0

tank you @nandsito – Aliryanfox

0

這是相同的構造作爲JsonRequest:

JsonRequest(INT方法,字符串URL,字符串requestBody,Response.Listener聽者,Response.ErrorListener errorListener)

+0

它沒有工作 – Aliryanfox

+0

好吧,它是在文檔http://afzaln.com/ volley/com/android/volley/toolbox/JsonRequest.html – ElDuderino

+0

tanx請注意 – Aliryanfox