在Android Studio中显示和使用来自DownloadTask的信息

时间:2016-08-15 19:04:28

标签: java android android-studio http-get

首次在任何大容量中使用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);
    }
}

此代码返回一个xml文件,作为toast:

<?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>

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

0 个答案:

没有答案