2013-02-22 29 views
0

這段代碼適用於較老的api版本,例如。我已經在API8-2.2上使用它,並且它做我想要的。如何使JSONParser類適用於Android ICS和更新版本?

但是,當試圖運行與Android 4.2.1我的手機上的應用程序,它只是墜毀。我試圖在互聯網上發現這個問題,發現我需要Asyntask,所以我開始尋找它,但我仍然不明白。有人可以幫我解決這個問題嗎?

package com.thevnkid93.ucebnice; 

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

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.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.client.utils.URLEncodedUtils; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.util.Log; 

    public class JSONParser { 

     static InputStream is = null; 
     static JSONObject jObj = null; 
     static JSONArray jArr=null; 
     static String json = ""; 

     // function get json from url 
     // by making HTTP POST or GET method 
     public static JSONArray makeHttpRequest(String url, String method, 
       List<NameValuePair> params) { 

      // Making HTTP request 
      try { 
       // check for request method 
       if(method == "POST"){ 
        // request method is POST 
        // defaultHttpClient 
        DefaultHttpClient httpClient = new DefaultHttpClient(); 
        HttpPost httpPost = new HttpPost(url); 
        httpPost.setEntity(new UrlEncodedFormEntity(params,"utf-8")); 

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

       }else if(method == "GET"){ 
        // request method is GET 
        DefaultHttpClient httpClient = new DefaultHttpClient(); 
        String paramString = URLEncodedUtils.format(params, "utf-8"); 
        url += "?" + paramString; 
        HttpGet httpGet = new HttpGet(url); 

        HttpResponse httpResponse = httpClient.execute(httpGet); 
        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, "iso-8859-1"), 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 { 
       jArr = new JSONArray(json); 
      } catch (JSONException e) { 
       Log.e("JSON Parser", "Error parsing data " + e.toString()); 
      } 

      // return JSON String (Array) 
      return jArr; 

     } 

} 

的logcat:

02-23 23:01:10.540:E/AndroidRuntime(30520):android.os.NetworkOnMainThreadException

+3

檢查LogCat以查看Java堆棧跟蹤告訴您有關「崩潰」的信息。 – CommonsWare 2013-02-22 20:24:01

+0

02-23 23:01:10.540:E/AndroidRuntime(30520):android.os.NetworkOnMainThreadException – vnkid 2013-02-23 22:05:00

+0

如異常所示,您正嘗試在主應用程序線程上執行網絡I/O。請按照Raghav Sood的建議,將此I/O移至後臺線程,例如'AsyncTask'。 – CommonsWare 2013-02-24 01:35:03

回答

1

看你的代碼,這將是非常容易轉換它到AsyncTask。只需將JSONParser. makeHttpRequest()放入AsyncTask的doInBackground()方法即可。

但是,您可能會因使用Thread而逃脫,因爲您沒有對代碼進行任何UI修改。

Thread thread = new Thread() { 
    @Override 
    public void run() { 
     try { 
      JSONArray myArray = JSONParser. makeHttpRequest(); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
    } 
}; 

thread.start(); 
+0

Thx Raghav Sood,我已經移動了JSONParser。 makeHttpRequest()進入doInBackground()...但還有另一個問題。我無法使用Post方法將params發送到webservice。你可以在這裏看到我的另一篇文章: http://stackoverflow.com/questions/15101146/post-method-from-android-doesnt-work – vnkid 2013-02-26 23:05:40

相關問題