2016-08-25 71 views
0

我們正在使用服務調用異步任務這項服務是由第三方,我需要調用並獲取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你發現任何錯誤,或者我需要做一些其他的方式,你肯定回覆我。 在此先感謝。

+0

你需要添加有效的URL字符串作爲URL例如URL =新的URL( 「http://fb.com/」); –

+0

現在我得到了解決方案:第一,我們需要創建字符串變量到特定的網址。喜歡。 String urlStr =「http://whatever.com」; URL url = new URL(urlStr);然後它的工作 – user6615010

回答

0
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); 
    } 
} 

現在,它的工作。

+1

雖然它可能工作,但它總是很好的在描述中包含您所做的更改以及它們如何解決問題,以便OP和其他人可以獲得更清晰的圖像。此外,在包含代碼時,它最大程度地足以在答案中僅包含相關行。 – Shaishav

+0

@Shaishav我已經評論問題部分我改變了什麼。 – user6615010

+0

酷...我建議只包含答案描述和答案。 – Shaishav

相關問題