2016-08-15 93 views
0

第一次在任何大容量中使用Android Studio。使用這裏的代碼:https://stackoverflow.com/a/30937657/5919360,我能夠成功地從URL中獲取我想要的信息,但我無法弄清楚如何使用它。在Android Studio中顯示和使用來自DownloadTask的信息

注意:我知道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文件,祝酒:

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

實際上,您應該使用XML解析器來解析服務器的響應。但是,如果響應總是與您的示例一樣簡單,則可以使用正則表達式來提取IMEI字段。

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