0

i am using this JSONParser.java:

public class JSONParser {

    static InputStream is = null;
    static JSONObject jObj = null;
    static String json = "";

    // constructor
    public JSONParser() {

    }

    public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setEntity(new UrlEncodedFormEntity(params));

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

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

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            Log.e("JSON", json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);            
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}

it is working fine for my other requests. Now my php-file sending an array. How I convert it to an Jarray or directly in an ArrayList, which i can use in my app?

the array (2 items) looks like:

03-15 20:40:16.667: E/JSON(26100): [{"ean":"8029694000","name":"KRINGS-VERBAU","betriebsdatenalt":"412"},{"ean":"8026786937","name":"KOMPRESSOR FAHR  5,3 M3  XAS97DDG","betriebsdatenalt":"0"}]
2
  • Have you looked at developer.android.com/reference/org/json/JSONArray.html ? Commented Mar 15, 2015 at 19:58
  • As an aside I'd seriously reccomend looking at gson or jackson for dealing with json; the org.json library is not the most pleasant to work with Commented Mar 15, 2015 at 19:59

1 Answer 1

2

If you have an array

      JSONArray myArray=new JSONArray(string)

now loop through the array and get the objects like

    ArrayList<JSONObject> yourObjects=new ArrayList<JSONObject>(); 
    for(int i=0;i<myArray.length();i++){
        yourObjects.add(myArray.getJSONObject(i))
    }

also check every time if its a object or array you are getting like if

     JSONObject obj=new JSONObject(string from server);
     if(obj==null){
         JSONArray array=new JSONArray(string from server)
     }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.