0

will JSON zu recyclerview mit Asynctask in Fragment analysieren, aber wenn Mittagessen App, App Absturz!Parsing JSON Recyclerview mit Asynctask in Fragment

das ist mein json Daten:

{ 
    "contacts": [ 
     { 
       "id": "c200", 
       "name": "erfan", 
       "email": "[email protected]", 
       "address": "xx-xx-xxxx,x - street, x - country", 
       "gender" : "male", 
       "phone": { 
        "mobile": "+91 0000000000", 
        "home": "00 000000", 
        "office": "00 000000" 
       } 
     }, 
     { 
       "id": "c201", 
       "name": "Johnny Depp", 
       "email": "[email protected]", 
       "address": "xx-xx-xxxx,x - street, x - country", 
       "gender" : "male", 
       "phone": { 
        "mobile": "+91 0000000000", 
        "home": "00 000000", 
        "office": "00 000000" 
       } 
     } 

    ] 
} 

Namen erhalten möchten, E-Mail, mobile

dies mein Fragment ist:

public class HttpHandler { 

    private static final String TAG = HttpHandler.class.getSimpleName(); 

    public HttpHandler() { 
    } 

    public String makeServiceCall(String reqUrl) { 
     String response = null; 
     try { 
      URL url = new URL(reqUrl); 
      HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
      conn.setRequestMethod("GET"); 
      // read the response 
      InputStream in = new BufferedInputStream(conn.getInputStream()); 
      response = convertStreamToString(in); 
     } catch (MalformedURLException e) { 
      Log.e(TAG, "MalformedURLException: " + e.getMessage()); 
     } catch (ProtocolException e) { 
      Log.e(TAG, "ProtocolException: " + e.getMessage()); 
     } catch (IOException e) { 
      Log.e(TAG, "IOException: " + e.getMessage()); 
     } catch (Exception e) { 
      Log.e(TAG, "Exception: " + e.getMessage()); 
     } 
     return response; 
    } 

    private String convertStreamToString(InputStream is) { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line; 
     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line).append('\n'); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 
} 
:

public class maghalat extends Fragment { 

    private RecyclerView recyclerView; 
    private DataAdapter adapter; 
    private View myFragmentView; 

    private String TAG = MainActivity.class.getSimpleName(); 
    private ProgressDialog pDialog; 
    // URL to get contacts JSON 
    private static String url = "http://memaraneha.ir/Erfan/jsonravi.php"; 

    List<jsonContent> listcontent=new ArrayList<>(); 
    public jsonContent content; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     myFragmentView = inflater.inflate(R.layout.maghalat, container, false); 

      new GetContacts().execute(); 

     return myFragmentView; 
    } 

      class GetContacts extends AsyncTask<Void, Void, Void> { 

       @Override 
       protected void onPreExecute() { 
        super.onPreExecute(); 
        // Showing progress dialog 
        pDialog = new ProgressDialog(getActivity()); 
        pDialog.setMessage("Please wait..."); 
        pDialog.setCancelable(false); 
        pDialog.show(); 

       } 

       @Override 
       protected Void doInBackground(Void... arg0) { 
        HttpHandler sh = new HttpHandler(); 

        // Making a request to url and getting response 
        String jsonStr = sh.makeServiceCall(url); 

        Log.e(TAG, "Response from url: " + jsonStr); 

        if (jsonStr != null) { 
         try { 
          JSONObject jsonObj = new JSONObject(jsonStr); 

          // Getting JSON Array node 
          JSONArray contacts = jsonObj.getJSONArray("contacts"); 

          // looping through All Contacts 
          for (int i = 0; i < contacts.length(); i++) { 
           JSONObject c = contacts.getJSONObject(i); 

           content.name=c.getString("name"); 
           content.email=c.getString("email"); 

           JSONObject phone = c.getJSONObject("phone"); 
           content.mobile=phone.getString("mobile"); 

           listcontent.add(content); 
          } 
         } catch (final JSONException e) { 
          Log.e(TAG, "Json parsing error: " + e.getMessage()); 
          getActivity().runOnUiThread(new Runnable() { 
           @Override 
           public void run() { 
            Toast.makeText(getActivity().getApplicationContext(), 
              "Json parsing error: " + e.getMessage(), 
              Toast.LENGTH_LONG) 
              .show(); 
           } 
          }); 

         } 
        } else { 
         Log.e(TAG, "Couldn't get json from server."); 
         getActivity().runOnUiThread(new Runnable() { 
          @Override 
          public void run() { 
           Toast.makeText(getActivity().getApplicationContext(), 
             "Couldn't get json from server. Check LogCat for possible errors!", 
             Toast.LENGTH_LONG) 
             .show(); 
          } 
         }); 

        } 

        return null; 
       } 

       @Override 
       protected void onPostExecute(Void result) { 
        super.onPostExecute(result); 

        pDialog.dismiss(); 
        /** 
        * Updating parsed JSON data into ListView 
        * */ 

        recyclerView=(RecyclerView)myFragmentView.findViewById(R.id.recycler_view); 
        recyclerView.setHasFixedSize(true); 
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity().getApplicationContext()); 
        recyclerView.setLayoutManager(layoutManager); 
        adapter=new DataAdapter(getActivity(),listcontent); 
        recyclerView.setAdapter(adapter); 
       } 

      }} 

dies meine Httphandler ist

das ist mein Recycler Ansicht Adapter:

public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> { 
    private Context context; 
    List<jsonContent> jcontent; 



    public DataAdapter(Context context,List<jsonContent> jcontent) { 

     this.context=context; 
     this.jcontent=jcontent; 
    } 



    @Override 
    public DataAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { 
     View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.card_row, viewGroup, false); 
     return new ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(DataAdapter.ViewHolder viewHolder,int i) { 



     viewHolder.name.setText(jcontent.get(i).name); 
     viewHolder.email.setText(jcontent.get(i).email); 
     viewHolder.mobile.setText(jcontent.get(i).mobile); 


    } 

    @Override 
    public int getItemCount() { 
     return jcontent.size(); 
    } 

    public class ViewHolder extends RecyclerView.ViewHolder{ 
     private TextView name,email,mobile; 



     public ViewHolder(final View view) { 
      super(view); 


      name = (TextView)view.findViewById(R.id.name); 
      email = (TextView)view.findViewById(R.id.email); 
      mobile = (TextView)view.findViewById(R.id.mobile); 




     } 
    } 
} 

und dies ist jsonContent Klasse:

public class jsonContent { 

    public String name; 
    public String email; 
    public String mobile; 

} 

so, wo bin ich falsch? Bitte helfen Sie

+0

wie dies tun posten Sie Ihre Log-Katze Fehler – Redman

+0

was Fehler in logcat –

+0

Beitrag zeigt Ihre Fehlermeldung bitte – Zain

Antwort

1

Sie sind Werte in JsonContent Inhalt zuweisen, ohne Objekt erstellen. Sie sollten in doinBackGround() von AsyncTask

jsonContent content=new jsonContent(); 
content.name=c.getString("name"); 
content.email=c.getString("email"); 

content.mobile=phone.getString("mobile"); 
+0

danke für schnelle und klare Antwort;) – erfan

+0

für zukünftige Versuche Logcat für Fehler zu suchen. –