0

I'm creating Database from JSON. I get this error when I run app: Problem parsing the news JSON results org.json.JSONException: Value Trump of type java.lang.String cannot be converted to JSONObject

Here is code:

private static List<News> extractFeatureFromJson(String newsJSON) {
    if (TextUtils.isEmpty( newsJSON )) {
        return null;
    }

    List<News> newsall = new ArrayList<>();

    try {
        JSONObject data = new JSONObject(newsJSON);
        JSONArray results = data.getJSONArray("articles");
        for (int i = 0; i < results.length(); i++) {
            JSONObject obj = results.getJSONObject(i);
            String webTitle = obj.getString("title");
            String webUrl = obj.getString("url");
            String webImage = obj.getString( "urlToImage" );

            List<NewsForDB> newsForDB = new ArrayList<>(  );
            Gson gson = new Gson();
            NewsForDB webTitleForDB=gson.fromJson( extractFeatureFromJson( webTitle ).toString(), NewsForDB.class );
            newsForDB.add(webTitleForDB);
            NewsForDB urlForDB=gson.fromJson( extractFeatureFromJson( webUrl ).toString(), NewsForDB.class );
            newsForDB.add(urlForDB);
            NewsForDB webImageForDB=gson.fromJson( extractFeatureFromJson( webImage ).toString(), NewsForDB.class );
            newsForDB.add( webImageForDB );


            News news = new News(webTitle, webUrl, webImage);
            newsall.add(news);

        }



    } catch (JSONException e) {
        Log.e("QueryUtils", "Problem parsing the news JSON results", e);
    }
    return newsall;
}

When there are no this lines of code

List<NewsForDB> newsForDB = new ArrayList<>(  );
        Gson gson = new Gson();
        NewsForDB webTitleForDB=gson.fromJson( extractFeatureFromJson( webTitle ).toString(), NewsForDB.class );
        newsForDB.add(webTitleForDB);
        NewsForDB urlForDB=gson.fromJson( extractFeatureFromJson( webUrl ).toString(), NewsForDB.class );
        newsForDB.add(urlForDB);
        NewsForDB webImageForDB=gson.fromJson( extractFeatureFromJson( webImage ).toString(), NewsForDB.class );
        newsForDB.add( webImageForDB );

Everything works great, but I'm trying to prepare code to use ActiveAndroid ORM.

Any suggestions?

Thanks!

UPDATE:

I am following first answer from here.

News:

public class News {
private String mWebTitle;
private String mUrl;
private String mImage;

public News(String webTitle, String webUrl, String webImage) {
    mWebTitle = webTitle;
    mUrl = webUrl;
    mImage = webImage;

}

public String getWebTitle() {
    return mWebTitle;
}
public String getUrl() {
    return mUrl;
}
public String getWebImage() {return mImage;}

}

And here is part of code from QueryUtils, which is before that posted in first question

public static List<News> fetchNewsData(String requestUrl) {
    URL url = createUrl( requestUrl );

    String jsonResponse = null;
    try {
        jsonResponse = makeHttpRequest( url );
    } catch (IOException e) {
        Log.e( LOG_TAG, "Problem making the HTTP request.", e );
    }

    List<News> news = extractFeatureFromJson( jsonResponse );

    return news;
}

And here is NewsForDB.java

    import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;


@Table(name = "NewsDB")

public class NewsForDB extends Model {

    @Column(name = "title", unique = true, onUniqueConflict = Column.ConflictAction.REPLACE)
    public String title;

    @Column(name = "url")
    public String url;

    @Column(name = "urlToImage")
    public String urlToImage;

    public NewsForDB(){
        super();
    }

    public NewsForDB(String title, String url, String urlToImage){
        super();
        this.title = title;
        this.url = url;
        this.urlToImage = urlToImage;
    }
}

Thank You!

zokistone
  • 7
  • 6

2 Answers2

0

I think problem occurs when you make recursive in function extractFeatureFromJson when you call extractFeatureFromJson( webTitle ), and so on.

Maybe you should share your class News, NewsForDB, testcases which make exception, and can you tell me the purpose you create an object newsForDB without usage?

Ha. Huynh
  • 1,772
  • 11
  • 27
0

you can edit code just like this !

private static List<News> extractFeatureFromJson(String newsJSON) {
    if (TextUtils.isEmpty( newsJSON )) {
        return null;
    }

    List<News> newsall = new ArrayList<>();

    try {
        JSONObject data = new JSONObject(newsJSON);
        JSONArray results = data.getJSONArray("articles");
        for (int i = 0; i < results.length(); i++) {
            Gson gson = new Gson();
            JSONObject obj = results.getJSONObject(i);
            News news = gson.fromJson(obj.toString() , News.class);
            newsall.add(news);
        }
    } catch (JSONException e) {
        Log.e("QueryUtils", "Problem parsing the news JSON results", e);
    }
    return newsall;
}
hamid
  • 171
  • 1
  • 1
  • 9