我使用 JSON 创建了一个数据库和 php 文件:
= Connection Established - Database selected
[{"latitude":"43222121","longitude":"2112212","description":"O masina rosie cu farurile stinse."},{"latitude":"33211322","longitude":"3211313","description":null}]
我正在使用一个只有一个的 XML TextView
。这是课程:
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);
}
}
}
我遵循了一个从 Tweeter 获取数据的教程,它可以工作,但在我的网站上却没有。
任何人都可以通过提供有用的建议来帮助我,或者更好地告诉我我做错了什么吗?
这是我放入 Log.i- 之后的 logcat:
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)