Note that there are some explanatory texts on larger screens.

plurals
  1. POReading data value from a webpage
    primarykey
    data
    text
    <p>I have an asp page that creates a text file on the server with only one value, I would like my Android app to be able to read that one value from the server rather than download the file like I have it do now..</p> <pre><code>package com.isusa.sdsmobile; imports; public class SDSMobileActivity extends Activity { WebView mWebView; String title = "SDS Mobile - "; String firstrun = "yes"; public static final String PREFS_NAME = "MyPrefsFile"; Spinner schoolspin; String web; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.main); mWebView = (WebView) findViewById(R.id.wview); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.setWebViewClient(new HelloWebViewClient()); WebSettings webSettings = mWebView.getSettings(); webSettings.setJavaScriptEnabled(true); mWebView.setVisibility(View.VISIBLE); Spinner spinner = (Spinner) findViewById(R.id.spinner123); ArrayAdapter&lt;CharSequence&gt; adapter = ArrayAdapter.createFromResource( this, R.array.school_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new MyOnItemSelectedListener()); final Activity activity = this; mWebView.setWebChromeClient(new WebChromeClient() { @Override public void onProgressChanged(WebView view, int progress) { activity.setProgress(progress * 100); }}); mWebView.setWebViewClient(new WebViewClient()); this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); final EditText usernameBox = (EditText) findViewById(R.id.usernameBox); final EditText passwordBox = (EditText) findViewById(R.id.passwordBox); String password = "123"; String username = "000"; int schoolsel; username = settings.getString("Username", username); password = settings.getString("Password", password); firstrun = settings.getString("firstrun", "yes"); web = settings.getString("website", "http://"); schoolsel = settings.getInt("school", 0); usernameBox.setText(username); passwordBox.setText(password); schoolspin = (Spinner) findViewById(R.id.spinner123); schoolspin.setSelection(schoolsel); init(); } private void init() { if(firstrun.equals("yes")) { mWebView.loadUrl("http://#.asp"); } else { mWebView.loadUrl("http://#"); } } public class MyOnItemSelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView&lt;?&gt; parent, View view, int pos, long id) { if( parent.getItemAtPosition(pos).toString().equals("SDS Live Demo") ) { setTitle(title + "SDS Live Demo"); web = "http://##"; } else if( parent.getItemAtPosition(pos).toString().equals("# Community School District") ) { setTitle(title + "## Community School District"); web = "http://lunch.##.us"; } public void onNothingSelected(AdapterView&lt;?&gt; parent) { // Do nothing. } } public void schoolselect(View view) { String[] items = new String[] {"One", "Two", "Three"}; ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_spinner_dropdown_item, items); new AlertDialog.Builder(this) .setTitle("the prompt") .setAdapter(adapter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // TODO: user specific action. dialog.dismiss(); } }).create().show(); } public void test1(View view) { final int NOTIF_ID = 1234; NotificationManager notifManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); Notification note = new Notification(R.drawable.ic_launcher, "New E-mail", System.currentTimeMillis()); PendingIntent intent = PendingIntent.getActivity(this, 0, new Intent(this, SDSMobileActivity.class), 0); note.setLatestEventInfo(this, "Low Balance Warning!", "You have a balance of: ", intent); notifManager.notify(NOTIF_ID, note); //notifManager.cancel(NOTIF_ID); //web + "/mobile/" + usernameBox.getText() + "_balance.txt"; //Get the .txt final EditText usernameBox = (EditText) findViewById(R.id.usernameBox); String url = web + "/mobile/" + usernameBox.getText() + "_balance.txt"; DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url)); request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, usernameBox.getText() + "_balance.txt"); // get download service and enqueue file DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE); manager.enqueue(request); // Finally read from .txt String sdcard = Environment.getExternalStorageDirectory() + "/download"; System.out.println(sdcard); //Get the text file File file = new File(sdcard, usernameBox.getText() + "_balance.txt"); //Read text from file StringBuilder text = new StringBuilder(); try { BufferedReader br = new BufferedReader(new FileReader(file)); String line; while ((line = br.readLine()) != null) { text.append(line); text.append('\n'); } } catch (IOException e) { //You'll need to add proper error handling here } //Set the text setTitle(text); System.out.println(text); File path = Environment.getExternalStorageDirectory(); File file1 = new File(path, "/download/" + usernameBox.getText() + "_balance.txt"); System.out.println(file1); file1.delete(); } public void home(View view) { mWebView.setVisibility(View.VISIBLE); final EditText usernameBox = (EditText) findViewById(R.id.usernameBox); final EditText passwordBox = (EditText) findViewById(R.id.passwordBox); String postData = "T1="+usernameBox.getText()+"&amp;T2="+passwordBox.getText(); mWebView.postUrl(web + "/authenticate_m.asp", EncodingUtils.getBytes(postData, "utf-8")); } public void news(View view) { mWebView.setVisibility(View.VISIBLE); mWebView.loadUrl("http://##/"); } public void funds(View view) { mWebView.setVisibility(View.VISIBLE); final EditText usernameBox = (EditText) findViewById(R.id.usernameBox); final EditText passwordBox = (EditText) findViewById(R.id.passwordBox); String postData = "T1="+usernameBox.getText()+"&amp;T2="+passwordBox.getText(); mWebView.postUrl(web + "/authenticate_m.asp", EncodingUtils.getBytes(postData, "utf-8")); mWebView.loadUrl(web + "/funds_m.asp"); } public void account(View view) { mWebView.setVisibility(View.VISIBLE); final EditText usernameBox = (EditText) findViewById(R.id.usernameBox); final EditText passwordBox = (EditText) findViewById(R.id.passwordBox); String postData = "T1="+usernameBox.getText()+"&amp;T2="+passwordBox.getText(); mWebView.postUrl(web + "/authenticate_m.asp", EncodingUtils.getBytes(postData, "utf-8")); mWebView.loadUrl(web + "/studentinfo_m.asp?SID=" + usernameBox.getText().toString()); } public void purhis(View view) { mWebView.setVisibility(View.VISIBLE); final EditText usernameBox = (EditText) findViewById(R.id.usernameBox); final EditText passwordBox = (EditText) findViewById(R.id.passwordBox); String postData = "T1="+usernameBox.getText()+"&amp;T2="+passwordBox.getText(); mWebView.postUrl(web + "/authenticate_m.asp", EncodingUtils.getBytes(postData, "utf-8")); mWebView.loadUrl(web + "/studentactivity_m.asp?SID=" + usernameBox.getText().toString()); } public void exit(View view) { finish(); } public void settings(View view) { mWebView.setVisibility(View.GONE); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK) &amp;&amp; mWebView.canGoBack()) { mWebView.goBack(); return true; } return super.onKeyDown(keyCode, event); } private class HelloWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } } public void save(View view) { final EditText usernameBox = (EditText) findViewById(R.id.usernameBox); final EditText passwordBox = (EditText) findViewById(R.id.passwordBox); String user = usernameBox.getText().toString(); String pass = passwordBox.getText().toString(); schoolspin = (Spinner) findViewById(R.id.spinner123); int schoolsel = schoolspin.getSelectedItemPosition(); SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0); SharedPreferences.Editor editor = settings.edit(); editor.putString("Username", user); editor.putString("Password", pass); editor.putString("firstrun", "no"); editor.putString("website", "web"); editor.putInt("school", schoolsel); editor.commit(); Toast.makeText(this, "Settings Saved!", Toast.LENGTH_SHORT).show(); } } </code></pre> <p>So my "public void test1(View view)" ignites on a button click.. what it does now is download the txt file from the server and then read it, which isn't reliable because it takes a few seconds to download (and creates clutter) I would like to read the .txt or even the asp output from the server itself, and I've tried code that would do that but I could never get it to work properly.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload