2012-10-17 27 views
0

我有這三個不同的項目。 Project1(isLibrary),Project2和Project3將Project1設置爲庫。現在,我的問題是,我發送一個請求到服務器,但我不能從我的Project2到Project1傳遞字符串。項目3也將使用Project1併發送不同的請求。有任何想法嗎?如何從這種情況傳遞變量字符串?

在我的Project1中,我有一個TestAsyncTask類。

public class TestAsyncTask extends AsyncTask<String, Void, String> { 

TextView textView1[], textView2[]; 
TextView textView; 
private LinearLayout linearLayout; 
//It's just a sample, not a valid soap header 
String string1 = "http://soapactionheaderline"; //Provides the value for the SOAPAction header line. 
//It's just a sample, not valid server 
String string2 = "https://server.com"; //This is the target URL/Server that we will be connecting to. 
Context context; 
int resultInt; 

//Constructor 
public TestAsyncTask(Context cContext){ 
    context = cContext; //Pass Context to constructor 
} 

//Getter for LinearLayout. 
public LinearLayout getLinearLayout(){ 
    return linearLayout; 
} 
//Setter for LinearLayout. 
public void setLinearLayout(LinearLayout lLinearLayout){ 
    this.linearLayout = lLinearLayout; 
} 

//Getter for String. 
public String getString(){ 
    return string2; 
} 
//Setter for String. 
public void setString(String sString){ 
    this.string2 = sString; 
} 


@Override 
protected String doInBackground(String... aServerConnectionString) { 

String resultString = null; 

try { 

    // Uses URL and HttpURLConnection for server connection. 
    URL uRL = new URL(string2); 
    HttpURLConnection httpURLConnection = (HttpURLConnection) uRL.openConnection(); 
    httpURLConnection.setDoOutput(true); 
    httpURLConnection.setDoInput(true); 
    httpURLConnection.setUseCaches(false); 
    httpURLConnection.setChunkedStreamingMode(0); 

    //.addRequestProperty - Adds the given property to the request SOAPAction header 
    httpURLConnection.addRequestProperty("SOAPAction", string1); 
    httpURLConnection.addRequestProperty("Content-Type", "text/xml; charset=utf-8"); 
    httpURLConnection.addRequestProperty("Content-Length", "" + "THIS IS WHERE I NEED TO PASS THE STRING VARIABLE FROM MY Project2".length()); 
    httpURLConnection.setRequestMethod(HttpPost.METHOD_NAME); 

    // Using OutputStream and Writer to send a request to the server. 
    OutputStream outputStream = httpURLConnection.getOutputStream(); 
    Writer writer = new OutputStreamWriter(outputStream); 
    writer.write("THIS IS WHERE I NEED TO PASS THE STRING VARIABLE FROM MY Project2"); 
    writer.flush(); 
    writer.close(); 

    // Using InputStream to get the response of the request from the server. 
    InputStream inputStream = httpURLConnection.getInputStream(); 
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); 
    ByteArrayBuffer byteArrayBuffer = new ByteArrayBuffer(50); 

    int aint = httpURLConnection.getResponseCode(); 

    while ((aint = bufferedReader.read()) != -1) { 
     byteArrayBuffer.append(aint); //Read bytes to the Buffer until there is nothing more to read. 
    } 

    resultString = new String(byteArrayBuffer.toByteArray()); 

    // Use SAXParser(Simple API for XML) to handle the parsing of XML(Response). 
    SAXParserFactory sAXParserFactory = SAXParserFactory.newInstance(); 
    SAXParser sAXParser = sAXParserFactory.newSAXParser(); 
    XMLReader xMLReader = sAXParser.getXMLReader(); 
    // Create handler to handle XML Tags 
    TestXMLHandler xMLHandler = new TestXMLHandler(); 
    xMLReader.setContentHandler(xMLHandler); 
    InputSource inputSource = new InputSource(new StringReader(resultString)); 
    xMLReader.parse(inputSource); 

    } catch (Exception exception) { 
    resultString = exception.getMessage(); in the created String and display it to UI. 
    } 
    return resultString; 
} 

//This step is the return-value from doInBackground. 
protected void onPostExecute(String aReturnValueString) { 

    // Create an object/instance of GBData Class and get results from GBXMLHandler. 
    TestGetterSetter data = TestXMLHandler.testdata; 

    int sizeInt = data.getOperatorName().size(); 

    textView1 = new TextView[sizeInt]; 
    textView2 = new TextView[sizeInt]; 

    //The for statement provides a compact way to iterate over a range of values. 
    for (resultInt = 0; resultInt < sizeInt; resultInt++) { 

    textView1[resultInt] = new TextView(context.getApplicationContext()); 
    textView1[resultInt].setText("OperatorName = " + data.getOperatorName().get(resultInt)); 
    linearLayout.addView(textView1[resultInt]); 

    textView2[resultInt] = new TextView(context.getApplicationContext()); 
    textView2[resultInt].setText("type = " + data.getType().get(resultInt)); 
    linearLayout.addView(textView2[resultInt]); 

    } 
} 
} 

在我Project2的,我有它擴展了活動TestActivity1類,它的UI

public class TestActivity1 extends Activity{ 

    TestAsyncTask asyncTask = new TestAsyncTask(this); 

    //This is just a sample soap 
    String requestString = "<soapenv---------------------------------------------------------------->"; 

    @Override 
    public void onCreate(Bundle aBundle) { 
     super.onCreate(aBundle);    

     asyncTask.execute(asyncTask.getString()); 

     LinearLayout linearLayout = asyncTask.getLinearLayout(); 
     linearLayout = new LinearLayout(this); 
     linearLayout.setOrientation(1); 
     asyncTask.setLinearLayout(linearLayout); 

     // Set the ContentView to layout for display 
     setContentView(linearLayout); 
    } 
} 
+0

你爲什麼要在AsyncTask中準備你的佈局? – waqaslam

+0

抱歉,無法理解你的問題。感謝您的答覆。你能幫我解決這個問題嗎?我需要將變量從我的活動傳遞給asynctask – Stella

回答

0

我真的不知道你想在你的代碼來實現的。無論如何,出現這個問題,我想你已經將一個字符串值傳遞給了你的AsyncTask。所有你需要做的就是在它的doInBackground方法中使用它。例如:

protected String doInBackground(String... aServerConnectionString) { 
    String yourValue = aServerConnectionString[0]; 

    ... 
    ... 
    ... 

    writer.write(yourValue); //pass your value to writer 

    ... 
    ... 
    ... 
} 

P.S.我相信你的代碼不會運行,因爲它在某些地方似乎不合邏輯

+0

我試圖實現的是重用TestAsyncTask類,我有兩個Android項目將向服務器發送不同的請求,但將使用相同的AsyncTask格式。 – Stella

+0

但是你爲什麼要將你的Activity的佈局放在裏面? – waqaslam

+0

您是否指出了LinearLayout? – Stella

0

首先,我認爲它非常重要,因爲你的實現沒有正確地利用它的設計工作方式,所以你閱讀了AsyncTask。如果我誠實的話,你已經給自己造成了更多的問題:)

要根據你當前的實現來解決你的問題,重要的是你會看到execute()函數如何與doInBackground()結合使用。

執行需要爲它的參數字符串數組,所以Project2中,你可以做這樣的事情:

String url = ""; 
String request = ""; 
asyncTask.execute(url, request); 

然後在你的AsyncTask,doInBackground方法接收時使用的execute方法的參數。當您通過您需要的類之間的值,你doInBackground方法可能是這個樣子:

@Override 
protected String doInBackground(String... aServerConnectionString) { 
String url, request; 

if (aServerConnectionString[0] != null) { 
    url = aServerConnectionString[0]; 
} 

if (aServerConnectionString[1] != null) { 
    request = aServerConnectionString[1]; 
} 

這樣你的AsyncTask可以刪除所有的字符串相關的東西,因爲它會依賴於Project2中/ 3通過琴絃。

0

保持strings.xml中的字符串庫中的項目,以及目標projects..always資源的首要任務是給有針對性的project..so可以引用相同的ID和發送盯着服務器沒有任何問題

相關問題