الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

مشكلة في كود تطبيق مدونة عن طريق دوال

بدأه أبوريتاج في 30 يونيو 2013 · 2 رد · 563 مشاهدة · في Android
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

لدي مشكلة في كود ولكن لا اعلم اين الخطاء بالتحديد في استرداد مواضيع المنتدى وهذا هوا الكود MainListActivity.java

حيت عملية تشغيل التطبيق عن طريق المحاكي تظهر لي الرسالة اللي قمت بإدخالها عندما لايكون هناك اتصال بالانترنت 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_list);
        
       GetBlogPostTask getBlogPostTask = new GetBlogPostTask();
       getBlogPostTask.execute();
       
      //  Toast.makeText(this, getString(R.string.no_itms). Toast.LENTH_LONG).show();
        
    }


    @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_list, menu);
        return true;
    }
    
    private class GetBlogPostTask extends AsyncTask<Object, Void, String> {

		@Override
		protected String  doInBackground(Object... params) {
			int respnoseCode =-1;
			
	        try{
	        	URL BlogFeedUrl = new URL("http://www.androidar.net/api/get_recent_summary/?count="+ NUMBER_OF_POSTS);
	        	HttpURLConnection connection =(HttpURLConnection) BlogFeedUrl.openConnection();
	        	connection.connect();
	        	
	            responseCode = connection.getResponseCode();
	            if (respnoseCode == HttpURLConnection.HTTP_OK) {
	            	InputStream inputStream = connection.getInputStream();
	            	Reader reader = new InputStreamReader(inputStream);
	            	int contentLength = connection.getContentLength();
	            	char[] charArray = new char[contentLength];
	            	reader.read(charArray);
	            	String responseData = new String(charArray);
	               	JSONObject jsonResponse = new JSONObject(responseData);
					String status = jsonResponse.getString("status");
					Log.v(TAG, status);
					
					JSONArray jsonPosts = jsonResponse.getJSONArray("posts");
					for (int i = 0 ; i < jsonPosts.length(); i++){
						JSONObject jsonPost = jsonPosts.getJSONObject(i);
						String title = jsonPost.getString("title");
						Log.v(TAG, "Post " + i + "; " + title);
						
					
						
					}
					
	            	
	            	
	            	
	            	
	            	
	            }
	            else {
							
	            	Log.i(TAG,"Unsuccessful HTTP Response Code: " + responseCode);
	            }
	        	
	        }
	        catch (MalformedURLException e) {
	        	Log.e(TAG , "Exception caught: " , e);
	        }
	        catch (IOException e){
	        	Log.e(TAG , "Exception caught: " , e);
	        	
	        }
	        catch (Exception e){
	        	Log.e(TAG , "Exception caught: " , e);
	        }
	        
	        return "Code : "+ respnoseCode; 
		}
    	
    }
    
}

تم تعديل هذه المشاركة بواسطة أبوريتاج في 30 يونيو 2013 في 22:07

#2

تفضل هذه دالة الـ doInBackground

@Override
        protected String doInBackground(Object... params) {

            DefaultHttpClient httpClient = new DefaultHttpClient();
            try {
                String url = "http://www.androidar.net/api/get_recent_summary/?count="
                        + NUMBER_OF_POSTS;

                HttpGet httpGet = new HttpGet(url);
                HttpResponse httpResponse = httpClient.execute(httpGet);

                int statusResponse = httpResponse.getStatusLine()
                        .getStatusCode();
                if (statusResponse == 200) {
                    HttpEntity httpEntity = httpResponse.getEntity();
                    String responseData = EntityUtils.toString(httpEntity);
                    JSONObject jsonResponse = new JSONObject(responseData);
                    String status = jsonResponse.getString("status");
                    Log.v(TAG, status);

                    JSONArray jsonPosts = jsonResponse.getJSONArray("posts");
                    for (int i = 0; i < jsonPosts.length(); i++) {
                        JSONObject jsonPost = jsonPosts.getJSONObject(i);
                        String title = jsonPost.getString("title");
                        Log.v(TAG, "Post " + i + "; " + title);
                    }
                } else {
                    Log.i(TAG, "Unsuccessful HTTP Response Code: "
                            + responseCode);
                }

            } catch (MalformedURLException e) {
                Log.e(TAG, "Exception caught: ", e);
            } catch (IOException e) {
                Log.e(TAG, "Exception caught: ", e);

            } catch (Exception e) {
                Log.e(TAG, "Exception caught: ", e);
            }

            return "Code : " + responseCode;
        }

Software Developer
Mahmoudkelany.com


 

#3

شكراً لك سوف اقوم بتعديل الكود ولكن ياريت تشرح لي ايش هوا الخطاء لكي يستفيد الجميع 

مواضيع مشابهة