Я пытаюсь прочитать строку JSON с веб-страницы, но получаю сообщение об ошибке jsonexception типа org.json.JSONObject невозможно преобразовать в JSONArray.
final static String URL = "http://www2.park.se/~ts5124/";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
tv = (TextView)findViewById(R.id.text1);
client = new DefaultHttpClient();
new Read().execute("JSON");
if (logged=="yes") {
setContentView(R.layout.main);
} else {
setContentView(R.layout.login);
b1 = (Button)findViewById(R.id.btn);
name = (EditText)findViewById(R.id.name);
pass = (EditText)findViewById(R.id.password);
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
try {
JSONObject json = new JSONObject();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www2.park.se/~ts5124/login.php");
json.put("userName", name.getText().toString());
json.put("password", pass.getText().toString());
StringEntity se;
se = new StringEntity(json.toString(), "UTF-8");
// Add your data
httppost.setEntity(se);
httppost.setHeader("Accept", "application/json");
httppost.setHeader("Content-type", "application/json");
Log.i(TAG, json.toString());
// Execute HTTP Post Request
httpclient.execute(httppost);
} catch (JSONException je) {
} catch (IOException e) {
// TODO Auto-generated catch block
}
}
});
}
}
public JSONObject getData(String page) throws ClientProtocolException, IOException, JSONException {
StringBuilder url = new StringBuilder(URL);
url.append(page);
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 {
Log.i("JSON","Ain't workin'");
return null;
}
}
public class Read extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... params) {
try {
json = getData("send.php");
return json.getString(params[0]);
} catch (ClientProtocolException e) {
return e.toString();
} catch (IOException e) {
return e.toString();
} catch (JSONException e) {
return e.toString();
}
}
@Override
protected void onPostExecute(String result) {
tv.setText(result);
}
}
http://pastebin.com/dUnmsEd6 Я получаю это в logcat, и когда я отлаживаю, он говорит: jsonexception типа org.json.JSONObject не может быть преобразовано в JSONArray