开发者

JSON parsing problem

开发者 https://www.devze.com 2023-03-31 18:45 出处:网络
i am trying to Json parsing in my android app the link is https://www.buzzador.com/apps/present_software/webservice/index.php?op=ProductQ&campaign_id=607&userid=10776

i am trying to Json parsing in my android app the link is https://www.buzzador.com/apps/present_software/webservice/index.php?op=ProductQ&campaign_id=607&userid=10776

when i put it into Json object it gives errors to me error is : 08-31 14:40:52.281: WARN/System.err(416): org.json.JSONException: Value of type java.lang.String cannot be converted to JSONObject

public static String getmyproductquestiondetails(String userid,
        String campaignid) {// https://www.buzzador.com/apps/present_software/webservice/index.php?op=EducationResult&userid=1&questionid=1,2,3&answergivenbyuser=1,1,0
    String data = null;
    try {

        URL url = new URL(
                "http://dignizant.com/buzz/webservice/index.php?op=getProductQuestion&userid="
                        + userid + "&campaign_id=" + campaignid);
        if (url.getProtocol().toLowerCase().equals("https")) {
            trustAllHosts();
            HttpsURLConnection https = (HttpsURLConnection) url
                  开发者_如何学JAVA  .openConnection();
            https.setHostnameVerifier(DO_NOT_VERIFY);
            http = https;
        } else {
            http = (HttpURLConnection) url.openConnection();
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    Utils utils = new Utils();

    try {
        data = utils.convertStreamToString(http.getInputStream());
        System.out.println("getproduct details response :: " + data);

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        data = e.toString();

    }
    return data;
}







try {


        JSONObject jo = new JSONObject(response);

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
    }


  char[] utf8 = null;
            StringBuilder properString = new StringBuilder("");

            utf8 = Response.toCharArray();

            for (int i = 0; i < utf8.length; i++) {
                if ((int) utf8[i] < 65000) {
                    properString.append(utf8[i]);
                }
            }
  System.out.println("Response of Login::"
                        + properString.toString());


Had similar problem. At first my app was working great on both androids 4.0+ and 4.0- (2.3.3 2.2 etc). after a revision i have that problem. JSonarray could parse on 2.3.3

PROBLEM: Json STRING (response from server) comes with a character ' in front so actual response= '[{"1":"omg"}] and not the correct one [{"1":"omg"}]

Solution: if string dosent start with [ then edit response string (remove the ' character)

  if (result.startsWith("["))
      {



      }
      else
      {
          result= result.substring(1);
      }

after then everything worked fine for me


If you are a using json-lib-2.4 as library, which I assume, you can parse strings with :

JSONSerializer.toJSON(yourString).toString() 

instead of using the JsonObject class


To remove character like (\n) or unwanted character in json string used commons-lang3:3.4 library in program . i used this class to remove unwanted character in json string "StringEscapeUtils.unescapeJava(string)".

this will help you.

0

精彩评论

暂无评论...
验证码 换一张
取 消