我试图从PoliceData Api获取数据时有点卡住了。
这是我的代码:
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Latest_crimes extends AppCompatActivity {
String Crimes_Url;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_latest_crimes);
Crimes_Url = "https://data.police.uk/api/crimes-street/all-crime?lat=52.629729&lng=-1.131592&date=2017-01";
new Latest_crimes.AsyncHttpTask().execute(Crimes_Url);
}
String streamToString(InputStream stream) throws IOException {
BufferedReader bufferesReader = new BufferedReader(new InputStreamReader(stream));
String data;
String result = "";
while ((data = bufferesReader.readLine()) != null) {
result += data;
}
if (null != stream) {
stream.close();
}
return result;
}
private void parseResult(String result) {
JSONObject response = null;
try {
response = new JSONObject(result);
JSONArray crimes = response.optJSONArray("category");
for (int i = 0; i < crimes.length(); i++) {
JSONObject crime = crimes.optJSONObject(i);
String category = crimes.getJSONObject(0).getString("category");
Log.i("Title", category);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public class AsyncHttpTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String result = "";
URL url;
HttpURLConnection urlConnection = null;
try {
url = new URL(urls[0]);
urlConnection = (HttpURLConnection) url.openConnection();
String response = streamToString(urlConnection.getInputStream());
parseResult(response);
return result;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
}
当它运行时我收到了这个错误:
W / System.err:org.json.JSONException和数据未显示在 正确的格式。
我是JSON和Android工作室的新手,基本上,我不知道我做错了什么。
你能告诉我我做错了什么吗?LE
11-21 19:09:24.997 8236-8257 / com.example.cosmin.crimeratev001 W / System.err:org.json.JSONException:Value [{ “类别”: “反社会行为”, “LOCATION_TYPE”: “力”, “位置”:{ “纬度”: “52.632469”, “街”:{ “ID”:883334, “名”: “上 或靠近Pocklingtons Walk“}
11-21 19:09:24.997 8236-8257 / com.example.cosmin.crimeratev001 W / System.err:at org.json.JSON.typeMismatch(JSON.java:111)
11-21 19:09:24.997 8236-8257 / com.example.cosmin.crimeratev001 W / System.err:at org.json.JSONObject。(JSONObject.java:160)
11-21 19:09:24.997 8236-8257 / com.example.cosmin.crimeratev001 W / System.err:at org.json.JSONObject。(JSONObject.java:173)
11-21 19:09:24.997 8236-8257 / com.example.cosmin.crimeratev001 W / System.err:at com.example.cosmin.crimeratev001.Latest_crimes.parseResult(Latest_crimes.java:49)
11-21 19:09:24.997 8236-8257 / com.example.cosmin.crimeratev001 W / System.err:at com.example.cosmin.crimeratev001.Latest_crimes.access $ 000(Latest_crimes.java:18)
11-21 19:09:24.997 8236-8257 / com.example.cosmin.crimeratev001 W / System.err:at com.example.cosmin.crimeratev001.Latest_crimes $ AsyncHttpTask.doInBackground(Latest_crimes.java:75)
11-21 19:09:24.997 8236-8257 / com.example.cosmin.crimeratev001 W / System.err:at com.example.cosmin.crimeratev001.Latest_crimes $ AsyncHttpTask.doInBackground(Latest_crimes.java:64)
11-21 19:09:24.997 8236-8257 / com.example.cosmin.crimeratev001 W / System.err:在android.os.AsyncTask $ 2.call(AsyncTask.java:305)
11-21 19:09:24.997 8236-8257 / com.example.cosmin.crimeratev001 W / System.err:at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-21 19:09:24.997 8236-8257 / com.example.cosmin.crimeratev001 W / System.err: 在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:243)
11-21 19:09:24.997 8236-8257 / com.example.cosmin.crimeratev001 W / System.err: 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
11-21 19:09:24.997 8236-8257 / com.example.cosmin.crimeratev001 W / System.err:at java.util.concurrent.ThreadPoolExecutor中$ Worker.run(ThreadPoolExecutor.java:607)
11-21 19:09:24.998 8236-8257 / com.example.cosmin.crimeratev001 W / System.err:at java.lang.Thread.run(Thread.java:761)