我是android的新手。我有一个只有json格式的url。假设url是- http://www.example.com/filter...此url仅包含json格式的数据。例如,此链接包含以下材料:-{u:“用户”,s:“男性”图像:“hello.jpg”}此链接仅包含上述数据。现在,我想在我的android i.e.whether中测试连接,我可以连接到参数u的值。我创建了一个http客户端,并尝试解析json格式。程序,但不显示任何内容..下面是我所做的
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.com/filter");
try
{
HttpResponse response = httpclient.execute(httppost);
InputStream in = response.getEntity().getContent();
byte [] buffer = new byte[in.available()];
while (in.read(buffer) != -1);
String jsontext = new String(buffer);
JSONArray entries = new JSONArray(jsontext);
for (i=0;i<entries.length();i++)
{
JSONObject post = entries.getJSONObject(i);
if(post.getString("u")=="driver")
{
x="Success";
}
}
txtResult.setText(x); // x is string
}我只想测试一下我的连接。请帮帮我!
发布于 2011-10-20 16:50:20
您的JSON无效。密钥必须用引号括起来:
[{"u": "user", "s": "male", "image": "hello.jpg"}]它可能是有效的JavaScript,但它不是有效的JSON。
您可能还希望查看logcat并发布发生的任何异常(我确信您在某个地方会得到一个无效的JSON异常)。
发布于 2011-10-20 16:46:40
如果你想从你的响应{u:"user",s:"male", image:"hello.jpg"}中解析出"u“,那么你的代码看起来还不错,除了你已经实现的字符串比较:
错误:
if(post.getString("u")=="driver")
{
x="Success";
}正确:
if(post.getString("u").eqaulIgnoreCase("driver"))
{
x="success"
}https://stackoverflow.com/questions/7833201
复制相似问题