2015-09-25 65 views
0

我知道,這不是從Android設備向web服務器發送文本文件的最佳方法,但我無法在服務器端更改任何內容,我必須堅持到這個方法。 讓我解釋一下:在Android中使用HTTP GET方法逐行發送TextFile

該文本文件是一個命令列表。我讀的第一行,並在發送這樣的:

http://1.2.3.4/?command=FirstLineOfTheTextfile

來自服務器的答案很簡單JSON,像這樣:

{「OK」:真正}或{「OK」 :假}

根據不同的答案(TRUE或FALSE)我要重新發送命令或讀取下一行,並把它以同樣的方式:

http://1.2.3.4/?command=SecondLineOfTheTextfile

這樣做直到文本文件的最後一行。

I`ve在做APP與如下:

  1. 掃描的AP
  2. 連接到它
  3. 讀取文件
  4. 發送第一線
  5. 解析答案

但是,我不知道最好的辦法是什麼:

發送命令

等待答案

如果FALSE發送再次

如果真發送下一個命令

等待答案

如果再假髮送

if TRUE發送下一個命令

等待答案 。 。 等等

任何建議表示讚賞! :)

在我做這樣的與第一和第二個命令的那一刻:

public void CheckStatus() { 
    TextView actualAction = (TextView) findViewById(R.id.action); 
    if (filePath != "") { 
     actualAction.setTextColor(getResources().getColor(R.color.green)); 
     actualAction.setText("Getting STATUS information..."); 
     sendCommand("http://1.2.3.4/?action=A"); //FIRST LINE OF THE TEXTFILE 
    } else { 
     actualAction.setTextColor(getResources().getColor(R.color.red)); 
     actualAction.setText("Choose a file first!"); 
    } 
} 

public void switchTheLightOn() { 
    okStatus = false; 
    TextView actualAction = (TextView) findViewById(R.id.action); 
    actualAction.setTextColor(getResources().getColor(R.color.green)); 
    actualAction.setText("Switching to UPDATE mode..."); 
    sendCommand("http://1.1.1.1/?action=on"); //SECOND LINE OF THE TEXTFILE 
} 

當答案解析:

private Handler messageHandler = new Handler() { 
    public void handleMessage(Message msg) { 
     super.handleMessage(msg); 

     TextView clientanswer = (TextView) findViewById(R.id.clientanswer); 

     String message = (String) msg.obj; 
     try { 
      JSONObject mainObject = new JSONObject(message); 
      okStatus = mainObject.getBoolean("OK"); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     } 
     String answerText = clientAnswer.toString(); 
     clientanswer.setText(Html.fromHtml(answerText)); 
     if (okStatus) { 
      switchTheLightOn() 
     } 
    } 
}; 
+0

你的問題是什麼問題? – Oleksandr

+0

嗨,我編輯了這篇文章。我的問題是,當你在那個文本文件中有400條命令時,做這個命令 - 回答方法的最好方法是什麼。所以我想逐行發送文本文件。每一行都緊挨着......但取決於答案。謝謝。 – TheSaint

回答

0

有關的解決方案是什麼,我覺得最簡單的一種是創建一個簡單的界面Command這將是所有可能命令的基本界面。

public interface Command { 
    /** Get text representation for command */ 
    String getCommand(); 

    /** Get description for command */ 
    String getDescription(); 
} 

然後你必須通過執行Command接口來實現所有可能的命令。

例如CommandA:

public class CommandA implements Command { 
    @Override 
    String getCommand() { 
     return "A" 
    } 

    @Override 
    String getDescription() { 
     return "Getting STATUS information..."; 
    } 
} 

然後初始化數組的你命令與適當的項目。

Command commands = new Command[100]; 
// TODO fill array 

然後移到該陣列迭代以便發送人這個命令

int maxRetryCount = 3; 
int actualRetryCount = 0; 
for (int i = 0; i< commands.size(); i++) { 
    actualAction.setText(commands.get(i).getDescription()); 
    boolean isSuccess = sendCommand("http://1.2.3.4/?action=" + command.getCommand()); 
    if (!isSuccess) { 
     if (actualRetryCount == maxRetryCount) { 
      // exit from loop in order to avoid situation with infinitive loop 
      break; 
     } 
     actualRetryCount++; 
     --i; 
    } else { 
     actualRetryCount = 0; 
    } 

    // Do whatever you need 
} 

P.S.這是有史以來最糟糕的客戶端服務器架構。

+0

感謝您的想法!不要評論我,網絡服務器是不可接觸的。我只能以這種方式與它溝通。 – TheSaint

+0

@TheSaint不用客氣 – Oleksandr

+0

但是我不明白的一件事。 sendCommand發送命令並且isSuccess始終爲真,除非服務器不在線。但下一步必須取決於服務器的答案。如果爲TRUE,則發送下一個命令,如果爲FALSE,則重試... sendCommand打開httpurlconnection,從inputstrem中恢復答案並設置okStatus變量。 – TheSaint