Search in sources :

Example 91 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project newsrob by marianokamp.

the class GRAnsweredBadRequestException method authenticate.

boolean authenticate(Context context, String email, String password, String captchaToken, String captchaAnswer) throws ClientProtocolException, IOException, AuthenticationFailedException {
    getEntryManager().getNewsRobNotificationManager().cancelSyncProblemNotification();
    NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
    try {
        // ?client="
        HttpPost authenticateRequest = new HttpPost("https://www.google.com/accounts/ClientLogin");
        // +
        // CLIENT_NAME);
        List<NameValuePair> keyValuePairs = new ArrayList<NameValuePair>();
        // HOSTED_OR_GOOGLE
        keyValuePairs.add(new BasicNameValuePair("accountType", "GOOGLE"));
        keyValuePairs.add(new BasicNameValuePair("Email", email));
        keyValuePairs.add(new BasicNameValuePair("Passwd", password));
        keyValuePairs.add(new BasicNameValuePair("source", CLIENT_NAME));
        keyValuePairs.add(new BasicNameValuePair("service", "reader"));
        if (captchaToken != null) {
            keyValuePairs.add(new BasicNameValuePair("logintoken", captchaToken));
            keyValuePairs.add(new BasicNameValuePair("logincaptcha", captchaAnswer));
        }
        authenticateRequest.setEntity(new UrlEncodedFormEntity(keyValuePairs, HTTP.UTF_8));
        HttpResponse response = httpClient.execute(authenticateRequest);
        int statusCode = response.getStatusLine().getStatusCode();
        String content = EntityUtils.toString(response.getEntity());
        switch(statusCode) {
            case HttpStatus.SC_UNAUTHORIZED:
            case HttpStatus.SC_FORBIDDEN:
                Map<String, String> keyValues = U.parseKeyValuePairsFromString(content);
                String error = keyValues.get("Error");
                String requiredCaptchaToken = keyValues.get("CaptchaToken");
                String captchaUrl = keyValues.get("CaptchaUrl");
                if (captchaUrl != null) {
                    captchaUrl = "https://www.google.com/accounts/" + captchaUrl;
                    String msg = "Error=" + error + " CaptchaToken=" + captchaToken + " CaptchaUrl=" + captchaUrl;
                    Log.d(TAG, msg);
                    PL.log(msg, context);
                    throw new LoginWithCaptchaRequiredException(requiredCaptchaToken, captchaUrl);
                }
                if ("BadAuthentication".equals(error))
                    error = "Username / Password wrong?";
                else
                    error += "\n" + response.getStatusLine();
                throw new AuthenticationFailedException("Authentication failed:\n" + error);
            case HttpStatus.SC_OK:
                break;
            default:
                String msg = "Oh, status code was " + statusCode + " but unexpected.";
                PL.log(msg, context);
                Log.w(TAG, msg);
                throw new AuthenticationFailedException("Autentication failed:\n" + EntityUtils.toString(response.getEntity()));
        }
        getEntryManager().saveAuthToken(new AuthToken(AuthToken.AuthType.AUTH_STANDALONE, U.parseKeyValuePairsFromString(content).get("Auth")));
        response.getEntity().consumeContent();
        getEntryManager().getNewsRobNotificationManager().cancelSyncProblemNotification();
        return true;
    } finally {
        httpClient.close();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient)

Example 92 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project newsrob by marianokamp.

the class GRAnsweredBadRequestException method submitSubscribe.

public boolean submitSubscribe(String url2subscribe) throws ReaderAPIException {
    Timing t = new Timing("Submit Subscribe", context);
    NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
    try {
        HttpPost editApiRequest = new HttpPost(getGoogleHost() + "/reader/api/0/subscription/edit");
        // quickadd
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        nameValuePairs.add(new BasicNameValuePair("client", CLIENT_NAME));
        nameValuePairs.add(new BasicNameValuePair("ac", "subscribe"));
        // quickadd
        nameValuePairs.add(new BasicNameValuePair("s", "feed/" + url2subscribe));
        HttpResponse resp = submitPostRequest(httpClient, editApiRequest, nameValuePairs, false);
        return resp.getStatusLine().getStatusCode() == HttpStatus.SC_OK;
    } catch (Exception e) {
        String message = "Problem during submission of subscribe: " + e.getMessage();
        Log.e(TAG, message, e);
        throw new ReaderAPIException(message, e);
    } finally {
        httpClient.close();
        t.stop();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) Timing(com.newsrob.util.Timing) ClientProtocolException(org.apache.http.client.ClientProtocolException) ParseException(java.text.ParseException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 93 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project newsrob by marianokamp.

the class GRAnsweredBadRequestException method submitNotes.

public void submitNotes(Job job) throws ReaderAPIException {
    Timing t = new Timing("Submit Notes", context);
    List<Entry> entries = getEntryManager().getEntriesWithNotesToBeSubmitted();
    if (entries.isEmpty()) {
        t.stop();
        return;
    }
    NewsRobHttpClient httpClient = NewsRobHttpClient.newInstance(false, context);
    try {
        for (Entry entry : entries) {
            HttpPost editApiRequest = new HttpPost(getGoogleHost() + "/reader/api/0/item/edit?client=" + CLIENT_NAME);
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("client", CLIENT_NAME));
            nameValuePairs.add(new BasicNameValuePair("title", entry.getTitle()));
            nameValuePairs.add(new BasicNameValuePair("url", entry.getAlternateHRef()));
            String s = "This feed url can only be set after the next article from this feed is imported into NewsRob. You can force this with Clear Cache/Refresh.";
            if (entry.getFeedAlternateUrl() != null)
                s = entry.getFeedAlternateUrl();
            nameValuePairs.add(new BasicNameValuePair("srcUrl", s));
            nameValuePairs.add(new BasicNameValuePair("srcTitle", entry.getFeedTitle()));
            nameValuePairs.add(new BasicNameValuePair("snippet", entry.getContent()));
            submitPostRequest(httpClient, editApiRequest, nameValuePairs, false);
            getEntryManager().update(entry);
        }
    } catch (Exception e) {
        String message = "Problem during submission of note: " + e.getMessage();
        Log.e(TAG, message, e);
        throw new ReaderAPIException(message, e);
    } finally {
        httpClient.close();
        t.stop();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NewsRobHttpClient(com.newsrob.download.NewsRobHttpClient) ArrayList(java.util.ArrayList) Timing(com.newsrob.util.Timing) ClientProtocolException(org.apache.http.client.ClientProtocolException) ParseException(java.text.ParseException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) SAXException(org.xml.sax.SAXException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 94 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project newsrob by marianokamp.

the class GRAnsweredBadRequestException method addParametersIncludingTokenToPostRequest.

private void addParametersIncludingTokenToPostRequest(HttpPost postRequest, List<NameValuePair> nameValuePairs) {
    // add token to the parameters, encode them and put them in the post
    // request
    List<NameValuePair> nvps = new ArrayList<NameValuePair>(nameValuePairs.size() + 1);
    nvps.addAll(nameValuePairs);
    nvps.add(new BasicNameValuePair("T", token));
    try {
        postRequest.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ArrayList(java.util.ArrayList) UnsupportedEncodingException(java.io.UnsupportedEncodingException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity)

Example 95 with BasicNameValuePair

use of org.apache.http.message.BasicNameValuePair in project wechat by motianhuo.

the class LoginActivity method onClick.

@Override
public void onClick(View v) {
    switch(v.getId()) {
        case R.id.img_back:
            Utils.finish(LoginActivity.this);
            break;
        case R.id.tv_wenti:
            Utils.start_Activity(LoginActivity.this, WebViewActivity.class, new BasicNameValuePair(Constants.Title, "帮助"), new BasicNameValuePair(Constants.URL, "http://weixin.qq.com/"));
            break;
        case R.id.btn_qtlogin:
            startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
            overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
            break;
        case R.id.btn_login:
            getLogin();
            break;
        default:
            break;
    }
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) Intent(android.content.Intent)

Aggregations

BasicNameValuePair (org.apache.http.message.BasicNameValuePair)289 NameValuePair (org.apache.http.NameValuePair)199 ArrayList (java.util.ArrayList)187 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)101 HttpPost (org.apache.http.client.methods.HttpPost)87 HttpResponse (org.apache.http.HttpResponse)77 HttpEntity (org.apache.http.HttpEntity)58 IOException (java.io.IOException)55 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)33 Test (org.junit.Test)32 HttpGet (org.apache.http.client.methods.HttpGet)29 ClientProtocolException (org.apache.http.client.ClientProtocolException)28 CSVReader (com.talend.csv.CSVReader)27 HttpClient (org.apache.http.client.HttpClient)25 UnsupportedEncodingException (java.io.UnsupportedEncodingException)23 HashMap (java.util.HashMap)20 JSONObject (org.json.JSONObject)20 Map (java.util.Map)19 URI (java.net.URI)16 WebserviceInvocation (com.github.hakko.musiccabinet.domain.model.library.WebserviceInvocation)15