0

私はこの3つの異なるプロジェクトを持っています。Project1(isLibrary)、Project2、および Project3 は、Project1 をライブラリとして設定します。今、私の問題は、サーバーにリクエストを送信していますが、Project2 から Project1 に文字列を渡すことができないことです。Project 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);
    }
}
4

3 に答える 3

0

ライブラリプロジェクトとターゲットプロジェクトのstrings.xmlに文字列を保持します..リソースの最優先事項は常にターゲットプロジェクトに与えられます..そのため、同じIDを参照して問題なくスターリングをサーバーに送信できます

于 2012-10-17T13:57:36.773 に答える
0

まず、 AsyncTaskを読むことが非常に重要だと思います。なぜなら、あなたの実装は、それが機能するように設計されている方法を正しく利用していないからです。私が正直に言うと、あなたは自分自身にもっと問題を引き起こしました:)

現在の実装に基づいて問題を解決するには、execute()関数がdoInBackground()と組み合わせてどのように機能するかを確認することが重要です。

Execute は引数として文字列配列を受け取るため、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 に依存して文字列を通過するため、文字列に関連するすべてのものを削除できます。

于 2012-10-17T13:54:09.860 に答える
0

コードで何を達成しようとしているのか、私にはよくわかりません。とにかく、問題に来て、あなたはすでに AsyncTask に文字列値を渡していると思います。doInBackgroundメソッドでそれを利用するだけです。例えば:

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

   ...
   ...
   ...

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

   ...
   ...
   ...
}

PS特定の場所では論理的ではないように見えるため、コードは実行されないと思います

于 2012-10-17T13:14:05.797 に答える