Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have created a database and the php file using JSON :

= Connection Established - Database selected 
[{"latitude":"43222121","longitude":"2112212","description":"O masina rosie cu farurile stinse."},{"latitude":"33211322","longitude":"3211313","description":null}]

I'm using a XML with only one TextView. Here is the class:

public class ConnectMySql extends Activity {

 TextView httpStuff;
 HttpClient client;
 JSONObject json;
 final static String URL = "http://79.114.48.119/RadarsMySql.php";

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    httpStuff = (TextView) findViewById(R.id.tvHttp);
    client = new DefaultHttpClient();
    new Read().execute("latitude");
}


public JSONObject lastTweet(String username) throws ClientProtocolException, IOException,JSONException{
    StringBuilder url = new StringBuilder(URL);
    url.append(username);

    HttpGet get = new HttpGet(url.toString());
    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();
    //if(status == 200){
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);
        JSONArray timeline = new JSONArray(data);
        JSONObject last = timeline.getJSONObject(0);
        return last;

    //}else{ 
        //Toast.makeText(ConnectMySql.this, "error", Toast.LENGTH_LONG);
        //return null;

    //}
}

public class Read extends AsyncTask<String, Integer, String>{

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        try {
            json = lastTweet("");
            return json.getString(params[0]);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        httpStuff.setText(result);
    }

}

}

I followed a tutorial which was getting data from Tweeter and it works, but from my website it doesn't.

Can anyone help me by giving useful advice, or even better, telling me what I've done wrong?

Here is the logcat after i put the Log.i- thing:

05-05 19:44:07.109: W/System.err(717): org.json.JSONException: Expected literal value at character 0 of =

05-05 19:44:07.109: W/System.err(717):      

05-05 19:44:07.109: W/System.err(717):    Connection Established - Database selected[{"latitude":"43222121","longitude":"2112212","description":"O masina rosie cu farurile stinse."},{"latitude":"33211322","longitude":"3211313","description":null}]
05-05 19:44:07.109: W/System.err(717):  at org.json.JSONTokener.syntaxError(JSONTokener.java:446)
05-05 19:44:07.109: W/System.err(717):  at org.json.JSONTokener.readLiteral(JSONTokener.java:281)
05-05 19:44:07.109: W/System.err(717):  at org.json.JSONTokener.nextValue(JSONTokener.java:107)
05-05 19:44:07.109: W/System.err(717):  at org.json.JSONArray.<init>(JSONArray.java:87)
05-05 19:44:07.109: W/System.err(717):  at org.json.JSONArray.<init>(JSONArray.java:103)
05-05 19:44:07.109: W/System.err(717):  at com.project.radars.ConnectMySql.lastTweet(ConnectMySql.java:53)
05-05 19:44:07.119: W/System.err(717):  at com.project.radars.ConnectMySql$Read.doInBackground(ConnectMySql.java:71)
05-05 19:44:07.130: W/System.err(717):  at com.project.radars.ConnectMySql$Read.doInBackground(ConnectMySql.java:1)
05-05 19:44:07.130: W/System.err(717):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
05-05 19:44:07.130: W/System.err(717):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
05-05 19:44:07.130: W/System.err(717):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-05 19:44:07.130: W/System.err(717):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
05-05 19:44:07.130: W/System.err(717):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
05-05 19:44:07.130: W/System.err(717):  at java.lang.Thread.run(Thread.java:1096)
share|improve this question
1  
Does not work is not sufficient. say what is wrong: is there an error? do you receive unexpected result? anything else? – Boris Strandjev May 5 '12 at 19:27
it doesn't return me anything in the TextView and no, I do not receive any error. – stanga bogdan May 5 '12 at 19:29
This is still too much of guessing for us to do. Log the json array you receive so that we cna limit the cause of the problem. – Boris Strandjev May 5 '12 at 19:32
i put in this topic all the project, i don't understand what you're asking for. – stanga bogdan May 5 '12 at 19:39
JSONArray timeline = new JSONArray(data); I want to see the contents of this object. Do Log.i("TAG", timeline.toString()); and tell us what is printed in the logs as the contents of the array. – Boris Strandjev May 5 '12 at 19:42
show 4 more comments

1 Answer

And here goes your problem: I have connected to the website of yours and literally received this string:

= Connection Established - Database selected[{"latitude":"43222121","longitude":"2112212","description":"O masina rosie cu farurile stinse."},{"latitude":"33211322","longitude":"3211313","description":null}]

However you are trying to load this in JSONArray, which will fail of course, because the = Connection Established - Database selected prefix is not part of any legal json array.

You need to truncate this prefix before you can parse the array. The best option will be if you remove the prefix from the php code (which I am pretty sure you add manually in order to log). However, if this is difficult for you you can do it on the java side. Add the following line before the parsing of the json array:

String data = EntityUtils.toString(e);
data = data.substring(data.indexOf("["));
share|improve this answer
"the method firstIndexOf is undefined for type String " . I can only use indexOf or lastIndexOf , what should i do ? btw : I erased that two echos and now the webiste starts like this : =[...] . – stanga bogdan May 5 '12 at 19:58
@stangabogdan I had already corrected my answer by the time you posted your comment. indexOf is the one you need. If you correct the php return, remove the = sign infront of the array too (then you will not need the substring thing). – Boris Strandjev May 6 '12 at 19:32
@stangabogdan I am sorry for the very late answer, but yesterday I went to sleep after the answer, and today I had work the whole day.\ – Boris Strandjev May 6 '12 at 19:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.