使用HttpURLConnection请求JSON文档并解析它

问题描述 投票:1回答:1

我有一个应用程序,其中一个类负责从本地服务器请求JSON ...

这是主要活动:

public class Activity_main extends AppCompatActivity {

    private ArrayList<StructAcount> netAcount = new ArrayList<StructAcount>();
    private TextView textView;


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

        textView = (TextView) findViewById(R.id.txtJSON);

        WebService.readUrl("http://localhost/match_picture/service.php?action=read");

        String result = WebService.getResult();


        if (result != null) {
            try {
                JSONArray tasks = new JSONArray(result);
                for (int i=0; i<tasks.length(); i++) {
                    StructAcount acount= new StructAcount();
                    JSONObject object = tasks.getJSONObject(i);
                    acount.id = object.getLong("user_id");
                    acount.name = object.getString("user_name");
                    acount.email = object.getString("user_email");

                    netAcount.add(acount);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Toast.makeText(Activity_main.this, "hsh" , Toast.LENGTH_SHORT).show();
        }

        for (StructAcount acount: netAcount) {
            textView.setText(textView.getText() + "username: " + acount.name + "  |  " + "useremail: " + acount.email + "\n");
            Toast.makeText(Activity_main.this, "username: " + acount.name + "\n" + "useremail: " + acount.email , Toast.LENGTH_SHORT).show();
        }
    }

}

...这里是HTTP请求的类:

public class WebService {

    private static String result;

    public static void readUrl(String server_url) {
       class GetJson extends AsyncTask<String, Void, String> {
           @Override
           protected String doInBackground(String... params) {
               BufferedReader bufferedReader = null;
               String uri = params[0];
               try {
                   URL url = new URL(uri);
                   HttpURLConnection con = (HttpURLConnection) url.openConnection();
                   StringBuilder sb = new StringBuilder();
                   InputStream in = new BufferedInputStream(con.getInputStream());
                   bufferedReader = new BufferedReader(new InputStreamReader(in));

                   String json;
                   while ((json = bufferedReader.readLine()) != null) {
                       sb.append(json);

                   }

                   return sb.toString().trim();


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

               return null;

           }

           @Override
           protected void onPostExecute(String s) {
               super.onPostExecute(s);
               WebService.set_result(s);
           }
       }

        GetJson gj = new GetJson();
        gj.execute(server_url);
    }

    public static void set_result(String s) {
        result = s;
    }

    public static String getResult() {
        return result;
    }

}

我第一次运行App,没有设置TextView。

当应用程序仍然在模拟器的屏幕上时:如果我做了一个小的更改,例如,更改Toast消息的文本,我再次运行应用程序和TextView的文本由JSON值设置...

任何人都可以为这种情况指出一些参考或解决方案吗?

android json android-activity httpurlconnection
1个回答
0
投票

出于研究目的,HttpUrlConnection可能有用。

对于现实生活,Google建议使用Android Volley库。

一些有用的链接:

对于使用带有HTTPS的Android Volley,不需要核对SSL证书(请不要)。

为什么?答案是here

© www.soinside.com 2019 - 2024. All rights reserved.