在尝试构建我的第一个与远程服务器文件通信的应用程序时,我试图从Android应用程序访问本地服务器上的php文件(带有硬编码数据)。问题是php文件正在返回xml数据,而不是json但是我已经放了
标题(' content-type:application / json; charset = utf-8')
确保只返回json但没有用。下面是我的andoroid应用程序和php文件的代码。有人可以帮忙吗?我还把log cat只显示了一堆xml标签,但没有显示数据
活动代码段
String responseString;
responseString = com.rrdtech.example.generic.GenericUtility.getResponse(url_all_products);
Log.i("inside doinbg=",responseString);
下面的getresponse(url)代码
public static String getResponse (String url)
{
HttpEntity entity = null;
StringBuilder sb = null;
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpGet httppost = new HttpGet(url);
Log.e("log_tag_htppost", httppost.toString());
HttpResponse response = httpclient.execute(httppost);
entity = response.getEntity();
InputStream is = entity.getContent();
Log.e("log_tag", "after input stream");
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line="0";
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
}catch(Exception e)
{
Log.e("log_tag", "Error in http connection"+e.toString());
}
return sb.toString();
}
LOG CAT值低于......我已经用标签doinbg
打印响应 11-27 14:40:18.058: I/DBtest2mainscreen(7476): clicked on the view all products
11-27 14:40:18.150: E/log_tag_htppost(7476): org.apache.http.client.methods.HttpGet@428bee98
11-27 14:40:18.710: E/log_tag(7476): after input stream
11-27 14:40:18.789: I/inside doinbg=(7476): <?xml version="1.0" encoding="UTF-8"?>
11-27 14:40:18.789: I/inside doinbg=(7476): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
11-27 14:40:18.789: I/inside doinbg=(7476): "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
11-27 14:40:18.789: I/inside doinbg=(7476): <html xmlns="http://www.w3.org /1999/xhtml" lang="en" xml:lang="en">
11-27 14:40:18.789: I/inside doinbg=(7476): <head>
11-27 14:40:18.789: I/inside doinbg=(7476): <title>Access forbidden!</title>
11-27 14:40:18.789: I/inside doinbg=(7476): <link rev="made" href="mailto:you@example.com" />
11-27 14:40:18.789: I/inside doinbg=(7476): <style type="text/css"><!--/*--><![CDATA[/*><!--*/
11-27 14:40:18.789: I/inside doinbg=(7476): body { color: #000000; background-color: #FFFFFF; }
11-27 14:40:18.789: I/inside doinbg=(7476): a:link { color: #0000CC; }
11-27 14:40:18.789: I/inside doinbg=(7476): p, address {margin-left: 3em;}
11-27 14:40:18.789: I/inside doinbg=(7476): span {font-size: smaller;}
11-27 14:40:18.789: I/inside doinbg=(7476): /*]]>*/--></style>
11-27 14:40:18.789: I/inside doinbg=(7476): </head>
11-27 14:40:18.789: I/inside doinbg=(7476): <body>
11-27 14:40:18.789: I/inside doinbg=(7476): <h1>Access forbidden!</h1>
11-27 14:40:18.789: I/inside doinbg=(7476): <p>
11-27 14:40:18.789: I/inside doinbg=(7476):
11-27 14:40:18.789: I/inside doinbg=(7476): </p><hr />
11-27 14:40:18.789: I/inside doinbg=(7476): <p style="margin-left: 2.6em; font-size: 1.2em; color: red;">New XAMPP security concept:</p>
11-27 14:40:18.789: I/inside doinbg=(7476): <p>Access to the requested object is only available from the local network.</p>
11-27 14:40:18.789: I/inside doinbg=(7476): <p>This setting can be configured in the file "httpd-xampp.conf".</p>
11-27 14:40:18.789: I/inside doinbg=(7476): <hr /><p>
11-27 14:40:18.789: I/inside doinbg=(7476):
11-27 14:40:18.789: I/inside doinbg=(7476): </p>
11-27 14:40:18.789: I/inside doinbg=(7476): <p>
11-27 14:40:18.789: I/inside doinbg=(7476): If you think this is a server error, please contact
11-27 14:40:18.789: I/inside doinbg=(7476): the < a href="mailto:you@example.com">webmaster</a>.
11-27 14:40:18.789: I/inside doinbg=(7476): </p>
11-27 14:40:18.789: I/inside doinbg=(7476): <h2>Error 403</h2>
11-27 14:40:18.789: I/inside doinbg=(7476): <address>
11-27 14:40:18.789: I/inside doinbg=(7476): <a href="/">192.168.1.101</a><br />
11-27 14:40:18.789: I/inside doinbg=(7476): <span>Apache/2.4.10 (Unix) OpenSSL/1.0.1i PHP/5.5.15 mod_perl/2.0.8-dev Perl/v5.16.3</span>
11-27 14:40:18.789: I/inside doinbg=(7476): </address>
11-27 14:40:18.789: I/inside doinbg=(7476): </body>
11-27 14:40:18.789: I/inside doinbg=(7476): </html>
以下是json.php
<?php header('content-type: application/json; charset=utf-8');
$response = array();
$response["products"] = array();
$product = array();
$product["pid"] = "1";
$product["name"] = "iphone 4s";
$product["price"] = "300.00";
$product["created_at"] = "2012-04-29 02:04:02";
$product["updated_at"] = "0000-00-00 00:00:00";
array_push($response["products"], $product);
$response["success"] = 1;
echo json_encode($response);
?>
新的json解析器类.....
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
// json_string = getJSONString(httpResponse);
// Log.i("calling method =","getjsonstring");
// Log.i("json_string=",json_string);
HttpEntity httpEntity = httpResponse.getEntity();
//is = httpEntity.getContent();
if (httpEntity != null)
{
is = httpEntity.getContent();
String response1= is.toString();
Log.i("httpresponse",response1);
}
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
答案 0 :(得分:1)
这不是xml。这是带错误的HTML。它说:
Access to the requested object is only available from the local network
因此,您应该正确配置服务器。
答案 1 :(得分:0)
由于跛脚我面临的错误。我试图找出解决方案,并发现一些链接/博客说更新httpd-xampp.cnf文件,包括字符串“允许所有”,但它帮助了很多人,但不是我的情况。我已经拥有我的网站托管所以我把PHP文件放在那里,我能够得到json对象输出。感谢大师的帮助!