簡體   English   中英

將javascript代碼轉換為android代碼-如何在POST請求中添加引薦來源網址?

[英]Converting javascript code to android code - how to add referrer in POST request?

我的網站上有這個Java腳本代碼,當有人訂閱我的新聞時該代碼就會執行。 基本上什么都沒有,只是發布請求。 這是一段代碼。

function es_submit_request(url, parameters, es_widget_form) {
http_req = false;

http_req.onreadystatechange = function() {eemail_submitresult(es_widget_form)}; // Passing the form to the submit request
http_req.open('POST', url, true);
http_req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http_req.send(parameters);
}

是否可以從我的android應用程序代碼中調用此post方法來訂閱某人的新聞通訊?

在這里嘗試過此代碼但無法正常工作。

當我調試我的js代碼時,變量值以

parameters = "es_email=fgnfg@dgd.com&es_name=&es_group=&timestamp=&action=0.9901232281510463"
url = "http://thetechguru.in/?es=subscribe"

如果有人可以幫助我提供此代碼,我將不勝感激。 我寧願不為此使用任何庫,因為我不想為這么小的事情帶來開銷。 (僅適用於我的應用中的一個網絡通話)

這是我正在嘗試的代碼,但是沒有用。

String urlString = "http://www.thetechguru.in/?es=subscribe&es_email=fsdsf@dgd.com&es_name=&es_group=&timestamp=&action=0.9901232281510463";

        String resultToDisplay = "";

        InputStream in = null;
        try {

            URL url = new URL(urlString);

            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            in = new BufferedInputStream(urlConnection.getInputStream());


        } catch (Exception e) {

            System.out.println(e.getMessage());

            return e.getMessage();

        }

        try {
            resultToDisplay = in.toString();//IOUtils.toString(in, "UTF-8");
            //to [convert][1] byte stream to a string
            Log.v("Response",resultToDisplay);
        } catch (Exception e) {
            e.printStackTrace();
        }

代碼執行,但是什么也沒有發生,列表中未添加電子郵件ID

您的Application Manifest文件是什么樣的? 如果您的應用程序沒有訪問互聯網的權限,則不會。 嘗試將<uses-permission android:name="android.permission.INTERNET" />到您的AndroidMainfest.xml文件中(在<manifest>標記內部,但在<application>標記之前)。

日志中是否顯示任何內容(僅在設置為verbose時才會顯示log.v)? 如果是這樣,請分享顯示的內容。 日志指南

如果沒有,es_email = fsdsf @ dgd.com電子郵件已經在列表中了嗎? 確保您正在更新按鈕上的URLstring的es_email參數

我終於解決了這個問題。 即使我使用發送POST請求的正確代碼,也無法訂閱電子郵件ID。 我檢查了請求的HTTP響應,發現它返回錯誤“意外錯誤”。 我檢查了服務器代碼,並在php代碼中檢查了HTTP_REFERER。 因此做了一些研究,並在我的java請求中添加了REFERER,瞧,成功了!

URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
 //Set to POST
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setDoInput(true);
//Added referer
connection.addRequestProperty("REFERER", "http://thetechguru.in");
connection.setRequestProperty("Content-type", "application/x-www-form-urlencoded");
connection.setReadTimeout(10000);
Writer writer = new OutputStreamWriter(connection.getOutputStream());
writer.write(query);
writer.flush();
writer.close();

我希望它可以幫助某人

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM