1

Before, i should say, my English is very bad. İm sorry for it. i parsing wanna Json output, in my android prject. my json file is with url. it is like there

{
  "urun": [
    {
      "ID": "1011245",
      "name": "Jeanne Darc-Elbise-jdje57942xl",
      "name_eng": "Jeanne Darc-Dress-jdje57942xl",
      "catID": "142",
      "tedarikciCode": "jdje57942xl",
      "markaID": "30",
      "data1": "4",
      "resim": "var/30/jdje57942xl/siyah_1_jdje57942xl.jpg",
      "resim2": "var/30/jdje57942xl/siyah_2_jdje57942xl.jpg",
      "resim3": "var/30/jdje57942xl/siyah_3_jdje57942xl.jpg",
      "fiyat": "28",
      "ozellik1detay": "44-50"
    }
] 
}

my parser class is

    public class JsonParsers
{
final String TAG = "JsonParsers.java";
static InputStream is  =null;
    static JSONObject jObj=null;
static String ParserJson=null;

    public JsonParsers(String yourJsonStringUrl) {
    }

    public JsonParsers() {
        super();
    }

    public String getJsonUrl(String url) throws IOException {
        try{
            DefaultHttpClient httpClient=new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity=httpResponse.getEntity();
            is=httpEntity.getContent();
        }
        catch (UnsupportedEncodingException e)
        {
            e.printStackTrace();
        }
        catch (ClientProtocolException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            StringBuilder sb= new StringBuilder();
            String line = "";
            while ((line=reader.readLine())!=null)
            {

                sb.append(line+"\n");
                //Log.e("çıktı:",line);
            }
            is.close();
            ParserJson = sb.toString();


        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        try {
            jObj=new JSONObject(ParserJson);

        }
        catch (JSONException e) {
            e.printStackTrace();
        }

        return ParserJson;


    }


}

My asyncTask Class is

 private class AsyncTaskParseJSonIncludes extends AsyncTask<String, String, String> {
    final String TAG = "MainActivity.java";

    JSONArray dataJsonArr = null;
    String ObjectStr;






    protected String doInBackground(String... path)  {
       try{

        try{

            JsonParsers parser = new JsonParsers();

            //Json = parser.getJsonUrl(JsonPath);
            ObjectStr=parser.getJsonUrl(JsonPath);
            JSONObject Json= new JSONObject(ObjectStr);
            dataJsonArr=Json.getJSONArray("urun");

            for(int i=0;i<dataJsonArr.length();i++)
            {
                 JSONObject c = dataJsonArr.getJSONObject(i);
                // Log.e("Deneme", c.getString("name"));
                ID.add(Integer.valueOf(c.getString("ID")));
                name.add(c.getString("name"));
                name_eng.add(c.getString("name_eng"));
                //name_py.add(c.getString("name_py"));
                CatID.add(Integer.valueOf(c.getString("CatID")));
                tedarikciCode.add(c.getString("tedarikciCode"));
                 markaID.add(Integer.valueOf(c.getString("markaID")));
                data1.add(Integer.valueOf(c.getString("data1")));
                resimmmm.add(c.getString("resim"));
                resim2.add(c.getString("resim2"));
                resim3.add(c.getString("resim3"));
                fiyat.add(Integer.valueOf(c.getString("fiyat")));
                ozellik1detay.add(c.getString("ozellik1detay"));
              //  ozellik2detay.add(c.getString("ozellik2detay"));




            }
          for(int i=0;i<name.size();i++) {
                Log.e("Deneme", name.get(i));
            }


        }
        catch (JSONException e)
        {
            e.printStackTrace();
        }
       }
        catch (IOException e)
        {
            e.printStackTrace();
        }

        return null;
    }
    protected void onPreExecute() {}
/////////////////////////////////////////////////////////


}

it is in normaly working but it is not returned json values. i check the parser class for stream of data, it is fixed. But it is only first value returned, other datas don't returned. i don't understand it logical problem. when someone help me for fix my code i very funny. Thanks.

Sagar Zala
  • 4,854
  • 9
  • 34
  • 62
Ksm.Ylmz
  • 13
  • 3

2 Answers2

0
name_eng.add().

where was the name_eng defined?

CatID to catID.

right:

Try{A}catch{}
Try{B}catch{}
Try{C}catch{}

wrong:

Try{A;B;C}catch{}
tiny sunlight
  • 6,231
  • 3
  • 21
  • 42
  • it definded in main activity, Async Task class is too – Ksm.Ylmz Nov 06 '15 at 13:24
  • What msg did you get form try catch? – tiny sunlight Nov 06 '15 at 13:30
  • 11-06 15:03:50.268 2251-2266/com.example.ddfs.dddddd E/Deneme﹕ Jeanne Darc-Elbise-jdje57942xl 11-06 15:03:50.269 2251-2266/com.example.ddfs.dddddd W/System.err﹕ org.json.JSONException: No value for CatID 11-06 15:03:50.269 2251-2266/com.example.ddfs.dddddd W/System.err﹕ at org.json.JSONObject.get(JSONObject.java:389) 11-06 15:03:50.269 2251-2266/com.example.ddfs.dddddd W/System.err﹕ at org.json.JSONObject.getString(JSONObject.java:550) – Ksm.Ylmz Nov 06 '15 at 14:06
  • 11-06 15:03:50.269 2251-2266/com.example.ddfs.dddddd W/System.err﹕ at com.example.ddfs.dddddd.MainActivity$AsyncTaskParseJSonIncludes.doInBackground(MainActivity.java:93) 11-06 15:03:50.269 2251-2266/com.example.ddfs.dddddd W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:288) 11-06 15:03:50.269 2251-2266/com.example.ddfs.dddddd W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237) 11-06 15:03:50.269 2251-2266/com.example.ddfs.dddddd W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) – Ksm.Ylmz Nov 06 '15 at 14:06
  • 11-06 15:03:50.269 2251-2266/com.example.ddfs.dddddd W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 11-06 15:03:50.269 2251-2266/com.example.ddfs.dddddd W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 11-06 15:03:50.269 2251-2266/com.example.ddfs.dddddd W/System.err﹕ at java.lang.Thread.run(Thread.java:818) – Ksm.Ylmz Nov 06 '15 at 14:06
  • CatID to catID!!No value for CatID. – tiny sunlight Nov 06 '15 at 14:09
  • Ok, Perfect, how i can't this basic problem. Thank you soo much – Ksm.Ylmz Nov 06 '15 at 14:17
0

Try GsonRequest, it's very simple to parse JSON, nice example of use it you have there: USE GsonRequest

Community
  • 1
  • 1
Szamot
  • 366
  • 2
  • 12