我们使用异步任务进行服务调用,这个服务是由第三方调用的,我需要调用并获取json数据,然后根据需要移动其他东西,因此我使用了以下代码。
private class FetchJsonData extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
try {
// Construct the URL for the OpenWeatherMap query
// Possible parameters are avaiable at OWM's forecast API page, at
// http://openweathermap.org/API#forecast
URL url = new URL("android-app://ir.com.irremote.ir.com.irremote.activity/http/" + "api.abc.com/TVlistings/v9/listings/services/postalcode/37215/info?locale=en-US&countrycode=US&format=json&apikey=abc");
urlConnection = (HttpURLConnection) url.openConnection();
// Create the request to OpenWeatherMap, and open the connection
urlConnection.setRequestMethod("GET");
urlConnection.connect();
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
forecastJsonStr = buffer.toString();
return forecastJsonStr;
} catch (IOException e) {
Log.e("PlaceholderFragment", "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("PlaceholderFragment", "Error closing stream", e);
}
}
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.i("json", "" + s);
}
}在那之后,我执行了onCreate(),但是它给了我一些例外,我不知道它为什么会出现,.i在这里放置日志,请看这个:
致命异常:主进程: ir.dumadu.com.irremote,PID: 20759 java.lang.RuntimeException:无法停止活动java.lang.IllegalArgumentException: AppIndex: URI主机必须匹配包名并遵循格式(android:/ host _path)。提供URI: android-app://ir.dumadu.com.irremote.ir.dumadu.com.irremote.activity/http/host/path at android.app.ActivityThread.handleSleeping(ActivityThread.java:4679)在android.app.ActivityThread.access$3400(ActivityThread.java:211) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1934) at android.os.Handler。android.app.ActivityThread.main(ActivityThread.java:6946) dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145)在java.lang.reflect.Method.invoke(Method.java:372)的java.lang.reflect.Method.invoke(原生方法)在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)由: java.lang.IllegalArgumentException: AppIndex引起: URI主机必须匹配包名并遵循格式(android:/AppIndex_path)。提供的URI: android-app://ir.dumadu.com.irremote.ir.dumadu.com.irremote.activity/http/host/path at com.google.android.gms.internal.zzju.zzb(Unknown Source)(在com.google.android.gms.internal.zzju.zza(Unknown来源)在com.google.android.gms.internal.zzjt.zza(Unknown来源)(在com.google.android.gms.internal.zzju.zza(Unknown来源)在com.google.android.gms.internal.zzju.end(Unknown来源)在ir.dumadu.com.irremote.ir.dumadu.com.irremote.activity.ChoseTransmitter.onStop(ChoseTransmitter.java:169) at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1305) at androidandroid.app.ActivityThread.handleSleeping(ActivityThread.java:4676) at android.app.ActivityThread.access$3400(ActivityThread.java:211的.app.Activity.performStop(Activity.java:6777))在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1934)(android.os.Handler.dispatchMessage(Handler.java:102))在android.app.ActivityThread.main(ActivityThread.java:6946)的android.os.Looper.loop(Looper.java:145)在java.lang.reflect.Method.invoke(本地方法) 在这里,无论我使用这个url,也就是我的后端url。这个网址给了我们一些服务,provider.when,我在网络浏览器中点击这个网址,然后当我通过移动应用程序打电话给我时,它给了我json格式的data.but,它给了我上面的log.if,人们在这方面发现了任何错误,或者我需要做一些其他的方式,你一定要回答我。提前谢谢。
发布于 2016-08-25 07:12:06
private class FetchJsonData extends AsyncTask<Void, Void, String> {
@Override
protected String doInBackground(Void... params) {
// These two need to be declared outside the try/catch
// so that they can be closed in the finally block.
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
// Will contain the raw JSON response as a string.
String forecastJsonStr = null;
try {
// Construct the URL for the OpenWeatherMap query
// Possible parameters are avaiable at OWM's forecast API page, at
String urls="http://" + "api.abc.com/TVlistings/v9/listings/services/postalcode/37215/info?locale=en-US&countrycode=US&format=json&apikey=abc";
URL url = new URL(urls);
urlConnection = (HttpURLConnection) url.openConnection();
// Create the request to OpenWeatherMap, and open the connection
urlConnection.setRequestMethod("GET");
urlConnection.connect();
//urlConnection.setDoOutput(true);
//urlConnection.setDoInput(true);
// Read the input stream into a String
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
if (inputStream == null) {
// Nothing to do.
return null;
}
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
// Since it's JSON, adding a newline isn't necessary (it won't affect parsing)
// But it does make debugging a *lot* easier if you print out the completed
// buffer for debugging.
buffer.append(line + "\n");
}
if (buffer.length() == 0) {
// Stream was empty. No point in parsing.
return null;
}
forecastJsonStr = buffer.toString();
return forecastJsonStr;
} catch (IOException e) {
Log.e("PlaceholderFragment", "Error ", e);
// If the code didn't successfully get the weather data, there's no point in attemping
// to parse it.
return null;
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
if (reader != null) {
try {
reader.close();
} catch (final IOException e) {
Log.e("PlaceholderFragment", "Error closing stream", e);
}
}
}
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Log.i("json", "" + s);
}
}现在是工作了。
https://stackoverflow.com/questions/39138361
复制相似问题