【问题标题】:Display And Use Information From A DownloadTask In Android Studio在 Android Studio 中显示和使用来自 DownloadTask 的信息
【发布时间】:2016-12-22 00:41:38
【问题描述】:

第一次以任何大容量使用 Android Studio。使用这里的代码:https://stackoverflow.com/a/30937657/5919360,我能够成功地从 URL 中提取我想要的信息,但我不知道如何使用它。

注意:我知道 IMEI 不是检查用户注册的好方法,以后会更改它。

public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // Create instance and populates based on content view ID
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        // store IMEI
        String imei = tm.getDeviceId();
        // store phone
        String phone = tm.getLine1Number();

        // Display IMEI - Testing Purposes Only
        TextView imeiText = (TextView) findViewById(R.id.imeiDisplay);
        imeiText.setText("IMEI:" + imei);
        // Display phone number - Testing Purposes Only
        TextView phoneText = (TextView) findViewById(R.id.phoneDisplay);
        phoneText.setText("Phone:" + phone);

        new DownloadTask().execute("http://www.url.com/mobileAPI.php?action=retrieve_user_info&IMEI="+imei);

    }

    private class DownloadTask extends AsyncTask<String, Void, String> {

        @Override
        protected String doInBackground(String... params) {
            try {
                return downloadContent(params[0]);
            } catch (IOException e) {
                return "Unable to retrieve data. URL may be invalid.";
            }
        }

        @Override
        protected void onPostExecute(String result) {

            Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();

        }
    }

    private String downloadContent(String myurl) throws IOException {
        InputStream is = null;
        int length = 500;

        try {
            URL url = new URL(myurl);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setReadTimeout(10000 /* milliseconds */);
            conn.setConnectTimeout(15000 /* milliseconds */);
            conn.setRequestMethod("GET");
            conn.setDoInput(true);
            conn.connect();
            int response = conn.getResponseCode();
            Log.d(TAG, "The response is: " + response);
            is = conn.getInputStream();

            // Convert the InputStream into a string
            String contentAsString = convertInputStreamToString(is, length);
            return contentAsString;
        } finally {
            if (is != null) {
                is.close();
            }
        }
    }

    public String convertInputStreamToString(InputStream stream, int length) throws IOException, UnsupportedEncodingException {
        Reader reader = null;
        reader = new InputStreamReader(stream, "UTF-8");
        char[] buffer = new char[length];
        reader.read(buffer);
        return new String(buffer);
    }
}

此代码以 toast 形式返回一个 xml 文件:

<?xml version="1.0" encoding="ISO-8859-1"?>
<mobile_user_info>
<rec>45</rec>
<IMEI>9900990099009</IMEI>
<fname>First</fname>
<lname>Last</lname>
<instance>instance1</instance>
<registered>N</registered>
</mobile_user_info>

我希望有人能指出正确的方向来分隔每条线并独立使用它。例如,如果 Registered 行返回为 N,则会显示一条消息,例如“您尚未注册。请联系管理员。

【问题讨论】:

    标签: java android android-studio http-get


    【解决方案1】:

    其实你应该使用 XML 解析器来解析服务器的响应。但是,如果响应总是像您的示例一样简单,您可以使用正则表达式来提取 IMEI 字段。

    String contentAsString = ...
    Pattern pattern = Pattern.compile("<IMEI>(\d*)</IMEI>");
    Matcher matcher = pattern.matcher(contentAsString);
    if (matcher.find()) {
        String imei = matcher.group(1);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-15
      • 1970-01-01
      • 2019-09-21
      • 2023-01-23
      • 2021-04-06
      相关资源
      最近更新 更多