0

HERE is php file result

Array[{"username":"abc","userpassword":"abc","id":"123456789"}]

Here is Code of getting response through HTTPConnection ..

        protected String doInBackground(String... params) {
        HttpURLConnection conn = null;

        try {
            // create connection
            URL wsURL=new URL(params[0]);
            conn=(HttpURLConnection) wsURL.openConnection();
            conn.setConnectTimeout(15000);;
            conn.setReadTimeout(10000);

            // get data
            InputStream in = new BufferedInputStream(conn.getInputStream());

            // converting InputStream into String

            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(in,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                in.close();

                String jsonResult=sb.toString();
                return jsonResult;
            }
            catch(Exception e){
                Log.e("log_tag", "Error  converting result " + e.toString());
            }

And The Code Through Which i am parsing string result into jsonArray and JsonObject

 protected void onPostExecute(String result) {

        try {
            JSONArray jArray = new JSONArray(result);
            JSONObject json = jArray.getJSONObject(0);
            String username = json.getString("username");

        } catch (Exception e) {
            error2 = e.getMessage().toString();
            // TODO: handle exception
            Log.e("log_tag", "Error Parsing Data "+e.toString());
        }
        dialog.dismiss();
    }

There is an error that "Value of type java.lang.String cannot be converted to JSONArray"

5
  • 3
    See where the word 'Array' is prepended to your JSON string? It shouldn't be. Commented Jan 26, 2016 at 18:27
  • sorry i did not get what you said .... ! can u explain me Commented Jan 26, 2016 at 18:37
  • Your PHP code is printing out an array before the JSON. PHP expertly handles the array-to-string conversion by printing the literal string "Array". Fix your PHP. Commented Jan 26, 2016 at 18:59
  • can you post complete php response Commented Jan 26, 2016 at 19:02
  • stackoverflow.com/users/1064767/sammitch Thanks for your Help... i got that ... this was stupid check point... ;) Commented Jan 26, 2016 at 19:53

1 Answer 1

0

Try this code

  try {
            result  = result.replace("Array","");
            JSONArray jArray = new JSONArray(result);
            JSONObject json = jArray.getJSONObject(0);
            String username = json.getString("username");

        } catch (Exception e) {
            error2 = e.getMessage().toString();
            // TODO: handle exception
            Log.e("log_tag", "Error Parsing Data "+e.toString());
        }
        dialog.dismiss();
Sign up to request clarification or add additional context in comments.

3 Comments

[{"example":"This text foolishly contains the word 'Array'."}]
The response will be Array[{"example":"This text foolishly contains the word 'Array'."}]. because Array will always be appended before the JsonArray. And I am using replace and not replaceAll. So it will clear only the first occurrence.
yep i got that ... i review my php file and got this stupid check point ;) thanks for your help... !

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.