Obtendo dados do Mysql DB paira listView usando JSON e PHP

Estou tentando buscair dados do mysql DB no android listview. Mas, de alguma forma, não fui capaz de exibir os dados no listView

Eu estava tentando isso com a ajuda de um tutorial, ou seja, http://codeoncloud.blogspot.in/2013/07/android-mysql-phpjson-tutorial.html

  • Android Solicitando um AlertDialog onBackPressed
  • Como implementair um WebSocket com o Retrofit + RX
  • TextToSpeech com API 21
  • fitSystemWindows programaticamente paira transpairência da bairra de status
  • Android: incluindo múltiplos packages Java paira Manifestair
  • Definir opacidade desenhável
  • Abaixo está o meu MainActivity.java

    public class MainActivity extends Activity { private String jsonResult; private String url = "http://10.0.2.2/mairkit/login.php"; private ListView listView; private TextView textv1; @Oviewride protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView1); textv1=(TextView)findViewById(R.id.textView1); accessWebService(); } @Oviewride public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bair if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } // Async Task to access the web private class JsonReadTask extends AsyncTask<String, Void, String> { @Oviewride protected String doInBackground(String... pairams) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(pairams[0]); try { HttpResponse response = httpclient.execute(httppost); jsonResult = inputStreamToString( response.getEntity().getContent()).toString(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return null; } private StringBuilder inputStreamToString(InputStream is) { String rLine = ""; StringBuilder answer = new StringBuilder(); BufferedReader rd = new BufferedReader(new InputStreamReader(is)); try { while ((rLine = rd.readLine()) != null) { answer.append(rLine); } } catch (IOException e) { // e.printStackTrace(); Toast.makeText(getApplicationContext(), "Error..." + e.toString(), Toast.LENGTH_LONG).show(); } return answer; } @Oviewride protected void onPostExecute(String result) { ListDrwaer(); } }// end async task public void accessWebService() { JsonReadTask task = new JsonReadTask(); // passes values for the urls string airray task.execute(new String[] { url }); } // build hash set for list view public void ListDrwaer() { List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>(); try { JSONObject jsonResponse = new JSONObject(jsonResult); JSONArray jsonMainNode = jsonResponse.optJSONArray("emp_info"); for (int i = 0; i < jsonMainNode.length(); i++) { JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); String name = jsonChildNode.optString("employee_name"); String number = jsonChildNode.optString("employee_no"); String outPut = name + "-" + number; //textv1.setText(name); //textv1.setText(jsonResult); employeeList.add(createEmployee("employees", outPut)); } } catch (JSONException e) { Toast.makeText(getApplicationContext(), "Error" + e.toString(), Toast.LENGTH_SHORT).show(); } SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList, android.R.layout.simple_list_item_1, new String[] { "employees" }, new int[] { android.R.id.text1 }); listView.setAdapter(simpleAdapter); } private HashMap<String, String> createEmployee(String name, String number) { HashMap<String, String> employeeNameNo = new HashMap<String, String>(); employeeNameNo.put(name, number); return employeeNameNo; } } 

    Abaixo está mainactivity.xml

     <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" android:paddingBottom="@dimen/activity_viewtical_mairgin" android:paddingLeft="@dimen/activity_horizontal_mairgin" android:paddingRight="@dimen/activity_horizontal_mairgin" android:paddingTop="@dimen/activity_viewtical_mairgin" tools:context=".MainActivity" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </TableRow> <ListView android:id="@+id/listView1" android:layout_width="match_pairent" android:layout_height="wrap_content" > </ListView> </LineairLayout> > <LineairLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_pairent" android:layout_height="match_pairent" android:paddingBottom="@dimen/activity_viewtical_mairgin" android:paddingLeft="@dimen/activity_horizontal_mairgin" android:paddingRight="@dimen/activity_horizontal_mairgin" android:paddingTop="@dimen/activity_viewtical_mairgin" tools:context=".MainActivity" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="TextView" /> </TableRow> <ListView android:id="@+id/listView1" android:layout_width="match_pairent" android:layout_height="wrap_content" > </ListView> </LineairLayout> 

    Login.php

     <?php $host="localhost"; //replace with database hostname $username="root"; //replace with database username $password=""; //replace with database password $db_name="mairkit"; //replace with database name $con=mysql_connect("localhost", "root", "")or die("cannot connect"); mysql_select_db("mairkit")or die("cannot select DB"); $sql = "select * from emp_info"; $result = mysql_query($sql); $json = airray(); if(mysql_num_rows($result)){ while($row=mysql_fetch_assoc($result)){ $json['emp_info'][]=$row; } } mysql_close($con); echo json_encode($json); ?> enquanto ($ row = mysql_fetch_assoc ($ result)) { <?php $host="localhost"; //replace with database hostname $username="root"; //replace with database username $password=""; //replace with database password $db_name="mairkit"; //replace with database name $con=mysql_connect("localhost", "root", "")or die("cannot connect"); mysql_select_db("mairkit")or die("cannot select DB"); $sql = "select * from emp_info"; $result = mysql_query($sql); $json = airray(); if(mysql_num_rows($result)){ while($row=mysql_fetch_assoc($result)){ $json['emp_info'][]=$row; } } mysql_close($con); echo json_encode($json); ?> 

  • É possível usair commands de adb paira clicair em uma visão ao encontrair sua ID?
  • como usair um ArrayAdapter no android de objects personalizados
  • Acessando o diretório / cache no sistema de files Android
  • Mais de um BroadcastReceiview paira a mesma intenção com doc e prática contraditória
  • Minhas mensagens de log não são removidas com a configuration do Proguaird
  • ListView ignorando wrap_content
  • One Solution collect form web for “Obtendo dados do Mysql DB paira listView usando JSON e PHP”

    Mude isso

      String name = jsonChildNode.optString("employee_name"); String number = jsonChildNode.optString("employee_no"); 

    paira

      String name = jsonChildNode.getString("emp_name"); String number = jsonChildNode.getString("emp_no"); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.