Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8323857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:51:12+00:00 2026-06-08T23:51:12+00:00

I am trying to get data from a php file. This is my PHP

  • 0

I am trying to get data from a php file.

This is my PHP code:

$sql=mysql_query("select * from `tracking`");
while($row=mysql_fetch_assoc($sql))
$output[]=$row;
print(json_encode($output));

And I really need all the data. This is the code I use to get and convert the data:

String result = null;
InputStream is = null;
StringBuilder sb = null;

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://server/getData.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());
}

//convertion de la réponse en String
try {
    //BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"),8);
    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
    sb = new StringBuilder();
    sb.append(reader.readLine() + "\n");

    String line="0";
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
        //sb.append(line);
    }

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

// Affectation des données
try {
    JSONArray jArray = new JSONArray(result);
    Log.d("jArray", ""+jArray);
    JSONObject json_data= null;

    for(int i=0; i<jArray.length(); i++) {
        p = new Position();
        json_data = jArray.getJSONObject(i);

        Log.d("js", ""+json_data);
        id=json_data.getInt("id");
        lat=(float) json_data.getDouble("lat");
        lon=(float) json_data.getDouble("lon");
        speed=(float) json_data.getDouble("speed");
        alt=json_data.getDouble("alt");
        time=json_data.getString("time");
        date=json_data.getString("date");

        p.setId(id);
        p.setLat(lat);
        p.setLon(lon);
        p.setSpeed(speed);
        p.setAlt(alt);
        p.setDate(date);
        p.setTime(time);

        datasource.createPosition(p);
    }
} catch(JSONException e1) {
    Log.e("JSONEX", ""+e1);
} catch (ParseException e1) {
    e1.printStackTrace();
}

This really works on emulator nicely but not on my android phone and I don’t get the reason.
Now this is my json result from the server:

[{“id”:”180″,”lat”:”33.894707″,”lon”:”-6.327312″,”speed”:”0.00000″,”alt”:”397″,”date”:”29/07/2012″,”time”:”23:44″}]

It’s a valid JSONArray validated on http://jsonlint.com/

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T23:51:15+00:00Added an answer on June 8, 2026 at 11:51 pm

    I found what was wrong with JSON.
    I found at the beginning a character “?” added somewhere in the code and it was not visible until I stored the logCat entry to a file. So I added the code line and it works fine now.

    result = result.substring(1);
    

    I hope it may help someone in the future thanx to Waqas

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to get data back from a PHP file assigned with GET
Using php I am trying to get some data from a .csv file which
I'm trying to get character data from www.wowarmory.com using PHP and cURL. The code
So I'm trying to get data from my sql database and sort it by
I'm trying to retrieve data from a php file named return that just contains
I am trying to get Ajaz to pull data from mysql using a php
I'm trying to change some php code so that i get my values from
I'm trying to get some JSON data from a url like this: $url =
In my PHP file, I use this line to pull data from my mySQL
I am trying to get data from my interface, written in c, to another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.