我收到以下错误:
org.json.JSONException: Value test( of type java.lang.String cannot be converted to JSONObject
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: org.json.JSONException: Value test( of type java.lang.String cannot be converted to JSONObject
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at org.json.JSONObject.<init>(JSONObject.java:160)
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at com.adeel.android.flightdemo.FlightDataModel.onPostExecute(FlightDataModel.java:86)
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at com.adeel.android.flightdemo.FlightDataModel.onPostExecute(FlightDataModel.java:21)
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at android.os.AsyncTask.finish(AsyncTask.java:632)
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at android.os.AsyncTask.access$600(AsyncTask.java:177)
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at android.os.Looper.loop(Looper.java:145)
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5832)
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at java.lang.reflect.Method.invoke(Native Method)
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
07-18 18:58:11.424 25827-25827/com.adeel.android.flightdemo W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
我收到此错误的行是:
JSONObject object = new JSONObject(result);
这是我的代码:
import android.os.AsyncTask;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class FlightDataModel extends AsyncTask<String, Void, String>{
private String data = "https://api.themoviedb.org/3/movie/550?api_key=f1d314280284e94ff7d1feeed7d44fdf&callback=test";
URL mURL;
HttpURLConnection mHttpURLConnection;
String result = "" ;
String main;
JSONArray mArray = null;
private String mImageIcon;
private String movieTitle;
private String movieDesc;
private String movieRating;
@Override
protected String doInBackground(String... strings) {
try {
Log.d("FlightDemo", "doInBackground method called");
mURL = new URL(data);
mHttpURLConnection = (HttpURLConnection)mURL.openConnection();
mHttpURLConnection.connect();
InputStream inputStream = mHttpURLConnection.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
int data = inputStreamReader.read();
while (data != -1){
char current = (char) data;
result += current;
data = inputStreamReader.read();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
@Override
protected void onPostExecute(String s) {
//What to do when the doInBackground Method is executed..
try {
//JSONObject json = new JSONObject(result);
JSONObject object = new JSONObject(result);
// JSONArray array = new JSONArray(result);
Log.d("FlightDemo", object.toString());
} catch (JSONException e) {
e.printStackTrace();
}
super.onPostExecute(s);
}
public String getImageIcon() {
return mImageIcon;
}
public String getMovieTitle() {
return movieTitle;
}
public String getMovieDesc() {
return movieDesc;
}
public String getMovieRating() {
return movieRating;
}
public String getData() {
return data;
}
}
我已经在这个代码上工作了几天。我是安卓应用程序开发的初学者。因此,非常感谢任何帮助。
非常感谢
json 字符串中可能有一些特殊/不可见的字符。您可以在转换为 JSONObject 之前打印结果字符串,也可以在某个编辑器中打开它以显示隐藏字符并查看 json 是否正确。您还可以对输入流使用 UTF 8 标准。