Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在这种情况下传递变量字符串?_Android_Variables_Android Asynctask - Fatal编程技术网

Android 如何在这种情况下传递变量字符串?

Android 如何在这种情况下传递变量字符串?,android,variables,android-asynctask,Android,Variables,Android Asynctask,我有三个不同的项目。Project1(isLibrary)、Project2和Project3将Project1设置为库。现在,我的问题是我向服务器发送了一个请求,但是我无法将字符串从我的Project2传递到Project1。项目3也将使用项目1,并将发送不同的请求。有什么想法吗 在我的Project1中,我有一个TestAsyncTask类 public class TestAsyncTask extends AsyncTask<String, Void, String> {

我有三个不同的项目。Project1(isLibrary)、Project2和Project3将Project1设置为库。现在,我的问题是我向服务器发送了一个请求,但是我无法将字符串从我的Project2传递到Project1。项目3也将使用项目1,并将发送不同的请求。有什么想法吗

在我的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]);

    }
}
}
公共类TestAsyncTask扩展了AsyncTask{
TextView textView1[],textView2[];
文本视图文本视图;
私人线性布局线性布局;
//它只是一个示例,不是有效的soap头
字符串string1=”http://soapactionheaderline“;//提供SOAPAction标题行的值。
//这只是一个示例,不是有效的服务器
字符串string2=”https://server.com“;//这是我们将要连接到的目标URL/服务器。
语境;
int-resultInt;
//建造师
公共TestAsyncTask(上下文cContext){
context=cContext;//将上下文传递给构造函数
}
//线性布局的Getter。
public LinearLayout getLinearLayout(){
返回线性布局;
}
//线性布局的设定器。
公共无效设置LinearLayout(LinearLayout-LinearLayout){
this.linearLayout=linearllayout;
}
//字符串的Getter。
公共字符串getString(){
返回字符串2;
}
//字符串设置器。
公共无效设置字符串(字符串sString){
this.string2=sString;
}
@凌驾
受保护的字符串doInBackground(字符串…aServerConnectionString){
字符串resultString=null;
试一试{
//使用URL和HttpURLConnection进行服务器连接。
URL=新URL(string2);
HttpURLConnection HttpURLConnection=(HttpURLConnection)uRL.openConnection();
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
httpURLConnection.setUseCaches(false);
httpURLConnection.setChunkedStreamingMode(0);
//.addRequestProperty-将给定属性添加到请求SOAPAction标头
httpURLConnection.addRequestProperty(“SOAPAction”,string1);
httpURLConnection.addRequestProperty(“内容类型”,“文本/xml;字符集=utf-8”);
httpURLConnection.addRequestProperty(“Content-Length”,这是我需要从Project2.Length()中传递字符串变量的地方;
httpURLConnection.setRequestMethod(HttpPost.METHOD_名称);
//使用OutputStream和Writer向服务器发送请求。
OutputStream OutputStream=httpURLConnection.getOutputStream();
Writer Writer=新的OutputStreamWriter(outputStream);
write(“这是我需要从Project2传递字符串变量的地方”);
writer.flush();
writer.close();
//使用InputStream从服务器获取请求的响应。
InputStream InputStream=httpURLConnection.getInputStream();
BufferedReader BufferedReader=新的BufferedReader(新的InputStreamReader(inputStream));
ByteArrayBuffer ByteArrayBuffer=新ByteArrayBuffer(50);
int=httpURLConnection.getResponseCode();
而((aint=bufferedReader.read())!=-1){
byteArrayBuffer.append(aint);//读取缓冲区中的字节,直到没有更多的内容可读取为止。
}
resultString=新字符串(byteArrayBuffer.toByteArray());
//使用SAXParser(XML的简单API)处理XML解析(响应)。
SAXParserFactory SAXParserFactory=SAXParserFactory.newInstance();
SAXParser SAXParser=sAXParserFactory.newSAXParser();
XMLReader=sAXParser.getXMLReader();
//创建处理XML标记的处理程序
TestXMLHandler=新的TestXMLHandler();
setContentHandler(xMLHandler);
InputSource InputSource=新的InputSource(新的StringReader(resultString));
parse(inputSource);
}捕获(异常){
resultString=exception.getMessage();并将其显示到UI。
}
返回结果字符串;
}
//此步骤是doInBackground的返回值。
PostExecute上受保护的void(字符串为ReturnValueString){
//创建GBData类的对象/实例,并从GBXMLHandler获取结果。
TestGetterSetter data=TestXMLHandler.testdata;
int sizeInt=data.getOperatorName().size();
textView1=新的TextView[sizeInt];
textView2=新的TextView[sizeInt];
//for语句提供了一种对一系列值进行迭代的紧凑方法。
对于(resultInt=0;resultInt
在我的项目2中,我有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);
    }
}
公共类TestActivity1扩展活动{
TestAsyncTask asyncTask=新的TestAsyncTask(此);
//这只是一个肥皂样本
字符串requestString=“”;
@凌驾
创建时的公共void(Bundle-aBundle){
super.onCreate(aBundle);
执行(asyncTask.getString());
LinearLayout LinearLayout=asyncTask.getLinearLayout();
linearLayout=新的linearLayout(本);
线性布局。设置方向(1);
asyncTask.setLinearLayout(linearLayout);
//将ContentView设置为要显示的布局
setContentView(线性布局);
}
}

我真的不知道你想在代码中实现什么。无论如何,说到问题,我认为您已经在向异步任务传递字符串值了。您所需要做的就是在其doInBackground方法中使用它。例如:

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

   ...
   ...
   ...

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

   ...
   ...
   ...
}

另外,我相信您的代码不会运行,因为它在某些地方似乎不符合逻辑。

首先,我认为您仔细阅读非常重要,因为您的实现
String url = "";
String request = "";
asyncTask.execute(url, request);
@Override
protected String doInBackground(String... aServerConnectionString) {
String url, request;

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

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