我目前正在尝试DL一些推特信息,出于某种原因,我使用的代码不会返回数据。
当我在资源管理器中手动使用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;
}
}
答案 0 :(得分:1)
我不确定您的异常是什么,但您的第一个问题可能是您正在主线程上进行网络活动。您必须启动新主题或使用Async Task。
你的第二个问题是你不正确地将响应转换为字符串。要获取实际的内容字符串,您必须迭代InputStream。 This is my preferred method
另外,请确保<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;
}
}