Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to pass variable String from this situation?
    primarykey
    data
    text
    <p>I have this three different projects. Project1(isLibrary), Project2 and Project3 set the Project1 as Library. Now, my problem is that Im sending a request to the server but I cant get to pass the String from my Project2 to Project1. Project 3 will also use Project1 and will send a different request. Any Ideas?</p> <p>In my Project1, I have an TestAsyncTask Class.</p> <pre><code>public class TestAsyncTask extends AsyncTask&lt;String, Void, String&gt; { 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 &lt; 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]); } } } </code></pre> <p>In my Project2, I have TestActivity1 class which extends the activity, it's the UI</p> <pre><code>public class TestActivity1 extends Activity{ TestAsyncTask asyncTask = new TestAsyncTask(this); //This is just a sample soap String requestString = "&lt;soapenv----------------------------------------------------------------&gt;"; @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); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload