2013-08-08 13 views
6

मैं कुछ JSON डेटा को पार्स करने का प्रयास कर रहा हूं। मेरा कोड थोड़ी देर के लिए काम कर रहा था, और मुझे यकीन नहीं है कि मैंने अचानक कोड को तोड़ने के लिए क्या बदल दिया। जब मैं अपना कोड चलाता हूं तो मुझे कोई रनटाइम त्रुटियां या चेतावनियां नहीं मिल रही हैं। मैं एक नया AsyncTask बनाते हैं और इसे निष्पादित करता हूं। जब मैं इस नए कार्य पर .get() पर कॉल करता हूं, तो डीबगर इस लाइन पर स्टाल करता है और 30 मिनट से अधिक समय लेता है। मैं इस कार्य को पूरा करने के लिए डीबगर या चलाने के दौरान नहीं मिला है।एंड्रॉइड पार्स JSON कार्य प्राप्त करने पर अटक गया

JSON:

protected void setUp(Context context) { 
    _context = context; 
    getConfig(); 
} 

// get config file 
protected void getConfig() { 
    if (config != null) 
     return; 

    config = new Config(); 

    String url = configURL; 
    AsyncTask<String, Integer, JSONObject> jsonTask = new DownloadJSONTask() 
      .execute(url); 
    JSONObject configItem = null; 
    try { 
     configItem = jsonTask.get(); //debugger pauses here 
     if (configItem == null) 
      return; 
     config.configVersion = configItem.getString("field_configversion"); 
     config.currentAppVersion = configItem 
       .getString("field_currentappversion"); 
     config.getSupportURL = configItem.getString("field_getsupporturl"); 
     config.getCatalogURL = configItem.getString("field_getcatalogurl"); 
     config.getDataVersion = configItem.getString("field_dataversion"); 
     config.getDataUrl = configItem.getString("field_dataurl"); 
     config.getDataApiKey = configItem.getString("field_dataapikey"); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
     System.err.println("Download of config interrupted"); 
    } catch (ExecutionException e) { 
     e.printStackTrace(); 
     System.err.println("Download of config failed to execute"); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 

    cacheStaticData(_context); 
} 

DownloadJSONTask.java

package com.example.simplegraph; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.content.Context; 
import android.os.AsyncTask; 

public class DownloadJSONTask extends AsyncTask<String, Integer, JSONObject> { 
private HttpClient client = new DefaultHttpClient(); 
private HttpGet request; 
private HttpResponse response; 

DownloadJSONTask() { 
    super(); 
} 

// tries to grab the data for a JSONObject 
@Override 
protected JSONObject doInBackground(String... urls) { 

    request = new HttpGet(urls[0]); 
    try { 
     response = client.execute(request); 
     HttpEntity entity = response.getEntity(); 
     if (entity != null) { 
      InputStream instream = entity.getContent(); 
      String result = convertStreamToString(instream); 
      JSONObject json = new JSONObject(result); 
      instream.close(); 
      return json; 
     } 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    return null; 
} 

// converts the InputStream to a string and add nl 
private String convertStreamToString(InputStream is) { 
    BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
    StringBuilder sb = new StringBuilder(); 
    String line = null; 
    try { 
     while ((line = br.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
    } catch (IOException ioe) { 
     ioe.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException ioe) { 
      ioe.printStackTrace(); 
     } 
    } 
    return sb.toString(); 
} 
} 

और HomeActivity.java

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_home); 

    new AddStringTask().execute(); 

} 

class AddStringTask extends AsyncTask<Void, String, Void> { 
    @Override 
    protected Void doInBackground(Void... unused) { 

     app = (EconApplication) getApplication(); 
     getApp().setUp(HomeActivity.this); 
     HomeActivity.this.setUpDrawer(); 
     return (null); 
    } 

    @Override 
    protected void onPostExecute(Void unused) { 
     setUpDataDisplay(); 
     setUpGraphRange(); 
     createTable(); 
     createGraph(-1); 
    } 
} 

प्रश्न: क्यों मेरी कोड .Get पर अटक हो रही है()?

+0

"अटक" का अर्थ है कि कौन सा logcat-output? – bofredo

+0

यह विधि कहां है() 'और यह क्या करती है? – Raghunandan

+0

@Raghunandan get() कोड के पहले सेट में है। कोशिश करने के बाद बस। और इसका उपयोग JSON प्राप्त करने के लिए किया जाता है। – buczek

उत्तर

1

AsyncTask.get() ब्लॉक फोन करने वाले धागा। इसके बजाय AsyncTask.execute() का उपयोग करें।

public final Result get()

जोड़ा गया एपीआई स्तर 3

वेट्स आवश्यक हो तो गणना को पूरा करने के लिए में, और उसके बाद उसके परिणाम प्राप्त करता है।

गणना परिणाम प्रस्तुत करता है।

से

How do I return a boolean from AsyncTask?

आकर्षित नीचे

new DownloadJSONTask(ActivityName.this).execute(url); 

प्रयास करें अपने DownloadJSONTask में

construcotr में

TheInterface listener; 
    public DownloadJSONTask(Context context) 
{ 

    listener = (TheInterface) context; 

} 

इंटरफ़ेस

public interface TheInterface { 

    public void theMethod(ArrayList<String> result); // your result type 

    } 

अपने doInbackground लौट परिणाम में। मैं अपने स्ट्रिंग प्रकार के ऐरेलिस्ट को मान रहा हूं। अपनी आवश्यकता के अनुसार क्या सरणी सूची बदलें।

अपने onPostExecute

if (listener != null) 
    { 
     listener.theMethod(result); // result is the ArrayList<String> 
     // result returned in doInbackground 
     // result of doInbackground computation is a parameter to onPostExecute 
    } 

अपनी गतिविधि वर्ग में इंटरफ़ेस

public class ActivityName implements DownloadJSONTask.TheInterface 

फिर

@Override 
public void theMethod(ArrayList<String> result) { // change the type of result according yo your requirement 
// use the arraylist here 
} 

लागू संपादित करें: वैकल्पिक

आप कर सकते हैं आपकी एसिंक्टास्क आपकी गतिविधि कक्षा का एक आंतरिक वर्ग है। doInbackground गणना पर परिणाम onPostExecute पर एक पैरामीटर है। परिणाम doInbackground में परिणाम। onPostExecute में ui अपडेट करें।

+0

फिर से धन्यवाद, यह बहुत उपयोगी है। – buczek

+0

@buczek आपका स्वागत है और खुशी है कि यह आपकी मदद करता है। – Raghunandan

1

आप बहुत droidQuery लाइब्रेरी का उपयोग कर सब कुछ आसान बनाने में कर सकते हैं:

$.getJSON("http://www.example.com", null, new Function() {//this will run using an AsyncTask, get the JSON, and return either a JSONObject or JSONArray on the UI Thread. 
    @Overrde 
    public void invoke($ droidQuery, Object... params) { 
     if (params[0] instanceof JSONObject) { //it's often ok just to assume a JSONObject, making your first line simply: JSONObject obj = (JSONObject) params[0]; 
      //JSONObject is returned 
      JSONObject json = (JSONObject) params[0]; 
      //to easily parse this Object, convert it to a map first: 
      Map<String, ?> map = $.map(json); 
      //then you can just make a call like this: 
      if (map.contains("field_currentappversion")) { 
       config.currentAppVersion = (String) map.get("field_currentappversion"); 
      } 
     } 
     else { 
      //JSONArray is returned 
      JSONArray json = (JSONArray) params[0]; 
      //if you got an array, you can easily convert it to an Object[] for parsing: 
      Object[] array = $.makeArray(json); 
     } 
    } 
}); 
+0

यह मेरी समस्या का समाधान नहीं करता है, इसके बजाय आप सुझाव दे रहे हैं कि मैं कुछ अन्य लाइब्रेरी का उपयोग करता हूं। – buczek

संबंधित मुद्दे