Note that there are some explanatory texts on larger screens.

plurals
  1. POLocalhost is not working but site is working in Android
    primarykey
    data
    text
    <p>I am learning Android. And i am trying to read the PHP output (through JSON ) from Android.</p> <p>If i give the Site URL, its fetching the value. If you enter the site it display the Json code like below.</p> <p><a href="http://cpriyankara.coolpage.biz/employee_details.php" rel="nofollow">http://cpriyankara.coolpage.biz/employee_details.php</a>";</p> <p>{"emp_info":[{"employee name":"Adam","employee no":"101700"},{"employee name":"John","employee no":"101701"},{"employee name":"Paul","employee no":"101702"},{"employee name":"Mark","employee no":"101703"},{"employee name":"Donald","employee no":"101704"},{"employee name":"Brain","employee no":"101705"},{"employee name":"Kevin","employee no":"101706"}]}</p> <p>If i give my local host PHP site also it displaying like above in the format of Json. ( I am running PHP using WAMPSERVER)</p> <p>{"emp_info":[{"employee name":"Adam","employee no":"101700"},{"employee name":"John","employee no":"101701"},{"employee name":"Paul","employee no":"101702"},{"employee name":"Mark","employee no":"101703"},{"employee name":"Donald","employee no":"101704"},{"employee name":"Brain","employee no":"101705"},{"employee name":"Kevin","employee no":"101706"}]}</p> <p>But in the Android it displaying results if i give the site, but if i give localhost address it says application stopped unexpectedly. </p> <p>Please let me know why??. In this case how i can see the errors OR exceptions.</p> <p>I provided the PHP and Android code below.</p> <p>PHP Code:</p> <pre><code>&lt;?php $host=""; //replace with database $username="root"; //replace with database username $password="root"; //replace with database password $db_name="and"; //replace with database name $con=mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $sql = "select * from emp_info"; $result = mysql_query($sql); $json = array(); if(mysql_num_rows($result)){ while($row=mysql_fetch_assoc($result)){ $json['emp_info'][]=$row; } } mysql_close($con); echo json_encode($json); ?&gt; </code></pre> <p>Android Java code:</p> <p>In the below code in the URL i changed to localhost URL</p> <p>package com.example.phpmysql;</p> <pre><code>import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import android.os.AsyncTask; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.Toast; public class MainActivity extends Activity { private String jsonResult; Private String url = "http://cpriyankara.coolpage.biz/employee_details.php"; private ListView listView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); listView = (ListView) findViewById(R.id.listView1); accessWebService(); } @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, menu); return true; } // Async Task to access the web private class JsonReadTask extends AsyncTask&lt;String, Void, String&gt; { @Override protected String doInBackground(String... params) { HttpClient httpclient = new DefaultHttpClient(); HttpPost httppost = new HttpPost(params[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; } @Override protected void onPostExecute(String result) { ListDrwaer(); } }// end async task public void accessWebService() { JsonReadTask task = new JsonReadTask(); // passes values for the urls string array task.execute(new String[] { url }); } // build hash set for list view public void ListDrwaer() { List&lt;Map&lt;String, String&gt;&gt; employeeList = new ArrayList&lt;Map&lt;String, String&gt;&gt;(); try { JSONObject jsonResponse = new JSONObject(jsonResult); JSONArray jsonMainNode = jsonResponse.optJSONArray("emp_info"); for (int i = 0; i &lt; jsonMainNode.length(); i++) { JSONObject jsonChildNode = jsonMainNode.getJSONObject(i); String name = jsonChildNode.optString("employee name"); String number = jsonChildNode.optString("employee no"); String outPut = name + "-" + number; 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&lt;String, String&gt; createEmployee(String name, String number) { HashMap&lt;String, String&gt; employeeNameNo = new HashMap&lt;String, String&gt;(); employeeNameNo.put(name, number); return employeeNameNo; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload