簡體   English   中英

在某些設備上輸入字符0錯誤

[英]end of input at character 0 error on some devices

我有以下代碼,我正在調用一個PHP構建的API。 代碼返回如下所示的json,我在stringBuilder對象中收集。 問題是它在一些運營商和少數設備上運行其他運營商/ wifi連接它在字符0異常時拋出JSONException輸入結束,我知道當輸入字符串為空時,這意味着stringBuilder對象為空。 問題是我無法訪問其拋出這些錯誤的設備。

我沒有得到一些設備為什么以下代碼返回空字符串,並在一些工作正常,用戶已測試3G以及wifi這些設備在其他國家的不同運營商。

            HttpClient httpClient = HttpClientBuilder.create().build();
            HttpPost postRequest = new HttpPost(ServiceUrls.base_url + ServiceUrls.get_profile_url);

            JSONObject object = new JSONObject();
            object.put("username", params[0]);

            StringEntity input = new StringEntity(object.toString());
            input.setContentType("application/json");
            postRequest.setEntity(input);

            HttpResponse response = httpClient.execute(postRequest);

            if (response.getStatusLine().getStatusCode() != 200) {
                throw new RuntimeException("Failed : HTTP error code : "
                        + response.getStatusLine().getStatusCode());
            }

            BufferedReader br = new BufferedReader(
                    new InputStreamReader((response.getEntity().getContent())));

            String output;
            StringBuilder stringBuilder = new StringBuilder();
            while ((output = br.readLine()) != null) {
                stringBuilder.append(output);
            }

如果它適用於所有API調用,那么它是合乎邏輯的但是其他API調用會發生,此API調用會在stringbuilder中返回更大的JSON字符串,如下所示

{
“狀態”:1,“停車”:{
“name”:“ghgjjghghg”,“cost”:3,“ownerId”:29,“address”:“xyz pqr”,“slots”:4,“image”:“d4bc95c1dd031685746f2c3570788acf.jpg”,“details”:“ gjhjghjgg“,”amenities“:”gjhg“,”id“:70,”lon“:73.7898023,”lat“:19.9974533,”type“:0,”available“:1},”rating“:0,”ratingCount “:0,”所有者“:{
“id”:29,“username”:“vd @ gmail.com”,“password”:“”,“fullname”:“vi hdjh”,“phone”:“23434fddf”,“ccNum”:null,“ccType “:null,”type“:1,”authType“:1,”image“:”582e3a77d76ae3203cfd6d6a346da429.jpg“,”dni“:”abc123“,”account“:”ABCBANK“}}

我不知道發生了什么,請幫忙。 任何輸入將不勝感激。

您發布的代碼沒有任何異常。 那里沒有線索。

讓我總結一下我認為您對症狀的看法。

  • 對於某些設備/運營商,特定的API調用失敗。 但並非所有設備/運營商。

  • 對於與上述相同的設備/運營商,其他API調用可以正常工作。

  • 除了URL之外,客戶端代碼在所有情況下都是相同的。

對我而言,這指向了服務器端的問題,該問題由請求的外觀觸發。 但無論如何,我會嘗試通過查看服務器端的請求和響應以及檢查服務器端日志來調查此問題。 查看來自不同設備/運營商的請求是否存在顯着差異。 尤其是那些不起作用的那些。 並在服務器發送響應時查看響應是否為空。

我發現Leonidos的理論很有用: https ://stackoverflow.com/a/19540249/6076711

這是我的解決方案的結束,您可以嘗試使用以下代碼

string output = "";
while(br.ready() && (br.readLine() != null))
{
    output += (String.valueOf(br.readLine()))+"\n";
}

可以通過檢查(在將其放入字符串構建器之前)內容的長度是否大於0來改進代碼。

HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost postRequest = new HttpPost(ServiceUrls.base_url + 
        ServiceUrls.get_profile_url);

JSONObject object = new JSONObject();
object.put("username", params[0]);

StringEntity input = new StringEntity(object.toString());
input.setContentType("application/json");
postRequest.setEntity(input);

HttpResponse response = httpClient.execute(postRequest);

String output;

if (response.getStatusLine().getStatusCode() != 200) {
    throw new RuntimeException("Failed : HTTP error code : "
            + response.getStatusLine().getStatusCode());
} else {
    HttpEntity entity = response.getEntity();

    if ((entity != null) && (entity.getContentLength() != 0)) {
        // Use writing to output stream to prevent problems with chunked responses    
       ByteArrayOutputStream os = new ByteArrayOutputStream();
       entity.writeTo(os);
       output = new String(os.toByteArray(),"UTF-8"); 
    } else {
       // Handle case there is not content returned
       System.out.println("Received no content (HTTP response code " + response.getStatusLine().getStatusCode() + " , reason: " + getReasonPhrase() +")");
    }
}

但是,上面的代碼並沒有解決為什么你得到一個空響應的問題。 我唯一正在處理它正在發生的事實是一種更優雅的方式。

但我注意到您在請求中需要用戶名。 您確定設備上存在用戶,如果不存在用戶,是否應該返回其他內容?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM