在Eclipse中如何将输入的信息发送到编辑文本字段中

时间:2012-03-03 21:34:30

标签: android

编码很新但到目前为止已经完成了很多工作我能够通过这行代码创建的应用程序向网站发送回声

String url  = "http://beatswith.us/mtest.php?username=";

在=之后的任何内容显示回来...我无法弄清楚如何将输入编辑文本字段的内容发送到此处的网址到目前为止的整个代码

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;





import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
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.TextView;

public class Loginretrieve extends Activity {

EditText getInput;
TextView textOut;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.logpage);


getInput = (EditText) findViewById(R.id.username);



String url  = "http://beatswith.us/mtest.php?username=";  

HttpGet httpGet = new HttpGet(url);
HttpClient httpClient = new DefaultHttpClient();
HttpResponse response1;
try {

    response1 = httpClient.execute(httpGet);

    Object responseString = getResponseString(response1);
    TextView textView = (TextView)findViewById(R.id.textView1);
    textView.setText(responseString.toString());


} catch (ClientProtocolException e1) {
    e1.printStackTrace();
} catch (IOException e1) {
    e1.printStackTrace();
} catch (IllegalStateException e) {  
    e.printStackTrace();
}


}

public static String getResponseString(HttpResponse response)
    throws IllegalStateException, IOException {

String responseString = "";
BufferedReader in = null;
try {
    in = new BufferedReader(new InputStreamReader(response.getEntity()
            .getContent()));
    StringBuffer sb = new StringBuffer("");
    String line = "";
    String NL = System.getProperty("line.separator");
    while ((line = in.readLine()) != null) {
        sb.append(line + NL);
        Log.d("beats.us.with", "Output");
    }
    in.close();
    responseString = sb.toString();

} finally {
    if (in != null) {
        try {
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
return responseString;
}
} 

1 个答案:

答案 0 :(得分:0)

您需要做的就是在文本字段中发送数据:

String url = "http://beatswith.us/mtest.php?username=" + URLEncoder.encode( textView.getText() );

如果您想将表单输入作为POST数据发送,请查看this