views:

434

answers:

2

I have the following code in a form's submit button onClickListener:

String action, user, pwd, user_field, pwd_field;

        action = "theURL";

        user_field = "id";
        pwd_field = "pw";
        user = "username";
        pwd = "password!!";

        List<NameValuePair> myList = new ArrayList<NameValuePair>();
        myList.add(new BasicNameValuePair(user_field, user)); 
        myList.add(new BasicNameValuePair(pwd_field, pwd));

        HttpParams params = new BasicHttpParams();
        HttpClient client = new DefaultHttpClient(params);
        HttpPost post = new HttpPost(action);
        HttpResponse end = null;
        String endResult = null;

        try {
            post.setEntity(new UrlEncodedFormEntity(myList));
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 

        try {
            HttpResponse response = client.execute(post);
            end = response;
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  


        BasicResponseHandler myHandler = new BasicResponseHandler();

        try {
            endResult = myHandler.handleResponse(end);
        } catch (HttpResponseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

How can I take the resulting string (endResult) and start a new activity using an intent that will open webview and load the html?

+2  A: 

You can start a new intent with

Intent myWebViewIntent = new Intent(context, MyWebViewActivity.class);
myWebViewIntent.putExtra('htmlString', endResult);
context.startActivity(myWebViewIntent);

Then in your MyWebViewActivity class you would have something like:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.my_view_that_contains_a_webview);
    WebView webview = (WebView)findViewById(R.id.my_webview);

    Bundle extras = getIntent().getExtras();
    if(extras != null) {

         // Get endResult
         String htmlString = extras.getString('htmlString', '');
         webview.loadData(htmlString, "text/html", "utf-8");

    }
}
disretrospect
This is exactly what I was looking for, thanks!questions: what does context refer to? (I'm new at this)What type of activity does the new class need to extend?Not sure if it matters, but it won't allow single quotes around htmlstringLastly, the extras.getstring won't accept 2 string params, only one?
datguywhowanders
Answers to my own questions:The context needs to be set to NameOfMainAppClass.this.The second class (the one with the webview) extends just Activity.The single/double quotes doesn't matter, and leaving off the second paramater to getString() doesn't affect the result.Although, as I found in another StackOverflow post, loadData() doesn't work well with straight up html, and I had better results using the full method loadDataWithBaseURL().
datguywhowanders
Cool, glad you got it sorted!
disretrospect
Now I'm facing a new issue unfortunately... the webview doesn't seem to retain the login information when I click on a link from that first page.
datguywhowanders
I thought it was because when you clicked on another link, it would open the default browser instead of in the webview, but I fixed that by implementing a webviewclient, and it still loses the session info. Any ideas?
datguywhowanders
A: 

The resulting code after applying the above answer is as follows:

Intent myWebViewIntent = new Intent(YourAppClassHere.this, YourWebViewClassHere.class);
myWebViewIntent.putExtra("htmlString", theStringThatHoldsTheHTML);
startActivity(myWebViewIntent);

The full code for a basic webview class that I used was:

public class MyWebView extends android.app.Activity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.web);

    WebView webview = (WebView)findViewById(R.id.mainwebview);

    Bundle extras = getIntent().getExtras();
    if(extras != null) {

         // Get endResult
         String htmlString = extras.getString("htmlString");
         webview.loadDataWithBaseURL(null, htmlString, "text/html", "utf-8", null);
    }

   }
}

It's also worth noting that this, for me anyway, crashed the program every time until I added the following lines to the AndroidManifest.xml:

<activity android:name=".MyWebView">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Hopefully that'll help somebody else out in the future :) Thanks to disretrospect.

datguywhowanders