java - 对字符串的 Android Web 请求

标签 java android string request

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

这是我的 WebRequest Activity :

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;
    }


}

主.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 中没有任何错误,但应用程序在我的设备上崩溃了。

PS: 我添加了这行

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

在 list 中,所以互联网权限不是问题。

最佳答案

这是对我有用的代码:

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");

关于java - 对字符串的 Android Web 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7514828/

相关文章:

java - jaxb意外元素错误本地:root

java - 多线程通信: how good is the use of Atomic Variables like AtomicInteger? 为什么没有AtomicFloat?

objective-c - indexOfObject 不正确匹配

string - Google 脚本 - 从网站获取特定数据

Java:比较字符串时顺序有区别吗?

java - 如何通过实例化类来使多个 JButton 单独工作?

java - 让 Java Web Service 在不同的上下文中执行

安卓拦截 Intent

Android - Google maps Api v2 - 身份验证错误

java - 在 RecyclerView 滚动监听器中实现单击监听器