收到图片时应用程序崩溃

时间:2019-07-06 01:33:48

标签: android multithreading http steam steam-web-api

当接收到图片(Steam配置文件背景)时,应用程序崩溃。有一个用于接收和替换图像的类。

代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_add_account_steam, container, false);

    steam_id = view.findViewById(R.id.steam_id);
    steam_id_set = view.findViewById(R.id.steam_id_set);
    steam_id_get = view.findViewById(R.id.steam_id_get);
    imageView = view.findViewById(R.id.imageView);

    steam = null;
    steam_id_64 = null;

    mQueue = Volley.newRequestQueue(getActivity());
    idQueue = Volley.newRequestQueue(getActivity());

    steam_id.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view){
            steam_id_64 = steam_id_get.getText().toString();
            if (steam_id_64.matches("[0-9]{17}")) {
                jsonParse();
            }else
            {
                parse_steamid_64();

            }
            try {
                getBackgroundURL();
            } catch (IOException e){
                Toast.makeText(getActivity(), "da", Toast.LENGTH_SHORT).show();
            }
        }
    });

    return view;
}
public void getBackgroundURL() throws IOException {
        String profile_response = getText("https://steamcommunity.com/id/kojimaone/");

        Pattern p = Pattern.compile("https://steamcdn-a[.]akamaihd[.]net/steamcommunity/public/images/items/[0-9]{6}/[0-9a-z]{40}[.]jpg");
        Matcher m = p.matcher(profile_response);

        // if an occurrence if a pattern was found in a given string...
        if (m.find()) {
            background_url = m.group(0);
            Glide.with(getActivity())
                    .load(background_url)
                    .into(imageView);
        }
    }
public String getText(String url) throws IOException {
    URL url2 = new URL(url);
    URLConnection con = url2.openConnection();
    InputStream in = con.getInputStream();
    String encoding = con.getContentEncoding();
    encoding = encoding == null ? "UTF-8" : encoding;
    BufferedReader r = new BufferedReader(new InputStreamReader(in));
    StringBuilder total = new StringBuilder();
    for (String line; (line = r.readLine()) != null; ) {
        total.append(line).append('\n');
    }
    String body = total.toString();
    return body;
}

错误

2019-07-06 11:19:50.705 19127-19127/com.example.steamaccountmanager E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.steamaccountmanager, PID: 19127
android.os.NetworkOnMainThreadException
    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1513)
    at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:117)
    at java.net.Inet6AddressImpl.lookupAllHostAddr(Inet6AddressImpl.java:105)
    at java.net.InetAddress.getAllByName(InetAddress.java:1154)
    at com.android.okhttp.Dns$1.lookup(Dns.java:39)
    at com.android.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
    at com.android.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
    at com.android.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
    at com.android.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
    at com.android.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
    at com.android.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
    at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
    at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:461)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:407)
    at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:244)
    at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getInputStream(DelegatingHttpsURLConnection.java:210)
    at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:26)
    at com.example.steamaccountmanager.AddAccountSteam.getText(AddAccountSteam.java:240)
    at com.example.steamaccountmanager.AddAccountSteam.getBackgroundURL(AddAccountSteam.java:254)
    at com.example.steamaccountmanager.AddAccountSteam$1.onClick(AddAccountSteam.java:94)
    at android.view.View.performClick(View.java:6597)
    at android.view.View.performClickInternal(View.java:6574)
    at android.view.View.access$3100(View.java:778)
    at android.view.View$PerformClick.run(View.java:25885)
    at android.os.Handler.handleCallback(Handler.java:873)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

请帮助我解决这个问题。我尝试做很多事情,但没有任何结果。我不明白为什么我的应用程序崩溃以及如何处理此问题。

应用程序必须通过按键获取SteamID,并接收背景和帐户信息。问题在收到图片时出现。

1 个答案:

答案 0 :(得分:0)

您的应用程序崩溃是因为您正在主线程中执行HTTPS请求。

以下方法应在后台线程中执行:

String profile_response = getText("https://steamcommunity.com/id/kojimaone/");

使用Volley时,可以使用Volley在后台线程中执行请求。

这里有一个简单的请求示例: https://developer.android.com/training/volley/simple.html