Java Android web请求字符串

Java Android web请求字符串,java,android,string,request,Java,Android,String,Request,我想从网页中收集文本,将其放入字符串中,然后在设备屏幕上显示 这是我的WebRequest活动: package com.work.webrequest; import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.client.HttpClient; import org.apache.http.clien

我想从网页中收集文本,将其放入字符串中,然后在设备屏幕上显示

这是我的WebRequest活动:

package com.work.webrequest;

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class WebRequest extends Activity {


    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView txt = (TextView) findViewById(R.id.textView1);
        txt.setText(getPage());
    }

    private String getPage() {
        String str = "***";

        try
        {
            HttpClient hc = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://zapmenow.co.uk/zapme/?getDetails=true&secret=zjXvwX5frK1po0adXyKJsbbyUe2ZY2PkW9M8r7sb1soIDppIWdTlgt1xmL5VM6g&UDID=401ceca29af68e4569a25e8c16a6987bb8cf1f5a&id=41");

            HttpResponse rp = hc.execute(post);

            if(rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            {
                str = EntityUtils.toString(rp.getEntity());
            }
        }catch(IOException e){
            e.printStackTrace();
        }  

        return str;
    }


}
main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView android:layout_height="wrap_content" 
    android:id="@+id/textView1" 
    android:text="" 
    android:layout_width="wrap_content"></TextView>
</LinearLayout>

我在Eclipse中没有任何错误,但应用程序在我的设备上崩溃

附言:我加了一行

 <uses-permission android:name="android.permission.INTERNET" />


在清单中,因此Internet权限不是问题。

这是适用于我的代码:

private String getPage(String url) {
    HttpURLConnection con = (HttpURLConnection) new URL(url).openConnection();
    con.connect();

    if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {
        return inputStreamToString(con.getInputStream());
    } else {
        return null;
    }
}

private String inputStreamToString(InputStream in) throws IOException {
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(in));
    StringBuilder stringBuilder = new StringBuilder();
    String line = null;

    while ((line = bufferedReader.readLine()) != null) {
        stringBuilder.append(line + "\n");
    }

    bufferedReader.close();
    return stringBuilder.toString();
}
稍后,您可以通过以下方式使用它:

String response = getPage("http://example.com");

你应该在这里发布你的logcat输出。因为应用程序正在崩溃,所以一定有堆栈跟踪。提示:我身边有这个代码片段,所以我把它给了你。在发布问题之前,您应该始终检查logcat输出。您不能否认没有编译错误,但它会在设备上崩溃