在Eclipse中如何显示来自url的检索信息

时间:2012-01-25 23:45:31

标签: android

所以我对编程非常陌生并且一直试图弄清楚如何将登录信息发送到网址并让用户登录并返回显示页面。我再一次非常新,但这是我的代码

public class LoginActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // setting default screen to login.xml
    setContentView(R.layout.login);

    final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.buttonclk);

     Button bhome1 = (Button) findViewById(R.id.btnLogin);
        bhome1.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                startActivity(new      
Intent("beats.us.with.Loginretrieve"));
                mpButtonClick.start();
            }
        } ) ;   



}

public void onClick(View v) {
    // TODO Auto-generated method stub

    TextView registerScreen = (TextView) findViewById(R.id.link_to_register);


    // Listening to register new account link
    registerScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Switching to Register screen
            Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
            startActivity(i);
        }
    });
}
}

其次是Loginretrive

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

公共类Loginretrieve扩展了Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    String url = "http://beatswith.us";


} 



}

再一次,我非常新,所以任何帮助都将是kewl 告诉我去android开发者页面阅读就像告诉我我已经知道的那就是为什么我在这里信任ppls知识

1 个答案:

答案 0 :(得分:0)

首先,您需要将internet权限添加到您的manifest.xml <uses-permission android:name="android.permission.INTERNET" />。 然后在您的Loginretrieve活动中,您需要创建您的请求(发布/获取请求),如下所示:

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

        response = httpClient.execute(httpGet);

        responseString = getResponseString(response);

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

getResponseString方法是:

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);
        }
        in.close();
        responseString = sb.toString();

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

就是这样....你已经提出了请求并检索了回复..希望这会有所帮助。