1

I am android developer.i make a app using webview to load url salefore login page in webpage i want on webpage click on login button go to activity and get token and url.plz help me..

My activity is

public class MainActivity extends AppCompatActivity {
    private WebView webView;
    Handler mHandler = new Handler();
    WebView.HitTestResult hr;
    Element elementById;
    String u, text;
    String newUrl;
    @Override
    @SuppressLint("JavascriptInterface")
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        webView = (WebView) findViewById(R.id.webView1);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new HelloWebViewClient(this));
        webView.loadUrl("https://************/API/oauth.php?community=**************&validate-header=***************0d7yuhggte543bdvsfrwwjskdga5d454e67e");
    }
    @Override
    // Detect when the back button is pressed
    public void onBackPressed() {
        if (webView.canGoBack()) {
            webView.goBack();
        } else {
            // Let the system handle the back button
            super.onBackPressed();
        }
    }

    private class HelloWebViewClient extends WebViewClient {
        Activity act;

        public HelloWebViewClient(Activity myAct) {
            act = myAct;
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            Log.d("Sfdc-sample", "Redirect URL: " + url);
            Log.i("url is====", url);

            //            Toast.makeText(getApplicationContext(),"url"+url,Toast.LENGTH_LONG).show();
            if (url.startsWith("https://************/API/oauth.php")) {
                u = url;
                try {
                    newUrl = URLDecoder.decode(url, "UTF-8");
                } catch (UnsupportedEncodingException e) {}
                (new ParseURL()).execute(new String[] {
                    url
                });
                /*parser parser = new parser();
                parser.execute();*/
                view.loadUrl(u);
                /* Intent i = new Intent(act, Main2Activity.class);
                  startActivity(i);
                  finish();*/
                return true;
            } else {

            }
            return false;
        }
    }
    class parser extends AsyncTask < String, Void, String > {
        String url1;
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(String...strings) {

            url1 = getRequest(newUrl);
            return null;
        }
        @Override
        protected void onPostExecute(String s) {

            Toast.makeText(getApplicationContext(), "sucess url" + url1, Toast.LENGTH_LONG).show();
            super.onPostExecute(s);
        }
    }
    @SuppressWarnings("deprecation")
    public static String getRequest(String url) {

        HttpParams httpParameters = new BasicHttpParams();
        int timeoutConnection = 50000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        int timeoutSocket = 50000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        HttpClient httpclient = new DefaultHttpClient(httpParameters);

        String result = null;
        // Prepare a request object
        HttpGet httpget = new HttpGet(url);
        // Execute the request
        HttpResponse response;
        try {
            response = httpclient.execute(httpget);
            //Examine the response status
            //Log.i("Praeda",response.getStatusLine().toString());
            //Get hold of the response entity
            HttpEntity entity = response.getEntity();
            // If the response does not enclose an entity, there is no need
            // to worry about connection release
            if (entity != null) {
                // A Simple JSON Response Read
                InputStream instream = entity.getContent();
                result = convertStreamToString(instream);
                // now you have the string representation of the HTML request
                instream.close();
            }
        } catch (Exception e) {
            Log.i("Connection Error", e.getMessage());
        }

        return result;
    }
    private static String convertStreamToString(InputStream is) {
        ByteArrayOutputStream oas = new ByteArrayOutputStream();
        copyStream(is, oas);
        String t = oas.toString();
        try {
            oas.close();
            oas = null;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return t;
    }
    private static void copyStream(InputStream is, OutputStream os) {
        final int buffer_size = 1024;
        try {
            byte[] bytes = new byte[buffer_size];
            for (;;) {
                int count = is.read(bytes, 0, buffer_size);
                if (count == -1)
                    break;
                os.write(bytes, 0, count);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
4
  • Can you please take a look into this tutorial developer.salesforce.com/…
    – Ratan Paul
    May 23, 2016 at 10:34
  • i get error code Error: call to token URL /services/oauth2/token failed with status 0, response , curl_error May 23, 2016 at 11:00
  • Please Help me?? May 23, 2016 at 11:11
  • sorry i don't have any experience with android. I suggest ask this question in stackoverflow.com as well hope you will get help.
    – Ratan Paul
    May 23, 2016 at 11:19

1 Answer 1

2

By default, any Android app built using the Salesforce1 Android SDK uses a webview for login. I would recommend you install the SDK and look at one of the sample apps e.g. RestExplorer and post another thread if you have any issues.

The error posted above indicates that your request to get an access token is failing. The advantage with SDK is that it abstracts any such details for you and provides a simple wrapper method that you can use to login.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .