2016-03-03 71 views
0

我有這個條件來編寫每50ms執行一次的服務器調用。 服務器調用必須來自齊射。 但是我面臨的困難是每個服務器調用都有不同的URL,以及如何在線程中傳遞這些不同的URL以便每隔50ms調用一次服務器。在線程中執行不同的操作 - android

+0

你有,你必須按順序呼叫服務器,每一個50ms的列表,或者必須把他們都稱爲每隔50ms? –

+0

聽起來像你正在嘗試DDOS的人 – DDsix

+0

我正在從移動控制板上工作。所以根據需要,這個板子不斷的服務器調用將會產生,這些命令將隨着用戶在移動應用程序上按下按鈕而不同@RaduIonescu – guerrero

回答

0

我不是Android專家,但這是我想到的,如果你想在50ms後調用不同的URL。請糾正我,如果我錯了:)

import android.app.AlertDialog; 
import android.app.Dialog; 
import android.app.ProgressDialog; 
import android.content.DialogInterface; 
import android.os.AsyncTask; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.Menu; 
import android.view.MenuItem; 

import java.io.BufferedReader; 
import java.io.DataOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.MalformedURLException; 
import java.net.URL; 
import java.net.URLEncoder; 
import java.util.concurrent.Executors; 
import java.util.concurrent.ScheduledExecutorService; 
import java.util.concurrent.TimeUnit; 

public class MainActivity extends AppCompatActivity { 
    private static final ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor(); 
    int i=0; 
    final String[] urlArray = new String[]{"http://google.com","http://fb.com"};//your url array here 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     Runnable task = new Runnable() { 
      public void run() { 
       String currentURL= MainActivity.this.getNextURL(); 
       new HitWebService().execute(currentURL); 
      } 
     }; 
     worker.schedule(task, 50, TimeUnit.SECONDS); 
    } 

    private String getNextURL(){ 
     String currentURL= urlArray[i]; 
     if(i == urlArray.length){ 
      i=0; 
     } 
     else{ 
      i++; 
     } 
     return currentURL; 
    } 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.menu_main, menu); 
     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) { 
      return true; 
     } 

     return super.onOptionsItemSelected(item); 
    } 

    private class HitWebService extends AsyncTask<String,Void,String> { 

     protected void onPreExecute(){ 
      //do whatever you want with respect to ui 
     } 

     @Override 
     protected String doInBackground(String... params){ 
      HttpURLConnection connection=null; 
      String stringUrl= params[0]; 
      try { 
       URL url= new URL(stringUrl); 
       connection=(HttpURLConnection)url.openConnection(); 
       connection.setRequestMethod("POST"); 
       connection.setDoInput(true); 
       connection.setDoOutput(true); 
       connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

       DataOutputStream outputStream=new DataOutputStream(connection.getOutputStream()); 
       String parameters = "initialise your parameters here, pass parameters also in params and access it like params[1]"; 

       outputStream.writeBytes(parameters); 
       outputStream.flush(); 
       outputStream.close(); 

       InputStream inputStream=connection.getInputStream(); 
       BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream)); 
       StringBuffer response = new StringBuffer(); 
       String line; 
       while((line=bufferedReader.readLine())!=null){ 
        response.append(line); 
       } 

       bufferedReader.close(); 
       return response.toString(); 
      } 
      catch (MalformedURLException malformedException){ 
       return malformedException.toString(); 
      } 
      catch (IOException ioException){ 
       return ioException.toString(); 
      } 
      finally { 
       if(connection !=null){ 
        connection.disconnect(); 
       } 
      } 
     } 

     @Override 
     protected void onPostExecute(String response){ 
      //do whatever you want here 
     } 
    } 
} 
+0

這不是我正在尋找的東西,但是這確實爲我提供了一個指導。謝謝。 @ sandeep-bhandari – guerrero

+0

總是歡迎先生:)我可以幫助的快樂:) –

相關問題