38557

error parsing json data (value of type java.lang.String cannot be converted to json array)

Question:

i am trying to implement simple android application where i am trying to retrieve the database values over php site, but the following code generates error parsing json data.

my php file encoding json is as below

getdeals.php <?php $host="localhost"; // Host name $username="root"; // Mysql username $password=""; // Mysql password $db_name="user"; // Database name $tbl_name="deal"; // Table name // Connect to server and select databse. mysql_connect("$host","$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $q=mysql_query("SELECT * FROM deal"); while($e=mysql_fetch_assoc($q)) $output[]=$e; print(json_encode($output)); mysql_close(); ?>

table deal is as below <img alt="table deal" class="b-lazy" data-src="https://i.stack.imgur.com/OJ5qT.png" data-original="https://i.stack.imgur.com/OJ5qT.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

android class is as below

public class JsonConnect extends Activity { InputStream is; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_json_connect); addListenerOnbutton(); Thread t = new Thread() { public void run() { getDeals(); } }; t.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_json_connect, menu); return true; } public void addListenerOnbutton() { final Context context = this; Button home = (Button)findViewById(R.id.button1); home.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); } public void getDeals() { String result = ""; //the year data to send //ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); //nameValuePairs.add(new BasicNameValuePair("year","1980")); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://10.0.2.2/dealnow/getdeals.php"); //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); HttpResponse response = httpclient.execute(httppost); HttpEntity entity = response.getEntity(); is = entity.getContent(); } catch(Exception e){ Log.e("log_tag", "Error in http connection "+e.toString()); } //convert response to string 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(); result=sb.toString(); }catch(Exception e){ Log.e("log_tag", "Error converting result "+e.toString()); } //parse json data try{ JSONArray jArray = new JSONArray(result); for(int i=0;i<jArray.length();i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","dealid: "+json_data.getString("deal_id")+ ", hotel name: "+json_data.getString("hotel_name")+ ", valid date: "+json_data.getInt("valid_date")+ ", location: "+json_data.getString("location")+ ", website: "+json_data.getString("website") ); } } catch(JSONException e){ Log.e("log_tag", "Error parsing data "+e.toString()); } } }

and finally logcat output is

05-11 01:23:08.767: D/dalvikvm(435): GC_EXPLICIT freed 24K, 4% free 6582K/6855K, paused 7ms+4ms 05-11 01:29:24.164: V/TLINE(497): new: android.text.TextLine@4065aa30 05-11 01:29:24.757: D/dalvikvm(497): GC_CONCURRENT freed 115K, 4% free 6560K/6791K, paused 5ms+14ms 05-11 01:29:24.764: E/log_tag(497): Error parsing data org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONArray 05-11 01:29:24.844: V/TLINE(497): new: android.text.TextLine@4065d8e0 05-11 01:34:12.147: D/dalvikvm(497): GC_EXPLICIT freed 48K, 4% free 6524K/6791K, paused 7ms+3ms

how can i get the desired values of the table

please help.

Answer1:

The response returned by PHP script is not in valid JSON format.

You need to put $output=array(); somewhere before the while loop

Recommend

  • Android button navigation with onclick listeners
  • Listview with multiple strings
  • How to provide objects of the same type? Dagger2
  • ant copy copying order
  • NLog Configuration API: Using Layouts stored in variables
  • Set indent on view in Zend Framework
  • how to swap Image in jquery
  • onLeScan never gets called android
  • Store additional information during registration with Firebase in Android
  • How to use getFilter form Listadapter
  • How to add an item to a ListView with each Button click
  • Background not visible in surface view
  • Visual Studio 2008 and JavaScript brackets formatting
  • Unable to use dot layout (graphviz as a library)
  • OnItemSelectedListener for Spinner that was added programmatically does not trigger
  • Android: How to open the webview in a new screen
  • Listview - list item selection and initial setting (custom made items)
  • How do i disable a text box within an iframe
  • I have two activity.I want to Send the data from second activity to previous activity.First activity
  • Custom WebViewPage inject code when razor template is rendering
  • Unable to resolve Static method?
  • Activity overlaying. WindowManager.LayoutParams
  • How to get listview position?
  • Syntax error on tokens, AnnotationName expected instead - error on query
  • WPF version of .ScaleControl?
  • 550 Access denied - Invalid HELO name
  • Android Activity.onWindowFocusChanged doesn't get called from within TabHost
  • SharedPreferences or SQLite Database?
  • How to determine if there are bytes available to be read from boost:asio:serial_port
  • Unity3D & Android: Difference between “UnityMain” and “main” threads?
  • Spring Data JPA custom method causing PropertyReferenceException
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Spray.io: When (not) to use non-blocking route handling?
  • Perl system calls when running as another user using sudo
  • align graphs with different xlab
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize