Results 1 to 4 of 4
  1. #1
    snaperdak is offline Member
    Join Date
    Mar 2015
    Posts
    2
    Rep Power
    0

    Question Error org.json.JSONException

    Hello. I am creating an application for Android. It is the task of retrieving data from an external database server mysql.
    Everything was fine, but by the time of compilation. Gave me the error:

    Java Code:
    Errororg.json.JSONException Value <? Xml of type java.lang.String can not be converted this JSONObject

    A link to a database using php script:
    PHP Code:
    $host="localhost";
    $username="root";
    $password="password";
    $db_name="baza";
     
    $con=mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    $sql = "select * from zastep";
    $result = mysql_query($sql);
    $json = array();
     
    if(mysql_num_rows($result)){
    while($row=mysql_fetch_assoc($result)){
    $json['zastep'][]=$row;
    }
    }
    mysql_close($con);
    echo json_encode($json);
    ?>
    The script is working correctly, the error shows up when compiling apk.
    Java Code:
    import android.app.Activity;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.view.Menu;
    import android.widget.ListView;
    import android.widget.SimpleAdapter;
    import android.widget.Toast;
     
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
     
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
     
     
    public class MainActivity extends Activity{
        private String jsonResult;
        private String url = "http://192.168.2.100/tyr/conn.php";
        private ListView listView;
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            listView = (ListView) findViewById(R.id.listView);
            accessWebService();
        }
         @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
        //uzyskiwanie dostępu do sieci
        private class JsonReadTask extends AsyncTask<String,Void, String> {
            @Override
            protected String doInBackground(String... params)
            {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(params[0]);
                try
                {
                    HttpResponse response = httpclient.execute(httppost);
                    jsonResult = inputStreamToString(response.getEntity().getContent()).toString();
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                return null;
            }
            private StringBuilder inputStreamToString(InputStream is)
            {
                String rLine = "";
                StringBuilder answer = new StringBuilder();
                BufferedReader rd = new BufferedReader(new InputStreamReader(is));
                try {
                    while ((rLine = rd.readLine())!= null)
                    {
                        answer.append(rLine);
                    }
                } catch (IOException e) {
                    Toast.makeText(getApplicationContext(), "Error..." + e.toString(), Toast.LENGTH_LONG).show();
                }
                return answer;
            }
            @Override
            protected void onPostExecute(String result)
            {
                ListDrwaer();
            }
        }
        public void accessWebService()
        {
            JsonReadTask task = new JsonReadTask();
            task.execute(new String[] {url});
        }
        public void ListDrwaer()
        {
            List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
            try
            {
                JSONObject jsonResponse = new JSONObject(jsonResult);
                JSONArray jsonMainNode = jsonResponse.optJSONArray("zastep");
                for (int i = 0; i < jsonMainNode.length(); i++)
                {
                    JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
                    String name = jsonChildNode.optString("klasa");
                    String data = jsonChildNode.optString("data");
                    String outPut = name + "-" + data;
                    employeeList.add(createEmployee("employees", outPut));
                }
            }
            catch (JSONException e)
            {
                Toast.makeText(getApplicationContext(), "Error" + e.toString(),Toast.LENGTH_SHORT).show();
            }
            SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,android.R.layout.simple_list_item_1, new String[]{"employees"}, new int[] {android.R.id.text1});
            listView.setAdapter(simpleAdapter);
        }
        private HashMap<String, String> createEmployee(String name, String data)
        {
            HashMap<String, String> employeeNameNo = new HashMap<String,String>();
            employeeNameNo.put(name, data);
            return employeeNameNo;
        }
     
    }
    please help

  2. #2
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Error org.json.JSONException

    In other words: you're feeding XML data to a JSON parser.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  3. #3
    snaperdak is offline Member
    Join Date
    Mar 2015
    Posts
    2
    Rep Power
    0

    Default Re: Error org.json.JSONException

    ok but I do not understand where the problem and how to fix it.

  4. #4
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Error org.json.JSONException

    Neither do I. I hope there is some documentation for you to find.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. org.json.JSONException: Duplicate key
    By blertadhimitri in forum New To Java
    Replies: 11
    Last Post: 12-05-2013, 12:41 PM
  2. Ask Json
    By ultra.coder in forum Advanced Java
    Replies: 0
    Last Post: 11-19-2013, 04:08 AM
  3. Json
    By mac in forum New To Java
    Replies: 10
    Last Post: 06-08-2010, 11:15 PM
  4. JSON Lib: json-lib-1.0-jdk13.jar
    By Raghuraman K in forum Advanced Java
    Replies: 3
    Last Post: 05-13-2010, 10:00 AM
  5. JSON Parsing
    By prakashkadakol in forum New To Java
    Replies: 1
    Last Post: 01-04-2010, 07:13 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •