0

我目前正在尝试 DL 一些 twitter 信息,由于某种原因,我使用的代码不会返回数据。

当我在资源管理器中手动使用 URL 时,我确实得到了数据。但是当我尝试让 Android 去做时,我得到了一个例外。

我找到了两种尝试获取数据的方法。第一个如图所示。第二个是注释掉注释上方的三行并取消注释其他行。

之后我将使用简单的 JSON 来解析字符串。

我希望我已经充分解释了我的问题。欢迎任何意见。

import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String searchUrl = "http://search.twitter.com/search.json?q=@aplusk&rpp=100&page=1";


    HttpClient client = new  DefaultHttpClient();
    HttpGet get = new HttpGet(searchUrl);

    ResponseHandler<String> responseHandler = new BasicResponseHandler();

    String responseBody = null;
    Log.d("log", "test"+responseBody);
    try{
        responseBody = client.execute(get, responseHandler);
        HttpResponse response = client.execute(get);
        responseBody =  response.toString();


//          HttpResponse response = client.execute(get);
//          InputStream inputStream = response.getEntity().getContent();
//          responseBody = inputStream.toString();

        Log.d("log", "test2"+responseBody);

    }catch(Exception ex) {
        Log.d("log", "nope");
        ex.printStackTrace();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}


}
4

1 回答 1

1

我不确定您的例外是什么,但您的第一个问题可能是您正在主线程上进行网络活动。您必须启动一个新线程或使用Async Task

您的第二个问题是您不正确地将响应转换为字符串。要获取实际的内容字符串,您必须遍历 InputStream。这是我的首选方法

另外,请确保<uses-permission android:name="android.permission.INTERNET" />在您的AndroidManifest.xml文件中。

试试这个:

import java.io.InputStream;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        (new Thread(new Runnable() {

            @Override
            public void run() {
                String searchUrl = "http://search.twitter.com/search.json?q=@aplusk&rpp=100&page=1";
                HttpClient client = new DefaultHttpClient();
                HttpGet get = new HttpGet(searchUrl);

                String responseBody = null;
                Log.d("log", "test" + responseBody);
                try {
                    HttpResponse response = client.execute(get);
                    InputStream inputStream = response.getEntity().getContent();
                    // responseBody = inputStream.toString();
                    responseBody = streamToString(inputStream);

                    Log.d("log", "test2" + responseBody);

                } catch (Exception ex) {
                    Log.d("log", "nope");
                    ex.printStackTrace();
                }

            }
        })).start();
    }

    public static String streamToString(InputStream is) {
        java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A");
        return s.hasNext() ? s.next() : "";
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}
于 2013-04-06T22:24:07.220 回答