2016-01-15 11 views
7

मैं अपने ऐप में webservices का उपयोग कर रहा हूं। असल में मेरा ऐप वर्डप्रेस साइट की एक पोस्ट दिखाता है। उपयोगकर्ता पोस्ट को बुकमार्क भी कर सकता है। मैं पोस्ट यूआरएल के साथ एसक्लाइट में सभी पुस्तक चिह्न पोस्ट सहेज रहा हूं। अब समस्या यह है कि मेरे पास कई अलग-अलग यूआरएल हैं। और मैं इन यूआरएल की सामग्री को एक ListView में दिखाना चाहता हूं।1 से अधिक विभिन्न यूआरएल और केवल एक सूचीदृश्य

इन सभी यूआरएल की जेसन संरचना भी वही है।

मैं इस मुद्दे से संबंधित अन्य प्रश्न पर ध्यान दिया है, लेकिन उन बहुत मदद नहीं कर रहे हैं।

मुझे आपको दिखाएं कि मैंने अब तक कितना प्रयास किया है।

यहाँ कोड इस यहाँ BookmarkActivity

ArrayList<HashMap<String,String>> allData; 
String[] urlarray; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_bookmark); 
    SqliteController controller= new SqliteController(this); 

    allData = controller.getAllData(); 
    for (int i=0; i<allData.size(); i++){ 
     String url=allData.get(i).get("link"); 
     urlarray= new String[]{url}; 
    } 
    for(int i=0; i <urlarray.length; i++){ 
     MyAsyncTask task = new MyAsyncTask(i); 
     task.execute(); 
    } 
} 
private class MyAsyncTask extends AsyncTask<Object,Void,String>{ 
    int urlNumber; 
    HttpURLConnection connection=null; 
    BufferedReader reader=null; 
    public MyAsyncTask (int number){ 
     this.urlNumber=number; 
    } 
    @Override 
    protected String doInBackground(Object... params) { 
     try { 
      URL url = new URL(urlarray[urlNumber]); 
      connection= (HttpURLConnection) url.openConnection(); 
      connection.connect(); 
      StringBuffer buffer = new StringBuffer(); 
      String line = ""; 
      while ((line = reader.readLine()) != null) { 
       buffer.append(line); 
      } 
      String json = buffer.toString(); 
      return json; 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 
    @Override 
    protected void onPostExecute(String s) { 
     super.onPostExecute(s); 
     //Log.d("TAG",s); 
    } 
} 

का कोड है json है। (अपने नहीं ठीक उसी json लेकिन मुझे यकीन है कि हर यूआरएल एक ही json संरचना होगा हूँ)

{ 
    "status": "ok", 
    "count": 1, 
    "count_total": 1, 
    "pages": 1, 
    "posts": [ 
    { 
     "id": 1, 
     "type": "post", 
     "slug": "hello-world", 
     "url": "http:\/\/localhost\/wordpress\/?p=1", 
     "title": "Hello world!", 
     "title_plain": "Hello world!", 
     "content": "<p>Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!<\/p>\n", 
     "excerpt": "Welcome to WordPress. This is your first post. Edit or delete it, then start blogging!\n", 
     "date": "2009-11-11 12:50:19", 
     "modified": "2009-11-11 12:50:19", 
     "categories": [], 
     "tags": [], 
     "author": { 
     "id": 1, 
     "slug": "admin", 
     "name": "admin", 
     "first_name": "", 
     "last_name": "", 
     "nickname": "", 
     "url": "", 
     "description": "" 
     }, 
     "comments": [ 
     { 
      "id": 1, 
      "name": "Mr WordPress", 
      "url": "http:\/\/wordpress.org\/", 
      "date": "2009-11-11 12:50:19", 
      "content": "<p>Hi, this is a comment.<br \/>To delete a comment, just log in and view the post's comments. There you will have the option to edit or delete them.<\/p>\n", 
      "parent": 0 
     } 
     ], 
     "comment_count": 1, 
     "comment_status": "open" 
    } 
    ] 
} 

किसी ने मुझसे इस के साथ मदद कर सकते हैं। मुझे ऐसी स्थिति के साथ कैसे काम करना चाहिए?

+0

आप सूची – Vishwa

+0

ठीक इंतजार से URL में से एक से एक json पोस्ट कर सकते हैं। मुझे –

+0

पोस्ट करने दें मेरे प्रश्न को अपडेट करें। कृपया इसे देखें –

उत्तर

3

इस एक कोशिश ... इस कोड में वास्तव में नेटवर्क संचालन के लिए वॉली लाइब्रेरी का उपयोग कर रहा हूँ। नेटवर्क ऑपरेशंस के लिए वॉली लाइब्रेरी का नया उदाहरण बनाने से बचने के लिए सबसे पहले अपने एप्लिकेशन में एप्लिकेशन क्लास बनाएं।

public class ApplicationController extends Application { 

public static final String TAG = ApplicationController.class 
     .getSimpleName(); 

private RequestQueue mRequestQueue; 
private ImageLoader mImageLoader; 

private static ApplicationController mInstance; 

@Override 
public void onCreate() { 
    super.onCreate(); 
    mInstance = this; 
} 

public static synchronized ApplicationController getInstance() { 
    return mInstance; 
} 

public RequestQueue getRequestQueue() { 
    if (mRequestQueue == null) { 
     mRequestQueue = Volley.newRequestQueue(getApplicationContext()); 
    } 

    return mRequestQueue; 
} 

//This function request the volley library to process with tag 
    //where <T> a generic method with an unbound type variable T.You can pass any datatype to functions.for more info About generic method.go through this link:http://docs.oracle.com/javase/tutorial/extra/generics/methods.html 
public <T> void addToRequestQueue(Request<T> req, String tag) { 
    // set the default tag if tag is empty 
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag); 
    getRequestQueue().add(req); 
} 
//This function request the volley library to process with tag 
public <T> void addToRequestQueue(Request<T> req) { 
    req.setTag(TAG); 
    getRequestQueue().add(req); 
} 
//This function cancel the requested Url those are all in pending 
public void cancelPendingRequests(Object tag) { 
    if (mRequestQueue != null) { 
     mRequestQueue.cancelAll(tag); 
    } 
} 
} 

एप्लिकेशन build.gradle में निर्भरता के तहत इस पंक्ति जोड़ें

compile 'com.mcxiaoke.volley:library-aar:1.0.0' 

मैनिफ़ेस्ट फ़ाइल में नेटवर्क तक पहुंच के लिए अनुमति

<uses-permission android:name="android.permission.INTERNET"></uses-permission> 
आवेदन टैग में

आवेदन वर्ग के नाम का उल्लेख किया जोड़ने

<application 
    android:name=".ApplicationController" 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    //your activity.... 
    </application> 

इस लाइन को जोड़ने के onCreate गतिविधि में ...

progressDialog=new ProgressDialog(MainClass.this); 
    progressDialog.setMessage("Loading..."); 
    progressDialog.show(); 
    //place your code to form urlarray 
    urlarray = new String[5]; 
    //where datas represents the arraylist to store received content from json response.if you want to show more datas add your custom object to arraylist 
    datas=new ArrayList<String>(); 

    adapter= new ArrayAdapter<String>(this, 
      android.R.layout.simple_list_item_1, android.R.id.text1); 
    try { 
     for (int i = 0; i < urlarray.length; i++) { 
      final int j = i; 
      String url=urlarray[i]; 
      JsonObjectRequest objectRequest = new JsonObjectRequest(Request.Method.GET, url, (JSONObject) null, new Response.Listener<JSONObject>() { 
       @Override 
       public void onResponse(JSONObject response) { 
        Log.e("Response is for " + Integer.toString(j), response.toString()); 
     //Here i assumed received json response is same as of sample json Provided by you. 
        try{ 
        JSONArray jsonArray=response.getJSONArray("posts"); 
        if(jsonArray.length()>0){ 
         JSONObject obj=jsonArray.getJSONObject(0); 
         String content=obj.getString("content"); 
         datas.add(content); 
        } 
        if(j==urlarray.length-1) { 
         progressDialog.dismiss(); 
         adapter= new ArrayAdapter<String>(MainClass.this, 
           android.R.layout.simple_list_item_1, android.R.id.text1,datas); 
         listView.setAdapter(adapter); 
        } 
        }catch(Exception e){ 
         e.printStackTrace(); 
        } 
       } 
      }, new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.e("error",error.toString()); 
        if(j==urlarray.length-1) { 
         progressDialog.dismiss(); 
        } 
        } 
      }){ 
       @Override 
       public Map<String, String> getHeaders() throws AuthFailureError { 
        HashMap<String, String> headers = new HashMap<String, String>(); 
        headers.put("Content-Type", "application/json"); 
        headers.put("charset", "utf-8"); 
        return headers; 
       } 
      }; 
      objectRequest.setRetryPolicy(new DefaultRetryPolicy(5000, 
        DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
     ApplicationController.getInstance().addToRequestQueue(objectRequest, "JSON"); 
     } 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
+0

क्या आप मुझे ऐप कंट्रोलर क्लास के बारे में और बता सकते हैं? आवेदन में सिंगलटन का समर्थन करने के लिए –

+0

(यानी एक ऑब्जेक्ट में वॉली लाइब्रेरी के त्वरण को प्रतिबंधित करने के लिए) – Vishwa

+0

मुझे पता है कि सिंगलटन क्या समझाता है धन्यवाद। मैं इसे पसंद करूंगा यदि आप मुझे उस कोड को समझाते हैं जिसे आपने विशेष रूप से चीज़ लिखा था। मैं जावा और एंड्रॉइड सामान –

1

आप onBackground विधि में हर निष्पादन करना चाहिए आप एक अतुल्यकालिक कॉल के परिणाम प्राप्त करने के बाद। ऐसा इसलिए है क्योंकि async कार्य एक अलग थ्रेड पर निष्पादित करता है।

ArrayList<HashMap<String,String>> allData; 
Iterator iterator; 
ArrayList<String> urlarray = new ArrayList<>(); 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_bookmark); 
    SqliteController controller= new SqliteController(this); 

    allData = controller.getAllData(); 
    for (int i=0; i<allData.size(); i++){ 
     String url=allData.get(i).get("link"); 
     urlarray.add(url); 
    } 
    iterator = urlarray.iterator(); 
    MyAsyncTask task = new MyAsyncTask(); 
    task.execute(); 

} 
private class MyAsyncTask extends AsyncTask<Object,Void,ArrayList<String>>{ 
    int urlNumber; 
    ArrayList<String> resultList = new ArrayList<>(); 
    HttpURLConnection connection=null; 
    BufferedReader reader=null; 
    public MyAsyncTask(){ 

    } 
    @Override 
    protected String doInBackground(Object... params) { 
     if(iterator.hasNext()) { 
      try { 

      URL url = new URL(iterator.next()); 
      connection= (HttpURLConnection) url.openConnection(); 
      connection.connect(); 
      StringBuffer buffer = new StringBuffer(); 
      String line = ""; 
      while ((line = reader.readLine()) != null) { 
       buffer.append(line); 
      } 
      String json = buffer.toString(); 
      resultList.add(json); 
     } catch (MalformedURLException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     } 
     return resultList; 
    } 
     @Override 
     protected void onPostExecute(ArrayList<String> s) { 
      super.onPostExecute(s); 
      //Log.d("TAG",s); 
      if(s.size > 0){ 
       //Your listview processing here. 
      } 
     } 
    } 
संबंधित मुद्दे