简体   繁体   中英

Json parsing as logical workout

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.

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{}

尝试使用GsonRequest,解析JSON非常简单,您可以在其中很好地使用它: USE GsonRequest

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM