从android中的url获取数据

时间:2011-06-30 09:46:08

标签: android url emulation fetch

我是android的新手。我的applcation将查询到一个url并将收到json响应。 但是我无法从网址中获取数据。我在代理服务器后面..所以更新了模拟器中的APN设置,内置的浏览器工作正常。

我使用以下代码获取数据......:

package com.android.urlfetch;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;


 public class UrlfetchActivity extends Activity {
Button but1;
String result ;
private TextView text;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    try{
        result = getStringContent("http://www.google.com");
    }catch (Exception e) {
        // TODO: handle exception
        result = "Error";
    }

    text = (TextView)findViewById(R.id.text);
    text.setText(result);


}
 url=http://voxpopis.com");


public static String getStringContent(String uri) throws Exception {

    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet();
        request.setURI(new URI(uri));
        HttpResponse response = client.execute(request);
        InputStream ips  = response.getEntity().getContent();
        BufferedReader buf = new BufferedReader(new InputStreamReader   (ips,"UTF-8"));

        StringBuilder sb = new StringBuilder();
        String s;
        while(true )
        {
            s = buf.readLine();
            if(s==null || s.length()==0)
                break;
            sb.append(s);

        }
        buf.close();
        ips.close();
        return sb.toString();

    } 
    finally {

    }
} 
}

我试图在堆栈溢出中得到类似问题但是dint得到任何答案。任何帮助将不胜感激。

我的logcat响应是:

06-30 15:14:08.820: WARN/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 601 uid 10037
06-30 15:14:10.800: WARN/PackageManager(58): Code path for pkg : com.android.urlfetch   changing from /data/app/com.android.urlfetch-1.apk to /data/app/com.android.urlfetch-2.apk
06-30 15:14:10.800: WARN/PackageManager(58): Resource path for pkg : com.android.urlfetch changing from /data/app/com.android.urlfetch-1.apk to /data/app/com.android.urlfetch-2.apk
06-30 15:14:12.250: WARN/RecognitionManagerService(58): no available voice recognition services found
06-30 15:14:25.629: WARN/ActivityManager(58): Launch timeout has expired, giving up wake lock!
06-30 15:14:26.257: WARN/ActivityManager(58): Activity idle timeout for HistoryRecord{44fd4af8 com.android.urlfetch/.UrlfetchActivity}
06-30 15:14:36.911: WARN/IInputConnectionWrapper(539): showStatusIcon on inactive InputConnection

此代码在直接连接中运行良好....但我在代理网络中......它不能在代理网络中工作。我应该在代理网络中进行哪些设置?

1 个答案:

答案 0 :(得分:0)

为了非常舒服地从网上获取数据,你可以使用Jsoup.To看看如何在Android中使用模拟器点击here