我有这三个不同的项目。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);
}
}