我收到以下错误:
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;
}
}
我已经在这个代码上工作了几天..我是Android应用程序开发的初学者。所以任何帮助都非常感谢。
非常感谢
答案 0 :(得分:0)
json字符串中可能有一些特殊/不可见的字符。您可以在转换为JSONObject之前打印结果字符串,或者在某些编辑器中打开它以显示隐藏的字符并查看json是否正确。您还可以将UTF 8标准用于输入流。