簡體   English   中英

Android在從服務器接收到數據時會讀取JsonArray中的不同數據

[英]Android reads different data in JsonArray on receiving from server

我正在使用API​​,但遇到了奇怪的事情。 當我發出一個API請求時,會收到一個JSON響應。 在瀏覽器中,我收到以下JSON:

[  
   { 
      "SalesAgentPhone":"829698539",
      "DriverStatus":"CustomerRescheduled",
      "DriverStatusDescription":[  
         {  
            "Reason":"sfdfsdf",
            "Date":"2016-10-02",
            "SlotId":"1"
         }
      ]
   }]

但是我在Android設備上收到了另一個JSON響應。

[  
   { 
      "SalesAgentPhone":"829698539",
      "DriverStatus":"CustomerRescheduled",
      "DriverStatusDescription":[  
         {"Reason":"sfdfsdf","Date":"2016-10-02","SlotId":"1"     
         }
      ]
   }]

這是我用來發出請求的課程

public class NewPost extends AsyncTask<String, Integer, String> {
    private final Context context;
    private final Registerinterface inter;
    private int response_code = 0;
    private HashMap<String, String> postDataParams;
    private ProgressDialog prgDialog;

    public NewPost(Context c, Registerinterface inter, HashMap<String, String> postParamater) {
        context = c;
        this.inter = inter;
        this.postDataParams = postParamater;


    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        if (!new NetworkStatus(context).isNetworkAvailable()) {
            inter.Result("", Constants.No_Internet);
            this.cancel(true);
        } else {
            prgDialog = new ProgressDialog(new ContextThemeWrapper(context, android.R.style.Theme_DeviceDefault_Light_Dialog));
            prgDialog.setMessage("Loading...");
            prgDialog.show();
            prgDialog.setIndeterminate(false);
        }
    }

    @Override
    protected String doInBackground(String... param) {

        URL url;
        String response = "";
        try {
            url = new URL(param[0]);

            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(15000);
            conn.setConnectTimeout(15000);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.setDoOutput(true);
            OutputStream os = conn.getOutputStream();
            BufferedWriter writer = new BufferedWriter(
                    new OutputStreamWriter(os, "UTF-8"));
            writer.write(getPostDataString(postDataParams));
            writer.flush();
            writer.close();
            os.close();
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpsURLConnection.HTTP_OK) {
                String line;
                StringBuilder sb = new StringBuilder();
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                while ((line = br.readLine()) != null)
                    sb.append(line + "\n");
                response = sb.toString();
                return response;
            } else {
                response = "";
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        prgDialog.dismiss();
        if (result == null || result.equals("null"))
            inter.Result("null", response_code);
        else
            inter.Result(result, response_code);
    }

    private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
        StringBuilder result = new StringBuilder();
        boolean first = true;
        for (Map.Entry<String, String> entry : params.entrySet()) {
            if (first)
                first = false;
            else
                result.append("&");

            result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
            result.append("=");
            result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
        }
        return result.toString();
    }
}

我已經在這個問題上停留了幾個小時,我們將不勝感激。

您可以從JSON數據中刪除。

 JSON.parse(data.replace(/&quot;/g,'"'));

不過,您可能想修復JSON編寫腳本,因為“在JSON對象中無效。

暫無
暫無

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

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