我试图从
获取每日报价我在onCreate中调用此方法 但是当我尝试执行httpclient.execute()时; 它逃到了捕获声明......
我做错了什么?
我确实包含了<uses-permission android:name="android.permission.INTERNET" />
在我的清单文件中。
public String getJson(){
String quoteUrl = "http://quotesondesign.com/api/3.0/api-3.0.json?callback=?";
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet(quoteUrl);
httpget.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
String aJsonString = null;
try {
HttpResponse response = httpclient.execute(httpget);
Toast.makeText(this, "It works", Toast.LENGTH_LONG).show();
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
JSONObject jObject = new JSONObject(result);
aJsonString = jObject.getString("quote");
} catch (Exception e) {
//Toast.makeText(this, "can't execute http request", Toast.LENGTH_LONG).show();
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return aJsonString;
}
编辑:这是onCreate()
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//verbergt notificatiebalk
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.splash);
jsonstring = getJson();
Log.d(jsonstring, "The jsonstring contains: " + jsonstring);
//Toast.makeText(this, jsonstring, Toast.LENGTH_LONG).show();
//tot hier testen
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
提前谢谢!
答案 0 :(得分:0)
在您的代码中
String quoteUrl = "http://quotesondesign.com/api/3.0/api-3.0.json?callback=?";
虽然您要提取的网址是
http://quotesondesign.com/api/3.0/api-3.0.json?callback=json
当网址为callback=?
时,请注意您的代码如何callback=json
。
答案 1 :(得分:0)
在Android 4.2之后,您无法在UI-Thread(&#34; main&#34;线程)上发出Http请求。你需要在一个单独的线程中完成它。
您可以在此stackoverflow帖子中找到示例on this website:HttpClient.execute(HttpPost) on Android 4.2 error
答案 2 :(得分:0)
更新:现在可以使用代码的实际答案:
private class AsyncQuoteDownload extends AsyncTask<Void, Void, String>{
@Override
protected String doInBackground(Void... params) {
String jsonData = getJson(); //or, if the jsonData var is available from everywhere, just put myR.run(); here, return null, and append the data directly in onPostExecute
return jsonData;
}
@Override
protected void onPostExecute(String result) {
(TextView)findViewById(R.id.Quote).append(result).append("\"");
} // \" makes it put an actual " inside a string
}
旧答案:
我打赌你的堆栈跟踪(因为oyu捕获它而不是错误,但它在日志中)读取类似“主线程上的网络”的内容?
因为那是你想要做的事情,而这是你不允许做的事情。相反,将它放在AsyncTask中:
onCreate(){ //beware pseudo code because it doesn't matter
//do stuff
setContentView(...); //Above here, everything stays as is.
//below here, only that:
new GetQuoteTask.execute();
}
class GetQuoteTask extends AsyncTask<Void, Void, String>{
String doInBackground(...){ //<- pseudo code, code completion is your friend
String result = getJson();
Log.d(jsonstring, "The jsonstring contains: " + jsonstring);
return result;
}
onPostExecute(String result){
maybePutYourStringSomewhereAKAUpdateUI();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
finish();
}
}, SPLASH_TIME_OUT);
}
}