-1

AndroidでJSONフォームのURLを解析するには?

例えば:

ボタンをクリックしてサーバーからデータをダウンロードし、画面に表示します。

JSON コード:

[{"id":1, "name":"ABC"},{"id":2,"name":"ABC"}]

また

{"response":[{"id":1,"name":"ABC","surname":"ABC"}]}

4

4 に答える 4

1
private void Get_List() {
            // TODO Auto-generated method stub
CountDownTimer count_Timer;
            try {
                count_Timer.start();
                SetUrl url = new SetUrl();
                URL myurl = new URL(//"Your URL");
                HttpURLConnection connection = (HttpURLConnection) myurl
                        .openConnection();
                connection.connect();
                int code = connection.getResponseCode();

                if (code != 200) {
                    setContentView(R.layout.webimg);
                    TextView tv_nointernet = (TextView) findViewById(R.id.tv_nointernet);
                    tv_nointernet.setText("Please turn your internet on. . .");
                } else {

                    BufferedReader dis = new BufferedReader(
                            new InputStreamReader(connection.getInputStream()));

                    String myString, Result = dis.readLine();

                    while ((myString = dis.readLine()) != null) {
                        Result += myString;
                    }
                    JSONObject jsonObject = new JSONObject(Result);
                    JSONArray jsonArray = jsonObject.getJSONArray("posts");

                    Rest_List = new ArrayList<Rest_Listing>();

                    for (int i = 0; i < jsonArray.length(); i++) {
                        Rest_Listing R_List = new Rest_Listing();

                        JSONObject O = jsonArray.getJSONObject(i);

                        R_List.setRest_Address1(O.getJSONObject("post")
                                .getString("Name"));


                        Rest_List.add(i, R_List);
                    }
                    count_Timer.cancel();
                }
            } catch (MalformedURLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
于 2014-11-09T05:03:02.527 に答える
1

URL からデータを取得するには、android-async-http-1.4.1.jar ( http://loopj.com/android-async-http/からダウンロードできます) を使用し、libs フォルダーに入れてから使用します。 json 構造が "{"response":[{"id":1,"name":"ABC","surname":"ABC"}]}":

AsyncHttpClient クライアント = new AsyncHttpClient();

    client.post(Your_URL_String, new AsyncHttpResponseHandler() {
        @Override
        public void onSuccess(String MyResponse) {
            JSONObject json_main;
            try {
                JSONObject json_main = new JSONObject(MyResponse);  
                JSONArray json_arr = json_main.getJSONArray("response"); 
                        for(int i = 0; i < json_arr.length(); i++)
                        {
                    JSONObject c = json_arr.getJSONObject(i);
                    String ID = c.getString("id");
                    String name = c.getString("name");
                    String surname = c.getString("surname");
                }
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        @Override
        public void onFailure(Throwable arg0, String arg1) {
            // TODO Auto-generated method stub
            super.onFailure(arg0, arg1);

        }
    });
于 2013-08-19T10:35:03.920 に答える
0
Use the following it will be helpful

public void JSONPARSE()
   {
    JSONObject jObj=null;
    JSONArray Jarray=null;
    InputStream is=null;
    String URL ="url";
    System.out.println("verify url is----> "+URL);
    //Making HTTP request
    try {
        // defaultHttpClient
            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 = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            String result = sb.toString();
            System.out.println("json RegistrationResult is----> "+result);
            try {
                    jObj = new JSONObject(result);
                } catch (JSONException e) {
                    Log.e("JSON Parser", "Error parsing data " + e.toString());
                }
            try {
                        Jarray = jObj.getJSONArray("response");
                        for(int i = 0; i < Jarray.length(); i++)
                        {
                            JSONObject c = Jarray.getJSONObject(i);
                            String id = c.getString("id");
                            x.add(id);    //x is a vector
                           String name =c.getString("name");
                           n.add(name); //n is a vector
                          String surname =c.getString("surname");
                           s.add(surname); //s is a vector
                        }
                 } catch (JSONException e) {
                     e.printStackTrace();
                 }

        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
    }
于 2013-08-19T10:11:55.533 に答える
0

これを行う方法はたくさんあります。このライブラリは非常に役に立ち、時間を節約できることがわかりました: Gson Library .

ここからユーザーガイドにアクセスできます: Gson ユーザーガイド

于 2013-08-19T10:14:43.660 に答える