首页新闻找找看学习计划

android如何向grails后台传值?

0
[待解决问题]
package com.zhijin.app;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AppTest extends Activity implements OnClickListener {

    EditText username, password;
    Button jsonBtn, xmlBtn, cancleBtn;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        username = (EditText) findViewById(R.id.userName);
        password = (EditText) findViewById(R.id.passWord);
        jsonBtn = (Button) findViewById(R.id.jsonLogin);
        xmlBtn = (Button) findViewById(R.id.xmlLogin);
        cancleBtn = (Button) findViewById(R.id.cancleLogin);
        jsonBtn.setOnClickListener(this);
        xmlBtn.setOnClickListener(this);
        cancleBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.jsonLogin:
            String strUrl = "http://123.3.3.4:8080/apptest/member/login";
            URL url = null;
            try {
                url = new URL(strUrl);
                HttpURLConnection urlConn = (HttpURLConnection) url
                        .openConnection();
                urlConn.setRequestMethod("POST");
                urlConn.setDoInput(true);
                urlConn.setDoOutput(true);
                urlConn.setUseCaches(false);
                urlConn.setRequestProperty("Content-Type",
                        "application/x-www-form-urlencoded");
                urlConn.setRequestProperty("charset", "utf-8");
                urlConn.connect();

                DataOutputStream dop = new DataOutputStream(
                        urlConn.getOutputStream());
                dop.writeBytes("params:userName=liting");

                dop.flush();
                dop.close();

                BufferedReader bufferReader = new BufferedReader(
                        new InputStreamReader(urlConn.getInputStream()));
                String result = "";
                String readLine = null;
                while ((readLine = bufferReader.readLine()) != null) {
                    result += readLine;
                    Log.i("info", result);
                }
                bufferReader.close();
                urlConn.disconnect();
                Toast.makeText(AppTest.this, result, Toast.LENGTH_LONG).show();
            } catch (MalformedURLException e) {
                e.printStackTrace();
                Log.e("info", "" + e.toString());
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("info", "" + e.toString());
            }
            break;
        case R.id.xmlLogin:
            break;
        case R.id.cancleLogin:
            finish();
            break;
        }
    }

}

这是我写的客户端代码,服务端利用的是grails编程来接收数据。

def login ={
println "params.userName1===" + params.userName

def member = Member.find("from Member h where h.userName = ? ",[params.userName])
println "member2==" + member
if (member){
if(params.mode == "1"){
render member as JSON

}else{
render member as XML

}

}else{
render "false"
}
}

 

我的值一直传不到服务端,哪位大神给我解决一下

mitty的主页 mitty | 菜鸟二级 | 园豆:207
提问于:2012-09-07 16:52
< >
分享
所有回答(1)
0

换了一个类,将DataOutputStream换成了OutputStreamWriter,调用.write("userName="+username.getText().toString());方法传值就可以了

,很想知道原因啊!

mitty | 园豆:207 (菜鸟二级) | 2012-09-07 17:28
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册