为什么我无法从android中获取值?

时间:2015-05-16 10:13:26

标签: java android jsp

在我的main.xml中,我设置两个EditText来获取num1和num 2,在Buntto上运行添加操作

/home/vagrant/.nave/installed/0.12.2/lib/node_modules/mymodule/config.json

MainActivity.java

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <EditText
        android:id="@+id/num1"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:hint="num1"
        android:inputType="number" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:padding="5dp"
        android:text="+" />

    <EditText
        android:id="@+id/num2"
        android:layout_width="0dp"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:hint="num2"
        android:inputType="number" />

    <TextView
        android:id="@+id/result"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:padding="5dp"
        android:text="=" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center"
        android:padding="5dp" />
</LinearLayout>

<Button
    android:id="@+id/btn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="add"
    android:textSize="10sp" />

我的index.jsp:

public class MainActivity extends Activity {
    public static final int UPDATE_TEXT = 1;
    private EditText etx1;
    private EditText etx2;
    private TextView result;
    Button getresult;
    private double num1;
    private double num2;
    private double resultnum;
    private Handler handler = new Handler() {
        public void handleMessage(android.os.Message msg) {
            switch (msg.what) {
            case UPDATE_TEXT:
                new Thread(new Runnable() {

                    public void run() {
                        // TODO Auto-generated method stub
                        String url = "http://localhost:8080/test/index.jsp?num1="
                                + num1 + "&" + "num2=" + num2;
                        HttpClient httpClient = new DefaultHttpClient();
                        HttpGet httpGet = new HttpGet(url);
                        try {
                            httpClient.execute(httpGet);
                        } catch (ClientProtocolException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
                });
                result.setText(new Double(resultnum).toString());
                break;

            default:
                break;
            }
        };
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etx1 = (EditText) findViewById(R.id.num1);
        etx2 = (EditText) findViewById(R.id.num2);
        result = (TextView) findViewById(R.id.result);
        getresult = (Button) findViewById(R.id.btn);

        getresult.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                num1 = Double.parseDouble(etx1.getText().toString());
                num2 = Double.parseDouble(etx2.getText().toString());
                resultnum = num1 + num2;
                // TODO Auto-generated method stub
                new Thread(new Runnable() {

                    public void run() {
                        // TODO Auto-generated method stub
                        Message message = new Message();
                        message.what = UPDATE_TEXT;
                        handler.sendMessage(message);
                    }
                }).start();
            }
        });

    }
}

当我在Chrome中导航到http://localhost:8080/test/时,我看到<body> <h1>Using GET Method to Read Form Data</h1> <ul> <li><p> <b>Num1:</b> <%=request.getParameter("num1")%> </p></li> <li><p> <b>Num2:</b> <%=request.getParameter("num2")%> </p></li> <li><p> <b>result:</b> <%=request.getParameter("num2")+request.getParameter("num1")%> </p></li> </ul> 。为什么值没有正确填写?

2 个答案:

答案 0 :(得分:0)

您的服务器在localhost上运行,因此您的设备无法连接到服务器。将服务器设置为在0.0.0.0上运行,然后在代码中将URL更改为计算机的IP地址而不是localhost。要查找IP地址,请转到命令提示符并键入ipconfig。然后寻找你的ipv4地址。

答案 1 :(得分:0)

您的代码未输入onCLick方法,这就是您的值始终为null的原因。将您的代码转换为此代码:

cout << "Input your character" << endl;
char ch;
cin >> ch;

我认为这可以解决您的问题。