2013-04-12 40 views
0

我有一個asyncTask像這樣的:JSON解析器返回一些HTML/CSS的東西,而不是實際的JSON字符串

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.util.ArrayList; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.DialogInterface.OnCancelListener; 
import android.os.AsyncTask; 
import android.util.Log; 

public class OfficeJSONParser extends AsyncTask<String, String, JSONObject> { 

    private ProgressDialog progressDialog; 
    InputStream inputStream = null; 
    String result = ""; 
    Context c; 
    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 
    String url; 

    public OfficeJSONParser(Context c, String url) { 
     this.c = c; 
     this.url = url; 
    } 

    protected void onPreExecute() { 
     progressDialog = new ProgressDialog(c); 
     progressDialog.setMessage("Downloading your data..."); 
     progressDialog.show(); 
     progressDialog.setOnCancelListener(new OnCancelListener() { 
      public void onCancel(DialogInterface arg0) { 
       OfficeJSONParser.this.cancel(true); 
      } 
     }); 
    } 

    @Override 
    protected JSONObject doInBackground(String... params) { 
     return getJSONFromUrl(url); 
    } 

    protected void onPostExecute(JSONObject jObj) { 
     JSONObject json = jObj; 
    } 

    public JSONObject getJSONFromUrl(String url) { 

     // Making HTTP request 
     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      is = httpEntity.getContent(); 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      BufferedReader reader = new BufferedReader(new InputStreamReader(
        is, "UTF-8"), 8); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
      is.close(); 
      json = sb.toString(); 
     } catch (Exception e) { 
      Log.e("Buffer Error", "Error converting result " + e.toString()); 
     } 

     // try parse the string to a JSON object 
     try { 
      jObj = new JSONObject(json.substring(json.indexOf("{"), 
        json.lastIndexOf("}") + 1)); 
     } catch (JSONException e) { 
      Log.e("JSON Parser", "Error parsing data " + e.toString()); 
     } 
     // return JSON String 
     return jObj; 

    } 
} 

我知道,所提供的URL是一個有效的JSON文件,但是我解析器返回此:

{"MARGIN-LEFT":"30px","PADDING-BOTTOM":"2em","FONT-SIZE":"0.7em"} 

爲什麼?發生什麼事?

編輯:

的JSON是挪威人,但結構很容易理解:

http://data.helsenorge.no/External.svc/Services/KA02/10.75/59.91

+0

對我來說看起來像是有效的JSON。什麼是URL? –

+0

那麼,***是***有效的JSON。你想什麼,而不是內容明智?此外,您忘記檢查您的成功響應狀態。 – Perception

+0

看到編輯,有鏈接。 –

回答

1

我編譯了你的代碼並運行它,我不確定爲什麼其他答案已被低估,除了沒有給出關於該主題的許多信息。我將您的HttpPost更改爲HttpGet,並且我返回給我的JSON字符串確實是該網站的JSON內容。只要改變:

HttpPost httpPost = new HttpPost(url); 

HttpGet httpGet = new HttpGet(url); 

和日誌或調試的JSON字符串你從

json = sb.toString(); 

得到,你應該有你想要什麼。所有你需要做的就是將JSON字符串解析成一個JSON對象,然後你就在路上。

+0

我不知道爲什麼我的回答是downvoted,我寫的相同.. :) –

0

我不知道這是什麼原因,但你真正想做的事情http post?如果我看得很清楚,你只是想打電話給一個服務。爲什麼不使用HttpGet?

相關問題