我们正在使用服务调用异步任务这项服务是由第三方,我需要调用并获取JSON数据,然后根据我需要移动其他事情,所以我用这个代码。MalformedURLException:?
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);
}
}
之后,我EXCUTE上的onCreate(),但它给了我一些例外,我不知道为什么它的到来。我把这里登录,请看到这一点:
致命异常:主要 过程:ir.dumadu.com.irremote,PID:20759 java.lang.RuntimeException:无法停止活动{ir.dumadu.com.irremote/ir.dumadu.com.irremote.ir.dumadu.com.irremote.activity。 ChoseTransmitter}:java.lang.IllegalArgumentException:AppIndex:URI主机必须匹配包名,并遵循格式(android-app://// [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) at android.app.ActivityThread.access $ 3400(ActivityThread.java:211) at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1934) at android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:6946) at java.lang.reflect.Method.invoke(Native Method) at java。 lang.reflect.Method.invoke(Method.java:372) at 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-app://// [host_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 ) at com.google.android.gms.internal.zzju.zza(Unknown Source) at com.google.android.gms.internal.zzjt.zza(Unknown Source) at com.google.android.gms.internal .zzju.zza(Unknown Source) at com.google.android.gms.internal.zzju.end(Unknown Source) at ir.dumadu.com.irremote.ir.dumadu.com.irremote.activity.ChoseTransmitter.onStop (ChoseTransmitter.java:169) at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1305) at andro android.app.ActivityThread.access中的id.app.Activity.performStop(Activity.java:6777) at android.app.ActivityThread.handleSleeping(ActivityThread.java:4676) at android.app.ActivityThread.access $ 3400(ActivityThread.java:211) at android .app.ActivityThread $ H.handleMessage(ActivityThread.java:1934) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:145) at android .app.ActivityThread.main(ActivityThread.java:6946) at java.lang.reflect.Method.invoke(Native Method)
这里有什么我使用这个网址ie m y后端网址。这个网址给了我们一些服务提供商。当我在网页浏览器中打开这个网址时,它给了我json格式化的数据。但是当我通过我们的移动应用程序调用它的时候给出了上面的log.if你发现任何错误,或者我需要做一些其他的方式,你肯定回复我。 在此先感谢。
你需要添加有效的URL字符串作为URL例如URL =新的URL( “http://fb.com/”); –
现在我得到了解决方案:第一,我们需要创建字符串变量到特定的网址。喜欢。 String urlStr =“http://whatever.com”; URL url = new URL(urlStr);然后它的工作 – user6615010