2016-05-26 2 views
1

IDEが必要です。ヘッダー情報を取得する必要があり、アプリの別の部分で同じ情報をcgi-binに送り返す必要があります。私のコードは以下の通りで、ヘッダやクッキーを使って何かをする必要があると思う部分についてコメントしました。私はこれには非常に新しいですし、基本的なヘッダ/クッキーのチュートリアルでさえ難しいです。は例のヘッダとクッキー

/** 
* Your application code goes here<br> 
* This file was generated by <a href="https://www.codenameone.com/">Codename One</a> for the purpose 
* of building native mobile applications using Java. 
*/ 


package userclasses; 

import com.codename1.io.ConnectionRequest; 
import com.codename1.io.Cookie; 
import com.codename1.io.NetworkEvent; 
import com.codename1.io.NetworkManager; 
import com.codename1.io.Storage; 
import com.codename1.notifications.LocalNotification; 
import generated.StateMachineBase; 
import com.codename1.ui.*; 
import com.codename1.ui.events.*; 
import com.codename1.ui.util.Resources; 


import java.util.Timer; 
import java.util.TimerTask; 
//import org.apache.commons.httpclient.UsernamePasswordCredentials; 
//import org.apache.commons.httpclient.auth.AuthScope; 

/** 
* 
* @John Barrett 
*/ 
public class StateMachine extends StateMachineBase { 
public StateMachine(String resFile) { 
    super(resFile); 
    // do not modify, write code in initVars and initialize class members there, 
    // the constructor might be invoked too late due to race conditions that might occur 
} 

/** 
* this method should be used to initialize variables instead of 
* the constructor/class scope to avoid race conditions 
*/ 
protected void initVars(Resources res) { 
} 
boolean stop = false; 
boolean notify = false; 
String OnOff; 


@Override // Starts monitor action. 
protected void onMain_ButtonAction(Component c, ActionEvent event){ 

    // starts a timer to repeat monitor every minute. 
    Timer timer = new Timer(); 
    String text = (String) Storage.getInstance().readObject("SavedData"); 

    timer.schedule(new TimerTask(){ 


    @Override 
    public void run(){  
    if (stop == true){ 
     cancel();//Monitor ends   
    } 

    //Starts a connection with the URL to monitor 
    ConnectionRequest r = new ConnectionRequest(); 
    r.setUrl("http://vault.infinitevault.com/cgi-bin/absentmedia?customer=" + text.toLowerCase().trim()); 
    r.setPost(true); 

    // Post Header/Cookie information to URL for access NEED HELP WITH THIS! 
    r.setHttpMethod("HEAD"); 
    r.setContentType("text/xml"); 
    r.setCookiesEnabled(true); 
    findCodeLabel(c).setText("Monitoring: " + text.toUpperCase()); 
    r.addResponseListener(new ActionListener(){ 

    @Override 
    public void actionPerformed(ActionEvent ev){ 



        // Monitor starts 
        try{ 

        NetworkEvent event = (NetworkEvent) ev; 
        // Need to post header/cookie information here? HELP! 
        Cookie.isAutoStored(); 
        byte[] data= (byte[]) event.getMetaData(); 
        String decodedData = new String(data,"UTF-8");       

        boolean none; 

        none = decodedData.endsWith("NONE\n"); 


        if (!none){       



        System.out.println(decodedData);        

         findCodeTextArea(c).setText(decodedData); 

         LocalNotification n = new LocalNotification(); 
         n.setId("OSStorage"); 
         n.setAlertBody(decodedData); 
         n.setAlertTitle("Absent Media");     

         Display.getInstance().scheduleLocalNotification(
           n, 
           System.currentTimeMillis() + 10 * 1000, // fire date/time 
           LocalNotification.REPEAT_MINUTE // Whether to repeat and what frequency 
         ); 

         if (notify != true){ 
          Display.getInstance().vibrate(5000); 
         } 
         Storage.getInstance().writeObject("MonitorData", decodedData); 
        } 
        else{ 
       System.out.println("None"); 
         findCodeTextArea(c).setText("System is Good"); 
        } 
        } 
        catch (Exception ex){ 
         ex.printStackTrace(); 
        }    

    } 

    }); 

    NetworkManager.getInstance().addToQueue(r);  

    } 

    }, 0, 60*1000); 

} 

@Override // Stops the monitoring action. 
protected void onMain_StopButtonAction(Component c, ActionEvent event) { 
    super.onMain_StopButtonAction(c, event);//To change body of generated methods, choose Tools | Templates. 
    stop = true; 

    findCodeLabel(c).setText("Monitor Stopped"); 
    findCodeTextArea(c).setText(""); 
    findCodeTextArea(c).setHint("System information will show here");  
    Storage.getInstance().deleteStorageFile("MonitorData"); 

} 


@Override // Saves the settings to storage. 
protected void onSettings_SetSaveAction(Component c, ActionEvent event) { 

    String Station = findSetStation(c).getText(); 

    Storage.getInstance().writeObject("SavedData", Station); 
    String LoadStation = (String) Storage.getInstance().readObject("SavedData"); 
    findStationLabel(c).setText("Saved Station is " + LoadStation); 


} 

@Override // Sets what is saved to appear when in settings. 
protected void beforeSettings(Form f) { 

    String LoadStation = (String) Storage.getInstance().readObject("SavedData"); 
    findStationLabel(f).setText("Saved Station is " + LoadStation); 
    findSetStation(f).setText(LoadStation); 

    String CurrentNotify = (String) Storage.getInstance().readObject("OnOff"); 
    findSetNotifyLabel(f).setText("Vibration is " + CurrentNotify); 
} 



@Override // Sets what is saved to appear when in monitor screen. 
protected void beforeMain(Form f) { 
    String LoadStation = (String) Storage.getInstance().readObject("SavedData"); 
    findCodeLabel(f).setText(LoadStation); 
    if (findCodeTextArea(f) != null){ 
     String foundData = (String) Storage.getInstance().readObject("MonitorData"); 
     findCodeTextArea(f).setText(foundData); 



    } 

} 



@Override // Sets notification for turning vibration on. 
protected void onSettings_SetNotifyOnAction(Component c, ActionEvent event) { 
    notify = false; 
    OnOff = "ON"; 
    Storage.getInstance().writeObject("NotifyOn", notify); 
    Storage.getInstance().writeObject("OnOff", OnOff); 
    findSetNotifyLabel(c).setText("Vibration is " + OnOff); 

} 


@Override // Sets notification for turning vibration off. 
protected void onSettings_SetNotifyOffAction(Component c, ActionEvent event) { 
    notify = true; 
    OnOff = "OFF"; 
    Storage.getInstance().writeObject("NotifyOn", notify); 
    Storage.getInstance().writeObject("OnOff", OnOff); 
    findSetNotifyLabel(c).setText("Vibration is " + OnOff); 

} 


@Override // Sets message for monitoring or not. 
protected void beforeStartPage(Form f) { 
Storage.getInstance().deleteStorageFile("MonitorData"); 
String LoadStation = (String) Storage.getInstance().readObject("SavedData"); 


} 

@Override // Login button pressed after entering username and password. 
protected void onLogin_LoginAction(Component c, ActionEvent event) { 

// Gets the username and password entered. 
String userName = findUsename().getText(); 
String passWord = findPassword().getText(); 

// Establishes a conneciton to authentication. 
ConnectionRequest req=new ConnectionRequest(); 
req.setPost(false); 

req.setUrl("http://authentication.infinitevault.com/validate.php"); 

req.addArgument("username",userName); 
req.addArgument("password",passWord); 
req.addArgument("grant_type","client_credentials"); 

// To get the Header/Cookie information. 
req.getHttpMethod(); 
req.setCookiesEnabled(true); 

Cookie.setAutoStored(true); 
Cookie.isAutoStored(); 

// Sends message to user that system is verifying. 
findDenied(c).setText("Verifying"); 

NetworkManager.getInstance().addToQueueAndWait(req); 

if (req.getResponseData() != null) { 
    String token = new String(req.getResponseData()); 
    token = token.substring(token.indexOf('=') + 1); 
    System.out.println(token); 

    // Checks credentials if response is denied, goes back, 
    // If response is authenticated goes to main monitor form. 
    if (token.endsWith("denied")){ 


     /*try { 
     Thread.sleep(2000);     //1000 milliseconds is one second. 
     } catch(InterruptedException e) { 
     Thread.currentThread().interrupt(); 
     }*/ 

     back(); 

    } 
    else { 
     showForm("Main",null); 
    } 

} 




} 









}//end of app 
+0

ここにあまりにも多くのコードがあり、あなたが興味深い点としてコメントしたものは不明ですか?サーバから受信したクッキーを解析するか、リクエストに認可クッキーを挿入するかどうかは不明です –

+0

「ヘルプが必要です」または「ヘルプが必要です」と書いたコメントは、私が理解していないところです。私は自分がやろうとしていることをよりうまく表現する方法を知っていればいいと思う。私はcookieReceived(Cookie c)を使用しようとしましたが、十分理解できず、例を見つけることができませんでした。 –

+0

私はURLに接続して認証するためにconnectionRequestを使用しています。認証が成功すると、URLはCookieを含むヘッダーを送信しています。私はこれを解析する必要はないと思う。私がしたいのは、ヘッダー/クッキーを取得し、この情報をそのヘッダー/クッキーの受信を待っている別のURLに送り返すことができ、アプリがもっとアクセスできるようにすることです。私は非常に多くの異なったことを試みてきましたが、私はまだ働くことは何も見つかりませんでした。それが私が手を差し伸べる理由です。御時間ありがとうございます。 –

答えて

0

接続要求メソッドcookieReceived(Cookie c)をオーバーライドして、そこにある各クッキーのロジックを処理してください。

+0

私はcookieReceived(Cookie c)をオーバーライドして挿入する方法を理解していますが、理解していない各クッキーのロジックを処理します。 –

+0

このメソッドはサーバーから返されたすべてのクッキーに対して呼び出されるので、レスポンスの状態を含む変数を保持し、そこにその状態を格納することができます。 'readResponse'の前に' handleCookie'が呼び出されることに注目してください。 –

+0

Shaiさん、私はまだ苦労していますが、あなたは私にもう少しヒントを与えました。私はあなたの時間を感謝します。 –

関連する問題