httpclient.execute(httpget) 似乎不起作用 (Android)
httpclient.execute(httpget) Doesn't seem to work (Android)
我正在尝试从
获取每日报价
http://quotesondesign.com/api/3.0/api-3.0.json?callback=json
我在 onCreate 中调用了这个方法
但是当我尝试执行 httpclient.execute();
它转义到 catch 语句...
我做错了什么?
我确实包含了 <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);
}
提前致谢!
在你的代码中你有
String quoteUrl = "http://quotesondesign.com/api/3.0/api-3.0.json?callback=?";
而您要获取的URL是
http://quotesondesign.com/api/3.0/api-3.0.json?callback=json
请注意在您的代码中您如何使用 callback=?
而 URL 具有 callback=json
.
在Android 4.2之后,您不能在UI-Thread("main"线程)上进行Http Request。您需要在单独的线程中执行此操作。
你可以找到一个例子on this website or in this Whosebug post:
更新:现在可用代码的实际答案:
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 捕获了它,但它在日志中)读取类似 "Network on Main Thread"?
因为那是你想做的事,而那是你不被允许做的事。相反,将其放在 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);
}
}
我正在尝试从
获取每日报价http://quotesondesign.com/api/3.0/api-3.0.json?callback=json
我在 onCreate 中调用了这个方法 但是当我尝试执行 httpclient.execute(); 它转义到 catch 语句...
我做错了什么?
我确实包含了 <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);
}
提前致谢!
在你的代码中你有
String quoteUrl = "http://quotesondesign.com/api/3.0/api-3.0.json?callback=?";
而您要获取的URL是
http://quotesondesign.com/api/3.0/api-3.0.json?callback=json
请注意在您的代码中您如何使用 callback=?
而 URL 具有 callback=json
.
在Android 4.2之后,您不能在UI-Thread("main"线程)上进行Http Request。您需要在单独的线程中执行此操作。
你可以找到一个例子on this website or in this Whosebug post:
更新:现在可用代码的实际答案:
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 捕获了它,但它在日志中)读取类似 "Network on Main Thread"?
因为那是你想做的事,而那是你不被允许做的事。相反,将其放在 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);
}
}