2013-11-27 4 views
6

द्वारा 2 अलग-अलग URL से डेटा का विश्लेषण कैसे करें मेरे पास एक ऐप है जो JSON URL से डेटा प्राप्त करता है। यह एक यूआरएल के साथ पूरी तरह से काम करता है, लेकिन मुझे एक ही समय में दो JSON यूआरएल से डेटा प्राप्त करने की आवश्यकता है। एक यूआरएल से कुछ डेटा और दूसरे से कुछ की तरह। और उन्हें दो टेक्स्ट व्यू में दिखाएं।asyncTask विधि

मेरा ऐप है, यह वर्तमान में कोई डेटा लोड नहीं करता है।

MainActivity:

public class MainActivity extends Activity { 

//URL to get JSON Array 
private static String url1 = "http://api.worldbank.org/countries/de?format=json"; 
private static String url2 = "http://api.worldbank.org/countries/it?format=json"; 

//JSON Node Names 
private static final String CountryNAME1 = "name"; 
private static final String CountryNAME2 = "name"; 
JSONArray user = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    new GetJSONTask().execute(url1); 
    //new GETJSONTask().execute(url2); 
} 
class GetJSONTask extends AsyncTask<String, Void, JSONObject> { 


    protected JSONObject doInBackground(String... urls) { 
     // Creating new JSON Parser 
     JSONParser jParser = new JSONParser(); 

     // Getting JSON from URL 
     JSONObject json1 = jParser.getJSONFromUrl(url1); 
     JSONObject json2 = jParser.getJSONFromUrl(url2); 

     return json1; 
    } 
    protected void onPostExecute(JSONObject json1, JSONObject json2) { 
     //Getting JSON Array 
     try { 

      //For Country 1 
      // Get the array 
      JSONArray countryArC1 = json1.getJSONArray("myValues"); 
      JSONObject countryObC1 = countryArC1.getJSONObject(0); 

      JSONArray countryAr2C1 = countryArC1.getJSONArray(1); 
      JSONObject countryOb2C1 = countryAr2C1.getJSONObject(0); 

      //For Country 2 
     // Get the array 
      JSONArray countryArC2 = json2.getJSONArray("myValues"); 
      JSONObject countryObC2 = countryArC2.getJSONObject(0); 

      JSONArray countryAr2C2 = countryArC2.getJSONArray(1); 
      JSONObject countryOb2C2 = countryAr2C2.getJSONObject(0); 




      //Storing JSON item in a Variable 
      String name1 = countryOb2C1.getString(CountryNAME1); 
      String name2 = countryOb2C2.getString(CountryNAME2); 

      //Importing TextView 
      final TextView textView1 = (TextView)findViewById(R.id.url1); 
      final TextView textView2 = (TextView)findViewById(R.id.url2); 

      //Set JSON Data in TextView 
      textView1.setText(name1); 
      textView2.setText(name2); 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 

JSONParser:

public class JSONParser { 
static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 

// constructor 
public JSONParser() { 

} 

public JSONObject getJSONFromUrl(String url) { 

    //System.out.println("url getJSONfromUrl " + url); 
    //url = "http://api.worldbank.org/countries/CA/indicators/SP.POP.TOTL?date=1980:1981&format=json"; 

    // Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     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(); 
     System.out.println("JSONParser string: " + json); 
    } 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); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 


    if (json.startsWith("[")) { 
     // We have a JSONArray 
     try { 
      jObj = new JSONObject(); 
      jObj.put("data", new JSONArray(json)); 
     } catch (JSONException e) { 
      Log.d("JSON Parser", "Error parsing JSONArray " + e.toString()); 
     } 
     return jObj; 
    } 

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

    // return JSON String 
    return jObj; 



} 
} 

एक्सएमएल:

<TextView 
     android:id="@+id/url1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 

    <TextView 
     android:id="@+id/url2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_below="@+id/url1" 
     android:layout_marginTop="104dp" 
     /> 

मुझे लगता है कि मुख्य समस्या new GetJSONTask().execute(url1); औरहै 10 क्योंकि मेरे पास 2 GetJSONTASK नहीं हो सकते हैं और एक ही समय में json1 और json2 वापस नहीं कर सकते हैं।

कोई विचार?

उत्तर

5

मैं सुझाव है कि आप

class GetJSONTask extends AsyncTask<String, Void, JSONObject[]> { 

... 

protected JSONObject[] doInBackground(String... urls) { 
    // Creating new JSON Parser 
    JSONParser jParser = new JSONParser(); 

    // Getting JSON from URL 
    JSONObject[] jsons = new JSONObject[2]; 
    jsons[0] = jParser.getJSONFromUrl(url1); 
    jsons[1] = jParser.getJSONFromUrl(url2); 

    return jsons; 
} 
protected void onPostExecute(JSONObject[] jsons) { 
    JSONObject json1 = jsons[0]; 
    JSONObject json2 = jsons[1]; 
    // do you work after this 
} 
} 

आशा है कि यह मदद करने के लिए अपने-प्रक्रिया में परिवर्तन करने के लिए!

+0

परफेक्ट बढ़ाता है। धन्यवाद। – Alex

0

संस्करण ए) आप इस तरह अपने GetJSONTask पर अमल कर सकते हैं:

new GetJSONTask().execute(url1, url2); 

और इस तरह अपने AsyncTask में यूआरएल का उपयोग:

class GetJSONTask extends AsyncTask<String, Void, JSONObject[]> { 
    ... 
    JSONObject[] doInBackground(String... urls) { 

     // Getting JSON from URL 
     JSONObject json1 = jParser.getJSONFromUrl(urls[1]); 
     JSONObject json2 = jParser.getJSONFromUrl(url2[2]); 

     return new JSONObject[] {json1, json2}; 
    } 

    void onPostExecute(JSONObject[] jsons) { ... } 

} 

संस्करण बी) आप चाहते हैं प्रक्रिया को वास्तव में समानांतर चलाना, आप एक और AsyncTask जोड़ सकते हैं जो 2 बाल कार्य बनाता है और उनके परिणाम के लिए इंतजार कर रहा है। अधिकांश उपकरणों पर, यह कार्य करने के लिए आधा समय आवश्यक होगा।

class GetTwoJsonsTask extends AsyncTask<String, Void, JSONObject[]> { 
    JSONObject[] doInBackground(urls) { 

     task1 = new GetJSONTask().execute(urls[1]); 
     task2 = new GetJSONTask().execute(urls[2]); 

     // block until sub-tasks are finished 
     JSONObject json1 = task1.get(); 
     JSONObject json2 = task2.get(); 

     return new JSONObject[] {json1, json2}; 
    } 

    void onPostExecute(JSONObject[] jsons) { ... } 

} 
+0

आपकी मदद के लिए धन्यवाद। लेकिन एक समस्या है। इसमें 'नए JSONObject [] {json1, json2} की वापसी की त्रुटियां हैं;' और यही कारण है: _Type मेल नहीं: JSONObject [] से JSONObject_ – Alex

+0

में कनवर्ट नहीं किया जा सकता है आपको सामान्य AsyncTask में सही प्रकार प्रदान करना होगा: 'GetJSontask AsyncTask <स्ट्रिंग, शून्य, JSONObject []> ' – sulai