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)
JSONArray timeline = new JSONArray(data);
I want to see the contents of this object. DoLog.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