Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Logcat says:
W/System.err: org.json.JSONException

Can someone please help me sort this out by going through the code.

Here is the code: Mainactivity.java

 public class MainActivity extends AppCompatActivity {
 Button click;
 public static  TextView tv_data;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    click = (Button)findViewById(R.id.secondactivity);
    tv_data = (TextView)findViewById(R.id.fetcheddata);

    click.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            fetchData process   = new fetchData();
            process.execute();

        }
    });
}
} 

fetchData.java:

public class fetchData extends AsyncTask<Void,Void,Void> {

String products="";
String dataParsed = "";
String singleParsed = "";
@Override
protected Void doInBackground(Void... voids) {
    try {
        URL url = new URL("http://staging.giftintime.com/mobile/categories/4/subcategories");
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = httpURLConnection.getInputStream();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));

        String line="";
        while (line!=null){
            line= bufferedReader.readLine();
            products= products+line;
        }

        JSONArray JA = new JSONArray(products);
        for(int i=0;i<JA.length();i++){
            JSONObject JO = (JSONObject) JA.get(i);
            singleParsed = "Name:" + JO.get("name") + "\n" +
                           "Description:" + JO.get("description") + "\n" +
                           "Price:" + JO.get("price") + "\n" +
                           "Currency:" + JO.get("cost_currency") + "\n" ;

            dataParsed  = dataParsed+singleParsed + "\n";

        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    MainActivity.tv_data.setText(this.dataParsed);
}
}


What I have tried:

I'am trying to fetch details of products from a URL . However, there is a json exception when trying to parse the JSON objects.
Posted
Updated 13-Apr-18 20:19pm
v3
Comments
Richard MacCutchan 14-Apr-18 3:58am    
You need to capture the text that causes the exception.
Sukhi_15 16-Apr-18 1:57am    
yup.. thanks.. i got it solved.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900