Php 从Android向本地服务器发出请求时出现连接错误
我正在构建一个应用程序,将json从数据库解析为listview。我使用PHP脚本以JSON格式对数据库中的数据进行编码,如下所示:Php 从Android向本地服务器发出请求时出现连接错误,php,android,json,Php,Android,Json,我正在构建一个应用程序,将json从数据库解析为listview。我使用PHP脚本以JSON格式对数据库中的数据进行编码,如下所示: [ {"id":"1","artistname":"Rihanna"}, {"id":"2","artistname":"Katy Perry"}, {"id":"3","artistname":"DJ Dibs"}, {"id":"4","artistname":"Jamie Foxx"}, {"id":"5","artistname":"Disclo
[
{"id":"1","artistname":"Rihanna"},
{"id":"2","artistname":"Katy Perry"},
{"id":"3","artistname":"DJ Dibs"},
{"id":"4","artistname":"Jamie Foxx"},
{"id":"5","artistname":"Disclosure"}
]
以下是以字符串形式返回上述数据的解析器类:
package com.example.jsontester;
public class JsonParser {
static InputStream is = null;
static JSONObject jsonObject = null;
static String data = "";
public JsonParser() {
}
public String getJSONFromUrl(String url) {
// Making HTTP request
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
catch (UnsupportedEncodingException exception) {
exception.printStackTrace();
} catch (ClientProtocolException exception) {
exception.printStackTrace();
} catch (IOException exception) {
exception.printStackTrace();
}
//Gets the JSON String
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sBuilder = new StringBuilder();
String line = null;
//Appends the string to the line
while ((line = reader.readLine()) != null) {
sBuilder.append(line + "\n");
}
is.close();
data = sBuilder.toString();
}
catch (Exception exception) {
exception.printStackTrace();
}
return data;
}
}
下面是使用图表项创建listview的活动
package com.example.jsontester;
public class JsonActivity extends ListActivity{
private ProgressDialog progressDialog;
// JSON Node names
private static final String TAG_ITEM = "item";
private static final String TAG_ID = "id";
private static final String TAG_ARTISTNAME = "artistname";
// chartItemList is the array list that holds the chart items
ArrayList<HashMap<String, Integer>> chartItemList =
new ArrayList<HashMap<String, Integer>>();
JsonParser Parser = new JsonParser();
// JSONArray
JSONArray chartItems = null;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.progressdialog);
//url from where the JSON has to be retrieved
String url = <<http://127.0.0.1/mmcv/>>;
//Check if the user has a connection
ConnectivityManager cm =
(ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info != null) {
if (!info.isConnected()) {
Toast.makeText(this,
"Please check your connection and try again.", Toast.LENGTH_SHORT).show();
}
//if positive, fetch the articles in background
else new getChartItems().execute(url);
}
//else show toast
else {
Toast.makeText(this, "Please check your connection and try again.",
Toast.LENGTH_SHORT).show();
}
}
class getChartItems extends AsyncTask<String, String, String> {
// Shows a progress dialog while executing background task
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(JsonActivity.this);
progressDialog.setMessage("Loading chart...");
progressDialog.setIndeterminate(false);
progressDialog.setCancelable(false);
progressDialog.show();
}
//Gets the json data for chart items data and presents it in a list view
@Override
protected String doInBackground(String... args) {
String url = <<http://127.0.0.1/mmcv>>;
String json = Parser.getJSONFromUrl(url);
System.out.println("Json should be below me");
System.out.println(json);
int id;
String artistname;
try{
chartItems = new JSONArray(json);
JSONObject json_data=null;
for(int i=0;i<chartItems.length();i++){
json_data = chartItems.getJSONObject(i);
artistname=json_data.getString("TAG_ARTISTNAME");
id=json_data.getInt("TAG_ID");
HashMap<String, Integer> hashMap = new HashMap<String, Integer>();
// adding each child node to HashMap key => value
hashMap.put(artistname,id);
// adding HashMap to ArrayList
chartItemList.add(hashMap);
}
}
catch (JSONException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
public void run() {
//updating list view with the parsed items
ListAdapter adapter = new SimpleAdapter(JsonActivity.this, chartItemList,
R.layout.listview,
new String[] { TAG_ID, TAG_ARTISTNAME }, new int[] {R.id.id,
R.id.artistname });
setListAdapter(adapter);
}
});
return null;
}
//Removes the progress dialog when the data has been fetched
protected void onPostExecute(String args) {
progressDialog.dismiss();
}
}
}
package com.example.jsontest;
公共类JsonActivity扩展了ListActivity{
私有进程对话;
//JSON节点名称
私有静态最终字符串标记\u ITEM=“ITEM”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记\u ARTISTNAME=“ARTISTNAME”;
//chartItemList是保存图表项的数组列表
ArrayList chartItemList=
新的ArrayList();
JsonParser=新的JsonParser();
//杰索纳雷
JSONArray chartItems=null;
/**首次创建活动时调用。*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.progressdialog);
//必须从中检索JSON的url
字符串url=;
//检查用户是否有连接
ConnectionManager cm=
(ConnectionManager)getSystemService(Context.CONNECTIVITY_服务);
NetworkInfo=cm.getActiveNetworkInfo();
如果(信息!=null){
如果(!info.isConnected()){
Toast.makeText(这个,
“请检查您的连接,然后重试。”,Toast.LENGTH_SHORT)。show();
}
//如果是肯定的,在后台获取文章
else new getChartItems().execute(url);
}
//还有烤面包吗
否则{
Toast.makeText(这是“请检查您的连接并重试”,
吐司。长度(短)。show();
}
}
类getChartItems扩展异步任务{
//显示执行后台任务时的进度对话框
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
progressDialog=新建progressDialog(JsonActivity.this);
设置消息(“加载图表…”);
progressDialog.setUndeterminate(false);
progressDialog.setCancelable(假);
progressDialog.show();
}
//获取图表项数据的json数据,并将其显示在列表视图中
@凌驾
受保护的字符串doInBackground(字符串…args){
字符串url=;
字符串json=Parser.getJSONFromUrl(url);
System.out.println(“Json应该在我下面”);
System.out.println(json);
int-id;
字符串艺术家姓名;
试一试{
chartItems=新JSONArray(json);
JSONObject json_data=null;
对于(int i=0;i值
hashMap.put(艺人姓名,id);
//将HashMap添加到ArrayList
添加(hashMap);
}
}
捕获(JSONException e){
e、 printStackTrace();
}
runOnUiThread(新的Runnable(){
公开募捐{
//使用已解析的项更新列表视图
ListAdapter=new SimpleAdapter(JsonActivity.this、chartItemList、,
R.layout.listview,
新字符串[]{TAG_ID,TAG_ARTISTNAME},新int[]{R.ID.ID,
R.id.artistname});
setListAdapter(适配器);
}
});
返回null;
}
//获取数据后删除“进度”对话框
受保护的void onPostExecute(字符串参数){
progressDialog.disclose();
}
}
}
以下是日志:
01-19 15:45:49.816: D/libEGL(29490): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
01-19 15:45:49.867: W/System.err(29490): org.apache.http.conn.HttpHostConnectException: Connection to
(http://127.0.0.1) refused
01-19 15:45:49.898: D/libEGL(29490): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
01-19 15:45:49.902: D/libEGL(29490): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
01-19 15:45:49.914: W/System.err(29490): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
01-19 15:45:49.918: W/System.err(29490): at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-19 15:45:49.925: W/System.err(29490): at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
01-19 15:45:49.925: W/System.err(29490): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-19 15:45:49.925: W/System.err(29490): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-19 15:45:49.925: W/System.err(29490): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-19 15:45:49.929: W/System.err(29490): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-19 15:45:49.929: W/System.err(29490): at com.example.jsontester.JsonParser.getJSONFromUrl(JsonParser.java:36)
01-19 15:45:49.929: W/System.err(29490): at com.example.jsontester.JsonActivity$getChartItems.doInBackground(JsonActivity.java:83)
01-19 15:45:49.929: W/System.err(29490): at com.example.jsontester.JsonActivity$getChartItems.doInBackground(JsonActivity.java:1)
01-19 15:45:49.933: W/System.err(29490): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-19 15:45:49.933: W/System.err(29490): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-19 15:45:49.933: W/System.err(29490): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-19 15:45:49.933: W/System.err(29490): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-19 15:45:49.933: W/System.err(29490): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-19 15:45:49.937: W/System.err(29490): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-19 15:45:49.937: W/System.err(29490): at java.lang.Thread.run(Thread.java:856)
01-19 15:45:49.937: W/System.err(29490): Caused by: java.net.ConnectException: failed to connect to /<<127.0.0.1>> (port 80): connect failed: ECONNREFUSED (Connection refused)
01-19 15:45:49.941: W/System.err(29490): at libcore.io.IoBridge.connect(IoBridge.java:114)
01-19 15:45:49.941: W/System.err(29490): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:192)
01-19 15:45:49.945: W/System.err(29490): at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:459)
01-19 15:45:49.945: W/System.err(29490): at java.net.Socket.connect(Socket.java:842)
01-19 15:45:49.945: W/System.err(29490): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
01-19 15:45:49.957: W/System.err(29490): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
01-19 15:45:49.957: D/OpenGLRenderer(29490): Enabling debug mode 0
01-19 15:45:49.957: W/System.err(29490): ... 16 more
01-19 15:45:49.961: W/System.err(29490): Caused by: libcore.io.ErrnoException: connect failed: ECONNREFUSED (Connection refused)
01-19 15:45:49.972: W/System.err(29490): at libcore.io.Posix.connect(Native Method)
01-19 15:45:49.992: W/System.err(29490): at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:85)
01-19 15:45:49.992: W/System.err(29490): at libcore.io.IoBridge.connectErrno(IoBridge.java:127)
01-19 15:45:49.992: W/System.err(29490): at libcore.io.IoBridge.connect(IoBridge.java:112)
01-19 15:45:49.992: W/System.err(29490): ... 21 more
01-19 15:45:49.992: W/System.err(29490): java.lang.NullPointerException
01-19 15:45:49.996: W/System.err(29490): at java.io.Reader.<init>(Reader.java:64)
01-19 15:45:49.996: W/System.err(29490): at java.io.InputStreamReader.<init>(InputStreamReader.java:79)
01-19 15:45:50.000: W/System.err(29490): at com.example.jsontester.JsonParser.getJSONFromUrl(JsonParser.java:51)
01-19 15:45:50.000: W/System.err(29490): at com.example.jsontester.JsonActivity$getChartItems.doInBackground(JsonActivity.java:83)
01-19 15:45:50.007: W/System.err(29490): at com.example.jsontester.JsonActivity$getChartItems.doInBackground(JsonActivity.java:1)
01-19 15:45:50.011: W/System.err(29490): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-19 15:45:50.011: W/System.err(29490): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-19 15:45:50.023: W/System.err(29490): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-19 15:45:50.031: W/System.err(29490): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-19 15:45:50.031: W/System.err(29490): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-19 15:45:50.035: W/System.err(29490): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-19 15:45:50.035: W/System.err(29490): at java.lang.Thread.run(Thread.java:856)
01-19 15:45:50.039: I/System.out(29490): Json should be below me
01-19 15:45:50.050: W/System.err(29490): org.json.JSONException: End of input at character 0 of
01-19 15:45:50.125: W/System.err(29490): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
01-19 15:45:50.152: W/System.err(29490): at org.json.JSONTokener.nextValue(JSONTokener.java:97)
01-19 15:45:50.152: W/System.err(29490): at org.json.JSONArray.<init>(JSONArray.java:87)
01-19 15:45:50.152: W/System.err(29490): at org.json.JSONArray.<init>(JSONArray.java:103)
01-19 15:45:50.152: W/System.err(29490): at com.example.jsontester.JsonActivity$getChartItems.doInBackground(JsonActivity.java:92)
01-19 15:45:50.152: W/System.err(29490): at com.example.jsontester.JsonActivity$getChartItems.doInBackground(JsonActivity.java:1)
01-19 15:45:50.156: W/System.err(29490): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-19 15:45:50.156: W/System.err(29490): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-19 15:45:50.156: W/System.err(29490): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-19 15:45:50.156: W/System.err(29490): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-19 15:45:50.156: W/System.err(29490): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-19 15:45:50.156: W/System.err(29490): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-19 15:45:50.156: W/System.err(29490): at java.lang.Thread.run(Thread.java:856)
01-19 15:45:49.816:D/libEGL(29490):已加载/vendor/lib/egl/libEGL\u POWERVR\u SGX540\u 120.so
01-19 15:45:49.867:W/System.err(29490):org.apache.http.conn.HttpHostConnectException:连接到
(http://127.0.0.1)拒绝
01-19 15:45:49.898:D/libEGL(29490):已加载/供应商/lib/egl/libGLESv1\u CM\u POWERVR\u SGX540\u 120.so
01-19 15:45:49.902:D/libEGL(29490):已加载/供应商/lib/egl/libGLESv2\u POWERVR\u SGX540\u 120.so
01-19 15:45:49.914:W/System.err(29490):位于org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
01-19 15:45:49.918:W/System.err(29490):位于org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
01-19 15:45:49.925:W/System.err(29490):at org.apache.http.impl.conn.abstractPooledConnapter.open(AbstractPooledConnapter.java:119)
01-19 15:45:49.925:W/System.err(29490):位于org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
01-19 15:45:49.925:W/System.err(29490):位于org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
01-19 15:45:49.925:W/System.err(29490):位于org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
01-19 15:45:49.929:W/System.err(29490):位于org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
01-19 15:45:49.929:W/System.err(29490):位于com.example.jsontest.JsonParser.getJSONFromUrl(JsonParser.java:36)
01-19 15:45:49.929:W/System.err(29490):在com.example.jsontest.JsonActivity$getChartItems.doInBackground(JsonActivity.java:83)
01-19 15:45:49.929:W/System.err(29490):位于com.example.jsontester.JsonActivity$getChartItems.doInBackground(JsonActivity.java:1)
01-19 15:45:49.933:W/System.err(29490):在android.os.AsyncTask$2.call(AsyncTask.java:287)
01-19 15:45:49.933:W/System.err(29490):在java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
01-19 15:45:49.933:W/System.err(29490):在java.util.concurrent.FutureTask.run(FutureTask.java:137)处
01-19 15:45:49.933:W/System.err(29490):在android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-19 15:45:49.933:W/System.err(29490):位于java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-19 15:45:49.937:W/System.err(29490):位于java.util.concurrent.Threa