Note that there are some explanatory texts on larger screens.

plurals
  1. PORetrieving data from database
    primarykey
    data
    text
    <p>I am trying to retrieve data from a database and I am using code that I found on blogs with some changes.</p> <p>This is my php file:</p> <pre><code>&lt;?php mysql_connect("host","username","password"); mysql_select_db("peopledata"); $q=mysql_query("SELECT * FROM people WHERE birthyear&gt;'".$_REQUEST['year']."'"); while($e=mysql_fetch_assoc($q)) $output[]=$e; print(json_encode($output)); mysql_close();?&gt; </code></pre> <p>I have created the following database:</p> <pre><code> CREATE TABLE people ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY , name VARCHAR( 100 ) NOT NULL , sex BOOL NOT NULL DEFAULT '1', birthyear INT NOT NULL ) </code></pre> <p>And this is my code in my Android Java application:</p> <pre><code>public class SnowReportActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); setImageClickListener(); } private void setImageClickListener() { ImageView map_image=(ImageView)findViewById(R.id.map_icon); map_image.setOnTouchListener(new ImageView.OnTouchListener() { //OnTouchListener listener = new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if(!(event.getAction() == MotionEvent.ACTION_DOWN)) return false; //If the touch event was not putting the finger down on the screen, return false(Actions may be move, up, and so on) final float x = event.getX(); final float y = event.getY(); //System.out.println("Coordinates of button pressed are: X is %d"+x+" and Y is %d"+ y); if(x and y in some range) DoFirst(); //... and so on... //In the end, you must return a boolean saying whether you "consumed" the event - if you handled the event or not. return true; } }); } @SuppressWarnings("null") private void DoFirst() { Log.d("SnowReportApp","Do first thing"); setContentView(R.layout.parnassos); String result = ""; InputStream is = null; StringBuilder sb=null; //the year data to send ArrayList&lt;NameValuePair&gt; nameValuePairs = new ArrayList&lt;NameValuePair&gt;(); nameValuePairs.add(new BasicNameValuePair("year","1980")); //http post try{ HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost("http://example/httpdocs/getAllPeopleBornAfter.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); 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&lt;jArray.length();i++){ JSONObject json_data = jArray.getJSONObject(i); Log.i("log_tag","id: "+json_data.getInt("id")+ ", name: "+json_data.getString("name")+ ", sex: "+json_data.getInt("sex")+ ", birthyear: "+json_data.getInt("birthyear") ); } }catch(JSONException e){ Log.e("log_tag", "Error parsing data "+e.toString()); } } } </code></pre> <p>The database is created on a server of my webpage. I haven't given the url, but instead I have written example.com. The problem is that in my logcat I see the html code of my whole website and an extra warning: </p> <blockquote> <p>Error parsing data org.json.JSONException: A JSONArray text must start with '[' at character 2 of </p> </blockquote> <hr> <p>When my server is on my site I get my whole html code because I do not provide on my code the username and password required from my server. The httppost takes as argument only the URL. If my server is on my local machine then by just giving the ip address of the machine I see the correct results. Can anyone help me on how to provide the username and password?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload