无法在java / android中收集int数组的值

时间:2012-09-25 12:01:59

标签: java android

我正在尝试收集int数组中的一些值,这些值来自Web服务,通​​过使用它。我正在使用SOAP方法进行消费。

当我试图收集int数组中的值时,我无法运行模拟器。

如何克服此错误?请找我的参考资料。

Main_WB.java

 public class Main_WB extends Activity 
 {
EditText edt1,edt2;
TextView txt_1;
Button btn;

 @Override
 public void onCreate(Bundle savedInstanceState) 
 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    edt1 = (EditText)findViewById(R.id.editText1);
    edt2 = (EditText)findViewById(R.id.editText2);
    btn = (Button)findViewById(R.id.button1);

    btn.setOnClickListener(new View.OnClickListener()
    {
    public void onClick(View v) 
    {
        getTMSChart(edt1.getText().toString(),edt2.getText().toString());
    }     
    });
  }

 private void getTMSChart(String FromDate,String ToDate)
 {
     txt_1 = (TextView)findViewById(R.id.textView1);

     System.setProperty("http.keepAlive", "false");        
     SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);        

     envelope.dotNet = true;

     String NAMESPACE = "http://tempuri.org/";
     String URL = "http://54.251.60.177/TMSOrdersService/TMSDetails.asmx";
     String METHOD = "GetTMSChart";

     SoapObject request = new SoapObject(NAMESPACE, METHOD);        
     request.addProperty("FromDate", FromDate);               
     request.addProperty("ToDate", ToDate);

     envelope.setOutputSoapObject(request);
     HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

     try 
     {
         androidHttpTransport.call(NAMESPACE + METHOD, envelope);

         SoapObject result = (SoapObject) envelope.bodyIn;

         SoapObject root =  (SoapObject) ((SoapObject)(result).getProperty(0)).getProperty("NewDataSet");

         int tablesCount = root.getPropertyCount();


      for (int i = 0; i < tablesCount; i++)
      {
         SoapObject table = (SoapObject) root.getProperty(i);
         int propertyCount = table.getPropertyCount();

      for (int j = 0; j < propertyCount; j++)
      {           

    //  String orderNo =  table.getPropertyAsString("Order_No");
    //  String freight =  table.getPropertyAsString("Freight_Rate");
    //  String percent =  table.getPropertyAsString("Margin_Percent");


       int orderNo = Integer.parseInt(table.getPropertyAsString("Order_No"));
       int freightRate = Integer.parseInt(table.getPropertyAsString("Freight_Rate"));
       int marginPercent = Integer.parseInt(table.getPropertyAsString("Margin_Percent"));

       int[] ord = new int[orderNo];
       int[] frei = new int[freightRate];
       int[] margin = new int[marginPercent];


     // whatever you do with these values

       txt_1.setText(ord);
       txt_1.setText(frei);
       txt_1.setText(margin);
          }                   
       }
    }   
    catch (Exception e) 
    {
    }   
    }    }

2 个答案:

答案 0 :(得分:1)

这是一个编译错误,错误很明显:

The method setText(CharSequence) in the type TextView is not applicable for the arguments (int[])

这意味着方法setText()的参数必须是CharSequence类型,但是您使用int[]类型的参数调用它,该参数不是CharSequence。

int[]数组转换为字符串,并在String实现CharSequence时,将生成的字符串传递给setText()。例如:

txt_1.setText(Arrays.toString(ord));

此外,我真的没有看到在同一文本字段上调用setText()三个不同的参数。

答案 1 :(得分:0)

   int orderNo = Integer.parseInt(table.getPropertyAsString("Order_No"));
   int freightRate = Integer.parseInt(table.getPropertyAsString("Freight_Rate"));
   int marginPercent = Integer.parseInt(table.getPropertyAsString("Margin_Percent"));

   int[] ord = new int[orderNo];
   int[] frei = new int[freightRate];
   int[] margin = new int[marginPercent];


 // whatever you do with these values

   txt_1.setText(ord);
   txt_1.setText(frei);
   txt_1.setText(margin);

你想在这做什么?从查看那段(无用的)代码看,你似乎缺乏基本的编程知识,也许应该阅读更多的教程。

那说我在指出你在那里做了什么:

    int orderNo = Integer.parseInt(table.getPropertyAsString("Order_No"));

在这里,您将属性“Order_No”作为字符串值请求并将其转换为int。到目前为止一切都很好。

    int[] ord = new int[orderNo];

在这里创建一个int数组,其元素数量等于orderNo。因此,如果orderNo是12345,则创建一个包含12345个元素的int数组。我认为这不是你想要的。

   txt_1.setText(ord);

在这里,您将这个巨大的(未初始化的)int-array作为参数传递给txt_1的setText方法。该方法显然需要字符串值而不是int数组。

那你想做什么?

修改

回答有关创建int数组的问题:

int[] ord = new int[propertyCount];
int[] frei = new int[propertyCount];
int[] margin = new int[propertyCount];

for (int j = 0; j < propertyCount; j++)
{           
    int orderNo = Integer.parseInt(table.getPropertyAsString("Order_No"));
    int freightRate = Integer.parseInt(table.getPropertyAsString("Freight_Rate"));
    int marginPercent = Integer.parseInt(table.getPropertyAsString("Margin_Percent"));

    ord[j] = orderNo;
    frei[j] = freightRate;
    margin[j] = marginPercent;

}                   

// process the arrays;

我假设你想要每个表一个数组,所以我在内部循环中创建 数组,并在循环中填充它们。

之后您可以处理这些数组。请注意,为外循环中的每个表重新创建数组。

希望有所帮助。