2017-05-04 138 views
-1

我爲獲取JSON對象而發出「GET」請求。當我嘗試讀取響應時,會引發錯誤。錯誤在頭文件中描述。 對我來說這真是個奇蹟,因爲當我在模擬器或瀏覽器上運行應用程序時,我得到了JSON響應,一切都很好。但是當我在真實手機上運行它時,我看到這個錯誤.... 將不勝感激任何答案。這是我的代碼。值類型java.lang.String的<html>無法轉換爲JSONObject

class Task extends AsyncTask<String, Void, String> { 
StringBuilder builder; 

@Override 
protected String doInBackground(String... params) { 
    HttpURLConnection connection; 
    try { 
     URL url = new URL(params[0]); 
     connection = (HttpURLConnection) url.openConnection(); 
     connection.setRequestMethod("GET"); 
     connection.connect(); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); 
     builder = new StringBuilder(); 
     String line; 
     while ((line = reader.readLine()) != null) { 
      builder.append(line); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Log.e("log", "background error " + e); 
    } 
    return builder.toString(); 
} 

@Override 
protected void onPostExecute(String s) { 

    Log.e("log", "response " + s); 

    try { 
     JSONObject jsonObject = new JSONObject(s); 
     Log.e("log", "JSON = " + jsonObject); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
     Log.e("log", "post error " + e); 
    } 

} 

}

這是字符串我在後期得到執行

<html>       <body>         
<script type='text/javascript' charset='utf-8'>          
window.location.href = '/';        </script>        
</body>      </html>       

這裏是錯誤我得到

05-04 08:56:19.989 2731-2731/? W/System.err: org.json.JSONException: Value 
<html> of type java.lang.String cannot be converted to JSONObject 
05-04 08:56:19.989 2731-2731/? W/System.err:  at 
org.json.JSON.typeMismatch(JSON.java:111) 
05-04 08:56:19.989 2731-2731/? W/System.err:  at org.json.JSONObject. 
<init>(JSONObject.java:158) 
05-04 08:56:19.989 2731-2731/? W/System.err:  at org.json.JSONObject. 
<init>(JSONObject.java:171) 
05-04 08:56:19.989 2731-2731/? W/System.err:  at 
com.example.dshahzadyan.gettest.Task.onPostExecute(MainActivity.java:64) 
05-04 08:56:19.999 2731-2731/? W/System.err:  at 
com.example.dshahzadyan.gettest.Task.onPostExecute(MainActivity.java:33) 
05-04 08:56:19.999 2731-2731/? W/System.err:  at 
android.os.AsyncTask.finish(AsyncTask.java:602) 
05-04 08:56:19.999 2731-2731/? W/System.err:  at 
android.os.AsyncTask.access$600(AsyncTask.java:156) 
05-04 08:56:19.999 2731-2731/? W/System.err:  at 
android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615) 
05-04 08:56:19.999 2731-2731/? W/System.err:  at 
android.os.Handler.dispatchMessage(Handler.java:99) 
05-04 08:56:19.999 2731-2731/? W/System.err:  at 
android.os.Looper.loop(Looper.java:137) 
05-04 08:56:19.999 2731-2731/? W/System.err:  at 
android.app.ActivityThread.main(ActivityThread.java:4517) 
05-04 08:56:19.999 2731-2731/? W/System.err:  at 
java.lang.reflect.Method.invokeNative(Native Method) 
05-04 08:56:19.999 2731-2731/? W/System.err:  at 
java.lang.reflect.Method.invoke(Method.java:511) 
05-04 08:56:19.999 2731-2731/? W/System.err:  at 
+0

你能發佈你的json響應嗎 – vishnumm93

+1

你得到的答案不僅僅是JSON,而是一個HTML文檔。 – Henry

+0

當您使用手機時,您會收到響應,該響應無法解析爲json對象。 –

回答

0

您得到的響應類型的HTML而不是JSON。所以,當HTML格式的響應字符串無法轉換爲json時,onPostExecute方法。確保你試圖打的網址是發送json而不是html

+0

謝謝你的回答,但問題是,響應是JSON格式。當我在仿真器上運行程序但不在真實設備上時,我以JSON格式獲得響應。 –

+0

如果我沒有嘗試使用其他URL發出請求,我會同意你的意見。效果是一樣的 –

相關問題