2017-08-03 86 views
1

這裏是MainActivity.class問題沒有顯示我的數據?** 在logcat中沒有錯誤或別的東西。我第一次嘗試這個。但我必須知道這個問題。 here the image of emultor。我在Onresponse metheod中使用了一個進度條。它並沒有看不見。因爲我的回覆不起作用。我該如何解決這個問題?爲什麼我的JSON數據未顯示在我的recyclerview

public class MainActivity extends AppCompatActivity { 
     View view; 
     ArrayList<user> list=new ArrayList<>(); 
     RecyclerView recyclerView; 
     RecyclerViewCardViewAdapter recycleExpert_adapter; 
     // private GoogleApiClient client; 
     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main); 

      recyclerView = (RecyclerView) findViewById(R.id.recyclerView1); 
      RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext()); 
      recyclerView.setLayoutManager(mLayoutManager); 
      recyclerView.setItemAnimator(new DefaultItemAnimator()); 
      recyclerView.setHasFixedSize(true); 

      // list= new ArrayList<user>(); 
      new AsynDemo().execute(); 

      // setSupportActionBar(toolbar); 
      // client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
     } 
     public class AsynDemo extends AsyncTask<String, Integer, String> { 
      // AlertDialog alertdialog; 
      @Override 
      protected void onPreExecute() { 
       super.onPreExecute(); 
       // alertdialog.new SpotsDialog(MainActivity.this, "Please Wait", R.style.Custom); 
       // alertdialog.show(); 
      } 

      @Override 
      protected String doInBackground(String... params) { 
       String response = ""; 
       URL url = null; 
       try { 
        url = new URL("http://127.0.0.1:8080/app/con_get_data.php"); 
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection(); 
        httpURLConnection.setConnectTimeout(30000); 
        httpURLConnection.connect(); 
        InputStream in = httpURLConnection.getInputStream(); 
        // read response 
        response = readStream(in); 
        httpURLConnection.disconnect(); 
        JSONObject jsonObject = new JSONObject(response); 
        JSONArray jsonArray = jsonObject.getJSONArray("result"); 
        int count = jsonArray.length(); 
        for (int i = 0; i < count; i++) { 
         JSONObject jsonObject2 = jsonArray.getJSONObject(i); 
         String name = jsonObject2.getString("name"); 
         String email = jsonObject2.getString("email"); 

         list.add(new user(name, email)); 
        } 

       } catch (IOException e) { 
        e.printStackTrace(); 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
       return null; 
      } 
      @Override 
      protected void onPostExecute(String s) { 
       super.onPostExecute(s); 
       try { 
        recycleExpert_adapter = new RecyclerViewCardViewAdapter(list,MainActivity.this); 
        recyclerView.setAdapter(recycleExpert_adapter); 
        recycleExpert_adapter.notifyDataSetChanged(); 

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

       } 
      } 

      private String readStream(InputStream is) { 
       BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); 
       String line = "\n"; 
       String result = ""; 
       try { 
        while ((line = bufferedReader.readLine()) != null) { 
         result += line; 
        } 
       } catch (IOException e) { 
        e.printStackTrace(); 
       } 
       return result; 
      } 
     } 
    } 

這裏是適配器

public class RecyclerViewCardViewAdapter extends RecyclerView.Adapter<RecyclerViewCardViewAdapter.ViewHolder> { 

    Context context; 

    List<user> item; 

    public RecyclerViewCardViewAdapter(List<user> getDataAdapter, Context context){ 

     super(); 

     this.item = getDataAdapter; 

     this.context = context; 
    } 

    @Override 
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.cardview, parent, false); 

     ViewHolder viewHolder = new ViewHolder(view); 

     return viewHolder; 
    } 

    @Override 
    public void onBindViewHolder(ViewHolder holder, int position) { 

     user getDataAdapter1 = item.get(position); 

     holder.Name.setText(getDataAdapter1.getName()); 
     holder.Email.setText(getDataAdapter1.getEmail()); 

    } 

    @Override 
    public int getItemCount() { 

     return item.size(); 
    } 

    class ViewHolder extends RecyclerView.ViewHolder{ 

     public TextView Name,Email; 


     public ViewHolder(View itemView) { 

      super(itemView); 

      Name = (TextView) itemView.findViewById(R.id.Name) ; 
      Email = (TextView) itemView.findViewById(R.id.email) ; 


     } 
    } 
} 
+0

可能是一些異常拋出的地方,你應該首先嚐試使用Log.e( 「MainActivity - RecyclerView」, 「<異常的說明>」,E) ;而不是e.printStacktrace()來查看異常。在你看到它之後,解決你的問題將會更直接。 –

+0

不要濫用文字屬性。在你的問題中沒有什麼特別的證據可以證明它的全文** boldfaced ** ... –

+0

我得到了我的答案它的模擬器。 url = new URL(「http://127.0.0.1:8080/app/con_get_data.php」); 更改爲 url = new URL(「http://10.0.2.2:8080/app/con_get_data.php」); –

回答

1

我有我的ANS其模擬器。

url = new URL("http://127.0.0.1:8080/app/con_get_data.php"); 

將其更改爲

url = new URL("http://10.0.2.2:8080/app/con_get_data.php"); 
相關問題